I really enjoy the external string feature in Lua 5.5, but I believe
there is still room for improvement, particularly in scenarios
involving multiple Lua virtual machines.
I would like to see certain constant objects separated from the Lua
state entirely, so they are not subject to garbage collection.
Instead, they should be managed through an additional mechanism to
ensure proper lifecycle control.
When an object is marked as a constant object, it should only be
collected during lua_close and should not be subject to the standard
garbage collection process. The existing luaC_fix API already achieves
this by marking objects as "fixed" in the GC system. However, in the
current Lua implementation, the luaC_fix API is used sparingly.
I think the function prototypes loaded via lua_load, as well as the
constant strings referenced within them, could all be marked as
fixed/constant objects.
I would like to propose adding a "readonly table" feature. When a
table's all keys and values are constant objects, it could be
converted into a readonly table and marked as a constant object. This
would allow the Lua runtime to treat such tables as immutable,
preventing unintended modifications while maintaining performance
optimizations for constant data structures.
You can convert a constant object into a C pointer using lua_topointer
(or a newly added API), and then push it into the Lua state via an API
like lua_pushconstantobject. This would enable efficient cross-Lua
state transmission of constant objects, as the C pointer would serve
as a lightweight reference to the immutable data.
I believe this new mechanism could significantly accelerate the
startup time of new Lua virtual machines in multi-Lua VM scenarios.
For example, in a web server framework, each request could quickly
create a new Lua state by reusing pre-prepared constant objects in the
framework's Lua state. This would eliminate the need to re-initialize
code and data structures for every request, as the necessary code and
data would already be stored as constant objects in the framework's
Lua state, enabling rapid reuse across new Lua states.
For scenarios that rely on massive and complex data structures, this
mechanism can significantly reduce the time spent on garbage
collection (GC) marking. Since most of the data is immutable, the GC
cycle can directly skip these objects during the marking phase. This
optimization would minimize the overhead of scanning and tracking
immutable data, allowing the GC to focus only on mutable objects and
thus improving overall performance.
--
http://blog.codingnow.com