-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some more ROTable preparation / optimisations pre Lua 5.3 #2858
Comments
Re the last comment if it looks worthwhile performance-wise then post lua53, we can always back engineer the new ROtable format into lua51 once we have it stable in Lua 53. I could also write a perl or python script to validate the Table declarations, rather than doing it manually. |
@jmattsson , @nwf et al, feedback requested. Now that my local branch has both the This exercise of fixing up the modules for Lua 5.3 compatibility has really underline for me a conclusion: that we've been lax about how we have allowed our modules to be implemented when interfacing to the Lua runtime. Both the Lua Reference Manual and PiL make it clear that the complete public API for C modules is as documented in There is also set of supplementary functions provided by an auxiliary library (auxlib) as documented in So all of our modules and helper libraries should only include A quick analysis of all of the 8266 modules shows these issues:
We have had the previous discussion about letter numbering (
|
Agreed. The big question mark is how we deal with |
To be honest IMO the best way to allocate resources is to use the Userdata mechanism. The overhead of doing so is pretty minimal. and the nice thing is that it is fully integrated into the Lua GC so if you need a temporary resource then just create it using userdata and put a reference on the stack. It doesn't matter whether the routine return normally or throws an error: the stack entry gets dereferenced and the resource will be recovered on the next GC run -- this makes coding leak-free routines a lot simpler with minimal overhead. The problem with the lmem.h interface is that it is complicated and confusing. Failing this we should just have a simple set of malloc / calloc / realloc / free macros/wrappers which follow the posix API and do " a normal malloc. if NULL return then do a Lua GC and retry the malloc before returning NULL". KISS |
I could certainly get behind a set of wrapper functions for the allocs. I'd have to dig into the IDF to work out what the best approach there would be, but memory is far less of an issue there in the first place. |
I would clearly prefer the userdata approach here. We have a higher level memory management available which frees us of the sometimes tedious task of keeping track of memory. It also includes GC before fail already. KISS |
Gregor, what I see is a multiple pronged approach.
|
Could we convert LwIP and mbedTLS to using the lua heap / udata objects? I think it's plausible, if not actually true, that tying mbedTLS's allocations to the Lua heap would help during the surges in heap requirement seen for TLS handshaking. |
I've decided that it's going to be a lot easier to maintain the modules if the public Table / ROTable interface is the same for both versions, so I'm regressing the Lua53 table handling into our app/lua codebase: faster and closer to standard Lua. All ROtables have a extra row at the start (generated by the LROT_TABLE macro) which contains a cut-down Table record, so apart from the |
Now implemented in both Lua 5.1 and 5.3 |
ROTable performance changes
__metatable
or__index
. We have therefore added an ordering constraint on all ROTables: fields with keys starting with the character "_" must be located at the head of the table in ascending order.strcmp(pentry->key, strkey)>=0
which means that typically only one or two fields are checked before the scan is aborted.luaR_entry
key
field now direclty points to the CString key name which are now word-aligned in Flash. The scan now does a word-read from flash of thepentry->key
and*pentry->key
and does a masked compare against the 1st word of the search key. (The mask is to accommodate keys shorter than 3 chars in length.) This means that in practice the scan is a small inline loop with no unaligned exceptions or calls to the ROMstrcmp()
(other than the last hit validation if there is a key match), and this executes fast.In practice this means that ROTable access is now perhaps 3× slower than equivalent RAM Table accesses, rather eLua case of perhaps 100× slower.
Preparing for
lua53
In the Lua53 implementation, all tables will have a
Table
header, (though this is a reduced variant which omits unused fields in the case of ROTables). TheLROT_
macros have been designed to hide these implementation differences at a source level in our C modules.__metatable
field. The core Lua VM only uses the__*
metafields in a metatable and ignores them in a normal table, but here any normal table is scanned for the__metatable
entry to determine if it uses a metatable. This entry has been encapsulated in theLROT_METATABENTRY( mt )
entry which expands to aLROT_TABENTRY( __metatable, mt )
.LRO_END()
macro and this is used in the generation of theROTable
's header. Note that this field is ignored in lua51 builds and similarly theLROT_METATABENTRY()
declarations are treated as empty expansons in lua53 builds. This enable the same module source to be used for both targets.flags
as an existence check for the common meta fields such as__index
to avoid unnecessary table accesses. This optimisation is bypassed for the lua51 eLua patch. However it is honoured in the lua53 implementation and the 3rd parameter of theLRO_END()
macro contains this static flag setting.Suggestions for reviewers reviewing these meta fields
Try using the command:
__*
fields must be in ascending order and consecutive immediately following theLROT_BEGIN()
statement.LRO_END()
macro such beNULL
unless this is a ROTable that uses another ROTable as its metatable.LRO_END()
macro should be 0 unless the table is used as a metatable and in this cae should match the corresponding metafield entries.Comment:
I realise that this change denomalises the source which is a bad thing, and if anyone can think of a better approach to us keeping this ROTable code base common to lua51 and lu53 then I am open to suggestions.
The text was updated successfully, but these errors were encountered: