Suggestion : More fixed binaries

89 views
Skip to first unread message

云风 Cloud Wu

unread,
Jul 23, 2025, 6:16:36 AMJul 23
to lu...@googlegroups.com
Lua 5.5.0 support static (fixed) binaries, as the document said "when
loading a binary chunk in memory, Lua can reuse its original memory in
some of the internal structures"

But I think we can do it more thoroughly.

I think the whole proto structure can be fixed , not just lineinfo and
abslineinfo.
In order to implement it, we need to add an external short string type
for the short strings in constants, and using offset rather than
memory address for the pointers.

The benefits :

1. Loading lua bytecodes (undump) can be O(1).
2. Save more memory for embedded environment.
3. Save lots of memory for a large number of virtual machines. (We
used thousands of lua states in one process)

Generating a standalone lua virtual machine with the same code is much
lighter, the cost is almost the same as generating a Lua closure.

--
http://blog.codingnow.com

Sainan

unread,
Jul 23, 2025, 6:29:05 AMJul 23
to lu...@googlegroups.com
> Loading lua bytecodes (undump) can be O(1).

No. When you have structs referencing each other, you need pointers in memory and offsets on disk/wire. You're not avoiding having to do some conversion.

-- Sainan

云风 Cloud Wu

unread,
Jul 23, 2025, 6:48:37 AMJul 23
to lu...@googlegroups.com
'Sainan' via lua-l <lu...@googlegroups.com> 于2025年7月23日周三 18:29写道:
>
> > Loading lua bytecodes (undump) can be O(1).
>
> No. When you have structs referencing each other, you need pointers in memory and offsets on disk/wire. You're not avoiding having to do some conversion.
>

We can put the offset of bytescode chunk instead of memory address in
the Proto structures.

btw, I have already used an O(1) function loader for years (but based
on another implementation method).

I patched lua ( https://github.com/ejoy/lua/tree/skynet55 ) , and
introduced a new api lua_clonefunction() (
https://github.com/ejoy/lua/blob/skynet55/src/lapi.c#L1142 ) to clone
a function proto from a lua state into another.

It can load a chunk in O(1), and It has been working very well all
these years. It makes starting a new lua state with code much faster.




--
http://blog.codingnow.com

云风 Cloud Wu

unread,
Jul 23, 2025, 7:08:00 AMJul 23
to lu...@googlegroups.com
云风 Cloud Wu <clo...@gmail.com> 于2025年7月23日周三 18:16写道:
>
> In order to implement it, we need to add an external short string type

About "external short string", we can use an unique id to compare
strings in O(1).

Roberto mentioned in the previous post "Suggestion : Use unique string
type instead of two (short and long)" (
http://lua-users.org/lists/lua-l/2019-06/msg00424.html )
Lua does not demand platforms to have a 64-bit integer type .

But for the external (fixed?) short strings, I think a 32bit integer
id is reasonable.




--
http://blog.codingnow.com
Reply all
Reply to author
Forward
0 new messages