lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Wim Couwenberg wrote:
Chris,


That makes sense. In the first version, some implicit functions are
being executed, which are faster than explicitly calling pairs().


Chris, the call of pairs is not important in this example.  You have
to keep in mind that a for loop like:

    for k, v in pairs(t) do ... end

calls pairs *only once*, regardless of the size of the table.  It is
the function "next" that gets called (implicitly) on each iteration.
And though this is the same function as it was in Lua 4, in Lua 5.x it
is now executed as any other C function call in a script.  This, plus
additional stack slot copying, incurs the overhead.


Seems like a 2x improvement in this sort of thing is A Good Thing (tm).


That stands.  :-)  The factor can be at most this high, it will drop
if the loop actually performs something useful...

But that's what all optimization is about, right? Double the performance of something and, if it is used 10% of the time that is a 10% boost, which is significant. If it is used 0.1% of the time, then not so much.

And to (hopefully) add to the clarity of this discussion, I now remember why this "feature" was removed. It's right there in the error message that I now get from 5.1. There was an ambiguity before. If I say:

    for p in t do ...

is the intent to iterate over t or to call the __call() metamethod of t and get an iterator from that? I think that 5.0 iterated t unless t had a __call() metamethod, which is very fragile.

So I fully accept the value of iterators and the need to define this statement as only working for iterators. But I still think it would be valuable to have a statement which allowed simple table enumeration.

In fact, it might have been better to define the iterator syntax like this:

    for p = next,t,nil do ... end

which looks like the numeric for loop. If the first expression is a number, you would use numeric iteration. If it is a function, you would use iterators. Then you could still do:

    for p = pairs(t) do ... end

which looks pretty nice to me. But, of course this is all water under the bridge!

--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'