lua-users home
lua-l archive

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

>   We are making a debug functionality that aims to store the X last
> executed Lua source lines in a list. Is it OK to store the 'source'
> field from the 'lua_Debug' structure as a pointer? (because copying a
> string each time a Lua line is executed is very slow and not
> plausible) Or is there any chance that they are garbage collected?

Currently, Lua does not define the validity of these pointers (it
should...). Anyway, the answer to your question depends on how
"religious" your code should be.

To assume that the pointer is valid as long as the prototype is not
collected is a reasonable compromise (we have no plans for a copy
garbage collector). Like everything else in Lua, prototypes are
collected when they are not more accessible. That means that (1) there
are no instances (functions) of it and (2) any prototpye (syntactically)
enclosing it is also not accessible.

Many programs never erase main functions (and therefore will never
collect prototypes). But some do :)

Is it too expensive to put those strings in a table? Something like this
(but in C):

local count = 0
local t = {}
function getcounter (s)
  local c = t[s]
  if not c then
    count = count + 1
    c = count
    t[s] = c
    t[c] = s
  return c

Then you can store the counters instead of the strings. (If most sources
are repeated, as they should be, the average overhead is one table
lookup.)  (Or yet, you can put the functions themselves in the table
and get their sources only when needed.)

-- Roberto