In view of my previous comments on Lua, I need to revise my remarks here: the most mature client development portfolio in the game development language work: C/D, lua/c#, assembly assembly. The C + + series is used to complete the game engine framework, assembly for optimization, and LUA for game logic. Meet the needs of performance, readability, and variability on a global-wide. Because I chose the direction of the engine, I could actually only thoroughly understand the C/S series to get an engine job. Also recommended a book "Game Engine Framework"-Ye Jin translation of the book, and did not csdn someone to read after the speech "engine water really deep" feeling. The book was well written, but the man in Ye Jin's interview said such a book is difficult, must have scared back some new people or at least poured cold water.
The weak reference of Lua is primarily to implement a reference counting function, or to manage memory. I suspect that the reference management of table is based on weak references. Enable weak references by setting the __mode= "kv" of a table.
When a weak reference is similar to a state with a reference count of 0, when the call to garbage collection collectgarbage () clears the reference calculation to 0 (analogy, currently I do not see the garbage collection implementation of the source code). , each time an assignment is performed, a +1 operation is performed on the object to which the address points, and the value, bool, string, should be the constant. There is a problem with this three types of data cleanup how to implement? Cond
First of all know that table is an object, the usual reference assignment is the address plus the reference technology. Look at the table and node source definitions.
typedef struct TABLE { commonheader; Lu_byte flags; /* 1<<p means Tagmethod (p) is not present */ lu_byte Lsizenode; /* log2 of size of ' node ' array */ struct Table *metatable; TValue *array; /* Array part */ Node *node; Node *lastfree; /* Any free position are before this position */ gcobject *gclist; int Sizearray; /* Size of ' array ' array */} Table;
Array part:
* * Union of all Lua values*/typedef Union { Gcobject *GC; void *p; Lua_number N; int b;} value;//This is a very classical data structure, you can name it as a LUA data atom, to complete the vast majority of data storage labeling 1/*** Tagged values*/#define TVALUEFIELDSVALUE value; The int TT//tt is used to record the data type stored in the current data field typedef struct LUA_TVALUE { tvaluefields;} TValue;
Key-value pairs section:
typedef Union TKey { struct { tvaluefields; struct Node *next; /* For Chaining */ } NK; TValue TVK;} tkey;typedef struct Node { TValue i_val; TKey I_key;} Node;
The data in table is divided into three parts: MetaTable, which is used to manage the operation definition of the current class, and array is obviously the array part; node is a common key-value pair. To be continued, we have to see what kind of hash function Lua uses.
Callout 1 First post a quote blog, worth reading click Open link
A weak reference is related to memory storage. Number, bool, string, etc. to clean up to the mechanism of C to manage, is the C compiler implementation of the set, the rest of the data atomic value of the data object, all conform to the use of weak reference environment.
Weak references have two major uses: the Pure data cache (cache, Memo), Tabel, and other objects in the namespace management (object properties).
Pure data caching: such as LoadString, LoadFile, or the RGB data cache mentioned in the book, this can share similar data in a sub-region, reducing duplication of operations. Saving in a buffer can only be a weak reference, and when the external strong reference is all set to nil, only the weak reference of the buffer is left, then the cache will be cleared at the next collectgarbage call.
Object properties: Because closure related techniques are used, the same table may be in a different syntactic domain lexical scoping, and additional information must be used to log the table's accessible path. This property does not belong to the original table, nor is it a cache of pure data, which, according to the book, should be the addition of the table record of the currently accessible region in the lexical domain, when an external weak reference is used.
Lua Programming Chapter 17th Weak citation notes