lua-users home
lua-l archive

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


As part of my "Lua for microcontrollers" project (, I'm trying to reduce the memory footprint needed by Lua. With all the eLua auxiliary modules enabled, "print(collectgarbage'count')" reports about 25k right after the Lua interpreter starts - quite a bit for a micro than can only access 64k of RAM. So I'm looking for ways to reduce this.
The first idea was that I don't really need "full tables" for the Lua modules, I'd rather have some sort of "read only tables" that can read their keys directly from Flash instead of RAM (keep in mind that RAM consumption is "more important" than execution speed here). The obvious implementation was to call "luaL_register" with a table that contains only the "__index" function, and set the metatable of this table to itself. All table requests are thus redirected to the "__index" function, which searches a fixed array of {key, value, type (function or number} (that resides in the Flash memory) and returns the proper key value and type, or nil.
I didn't yet implement this for all modules, but I already know that it works and that it will have a good impact on the memory consumption. What I'm wondering at this point is if I can go even further. The idea presented above assumes that each module still needs to call "luaL_register", thus each module creates a new table in RAM. What I'd really like is to have a _single_ table that can handle the access to _all_ my modules. In other words, given the modules m1, m2, ... mn, I'd like to differentiate between m1.func(), m2.func(), ... , mn.func() with a single (meta)table. And I don't know how (if?) this is possible, given the fact that the (meta)table somehow needs to know for what "table" (m1, m2, ... mn) it must do the key search.
Do you know of any way to implement something like this? I know I can do this by introducing a completely new data type (something like ROTABLE) and implement the required semantics, but given the fact that I'm not very familiar with the Lua code, I'd rather skip this option for now.