Implement inline caching for tables (optimization) #2
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Like https://github.com/v8/v8/wiki/Design%20Elements#fast-property-access explains, LuaJ could optimize field accesses by implement a trick called "inline caching". I think this optimization is done for prototype-based things, but maybe in Lua we can base in metatable's __index.
How would it work? Suppose we've an empty table
t. This table is currently based in an hidden class, call it,C0, which tells it the offset of which each field will be stored in memory. Each time it defines a new field, it does transition fromCAtoCBhidden class, in this way classCBtells how the fields that were in the previousCAclass are stored and how the new field is stored (all the previous fields can be in the same offset of course). When there's noCBclass, then a newCBclass must be created, structuring it the same way as described before. And so on.When a field is suddenly deleted (with
nil,-nanor __mode), then there're two things to do: 1. Create a new hidden class, which tells the table to store its fields in hash mode. 2. Fill the deleted field offset with thenilvalue.In this optimization it'd be much performant for a code to add fields in the same sequence, because otherwise then other hidden classes will be created for that sequence. Of course everyone will add fields sequentially in the same order as tables of the same type.
In order to avoid accumulating hidden classes for the table type, this optimization must only happen with metatable __index based tables, a.k.a. instances.