lua-users home
lua-l archive

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


Title: RE: suggestion : method to push constant strings

> Hmm.  Do you know that lua interns its strings?  What I mean
> is for any
> particular sequence of characters that form a string lua keeps at most
> one copy of that sequence in its heap.  So if I go:
>
> lua_pushstring(L, "spong")
> lua_pushstring(L, "spong")
>
> there is only one copy of the string "spong" in the heap
> (there are two
> objects both of which reference the same character sequence,
> but that's
> unavoidable in any scheme).

When implementing a notification system, where events in a game
engine end up generating calls to Lua scripts, it is common to
use strings as parameters. These strings are only "active" in the
Lua function called from C++ side, and are GC'd as the script
returns. Such strings have a very low lifetime, and do not need
to be otherwise "managed" by Lua. Moreover, the "interning" of
strings by Lua mean that there will be (at least) a hash lookup
whenever lua_pushstirng is called, which can add up when the
script is just making simple boolean tests and return the value,
which is rather common in my case.
I am aware of course that I can take a reference to often used
strings, and use this to speed it up.

--
Vincent Penquerc'h