[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: caching pointers to Lua strings?
- From: Josh Haberman <jhaberman@...>
- Date: Fri, 10 Jun 2011 01:48:57 +0000 (UTC)
The documentation for lua_tolstring() says:
Because Lua has garbage collection, there is no
guarantee that the pointer returned by
lua_tolstring will be valid after the
corresponding value is removed from the stack.
--http://www.lua.org/manual/5.1/manual.html#lua_tolstring
What if I guarantee that the string is still reachable,
even after it is removed from the stack?
Say I had a userdata whose environment table contains
a reference to a given string. Is it safe to cache the
pointer to that string (obtained from lua_tolstring())
inside the userdata? Or could Lua perform compacting
garbage collection that moves the string to somewhere
else, even though it is alive (thus invalidating my
cached pointer)?
It would be slightly strange if the stack was "special"
in that strings on the stack are not subject to being
relocated but other reachable strings are.
Thanks,
Josh
p.s. The caching I want to perform is for functional
reasons (not performance), so please don't get hung up
on the fact that the performance difference would
probably not be measurable.