lua-users home
lua-l archive

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


Alex Davies wrote:
> I know bumping old posts is very (extremely?) bad form [...]

Well, only if you don't read it in context ... (see below).

> The idea is easy enough to implement, and with a small
> optimization (to recognise vararg appends / vararg pops) some
> functions that are currently impossible to write in O(n) time
> Lua become possible. The vm would require a second value stack
> for arguments, but that's possibly a better solution then the
> current varargs-between-call-frames anyway imo. Simplifies
> callinfos somewhat, removing the need for a func stkid.

Umm, no. Another stack drives up complexity. We already have two
semi-synchronous stacks (TValue stack and Callinfo aka the frame
stack). I've gotten rid of Callinfo completely in LuaJIT 2.x -- it
improved performance remarkably and simplified many code paths.

Introducing another stack/queue is certainly not the way to go.
First you have to put in all the magic to make the compiler
recognize the append/prepend cases to keep them O(1). And then you
have to implement a data structure that can efficiently grow on
either side and still avoid pathological unbounded growth. And you
somehow need to attach it to the current frame and keep many of
these data structures at once for all vararg frames. Ugh ...

>   while ... do
>     local x, ... = ...
>     res = res + x
>   end

This probably looks like line-noise for anyone not as deep into
Lua as you and me. It's very unlikely that '...' as an lvalue is a
concept that can be easily comprehended and safely used. Without
an efficient append-at-front this would be O(n^2) again. I'm not
sure you want to write the docs explaining all the cases that do
lead to degenerate behaviour and those that don't.

But the same thread you're resurrecting had this posting about
select() vs. apairs() peformance:

  http://lua-users.org/lists/lua-l/2006-04/msg00120.html

IMHO many use cases become much more readable with apairs().
And your example is now O(n), too:

  function sumargs(...)
    local res = 0
    for i,x in apairs(...) do
      res = res + x
    end
    return res
  end

apairs() would be a simple 10-line addition to the standard
library and requires no changes to the Lua core.

--Mike