[opensource-dev] Review Request: Squared all dist_vec() based comparisons and other dist_vec() operations where sensible.

Oz Linden (Scott Lawrence) oz at lindenlab.com
Sat Mar 12 07:34:25 PST 2011


On 2011-03-11 11:43, Ricky wrote:
> Thanks for the review! Yes, I am familiar with those "principles of 
> optimization" - yet it seemed to just feel wrong to leave it alone... :P
>
> As to the variables that are initialized with high numbers, there has 
> to be a better way: if these were standard for-loops, I would just 
> initialize the first value with the first item in the list, and then 
> start the loop at the second value, if any.  However, these are a 
> iterator style I am not yet familiar enough with to bend that far. 
>  Any suggestions?

When I write a for loop that has multiple control variables, I use comma 
expressions.  For example, if I wanted to iterate over selections and 
operate on the first MAX_SELECTIONS or until there were not any more one 
could put the count outside the loop:

    S32 counter = 0;
    LLObjectSelection::root_iterator  it;
    for (it  =  getSelection()->root_begin();
          it  !=  getSelection()->root_end();
          ++it)
    {
         if (counter>= MAX_SELECTIONS)
         {
             break;
         }
         // whatever it is the loop does

         counter++;
    }


One could make that better by moving the counter test up into the second 
expression in the for loop, but I'd take the extra step of also putting 
the initialization and increments of both variables into the for:


    S32 counter;
    LLObjectSelection::root_iterator  it;
    for (it  =  getSelection()->root_begin(), counter = 0;
          it  !=  getSelection()->root_end()&&  counter<  MAX_SELECTIONS;
          ++it, ++counter)
    {
         // whatever it is the loop does
    }

I think that makes it clearer that both variables are controlling the loop.

Comma expressions are not seen all that often (this sort if thing is the 
only really good application of them I can think of), but I've never 
found a compiler that didn't support them correctly.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.secondlife.com/pipermail/opensource-dev/attachments/20110312/2aa099f5/attachment.htm 


More information about the opensource-dev mailing list