Changed access to global table in the registry
The global table is always in the array part of the registry; we can use this fact to make its access slightly more efficient.
This commit is contained in:
25
lapi.c
25
lapi.c
@@ -629,11 +629,21 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Get the global table in the registry. Since all predefined
|
||||||
|
** indices in the registry were inserted right when the registry
|
||||||
|
** was created and never removed, they must always be in the array
|
||||||
|
** part of the registry.
|
||||||
|
*/
|
||||||
|
#define getGtable(L) \
|
||||||
|
(&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
||||||
Table *reg;
|
const TValue *G;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
reg = hvalue(&G(L)->l_registry);
|
G = getGtable(L);
|
||||||
return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
|
return auxgetstr(L, G, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -811,10 +821,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
|||||||
|
|
||||||
|
|
||||||
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
||||||
Table *reg;
|
const TValue *G;
|
||||||
lua_lock(L); /* unlock done in 'auxsetstr' */
|
lua_lock(L); /* unlock done in 'auxsetstr' */
|
||||||
reg = hvalue(&G(L)->l_registry);
|
G = getGtable(L);
|
||||||
auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
|
auxsetstr(L, G, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1063,8 +1073,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
|||||||
LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
|
LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
|
||||||
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
||||||
/* get global table from registry */
|
/* get global table from registry */
|
||||||
Table *reg = hvalue(&G(L)->l_registry);
|
const TValue *gt = getGtable(L);
|
||||||
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
|
|
||||||
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
||||||
setobj(L, f->upvals[0]->v, gt);
|
setobj(L, f->upvals[0]->v, gt);
|
||||||
luaC_barrier(L, f->upvals[0], gt);
|
luaC_barrier(L, f->upvals[0], gt);
|
||||||
|
|||||||
9
lstate.c
9
lstate.c
@@ -213,17 +213,14 @@ static void freestack (lua_State *L) {
|
|||||||
** Create registry table and its predefined values
|
** Create registry table and its predefined values
|
||||||
*/
|
*/
|
||||||
static void init_registry (lua_State *L, global_State *g) {
|
static void init_registry (lua_State *L, global_State *g) {
|
||||||
TValue temp;
|
|
||||||
/* create registry */
|
/* create registry */
|
||||||
Table *registry = luaH_new(L);
|
Table *registry = luaH_new(L);
|
||||||
sethvalue(L, &g->l_registry, registry);
|
sethvalue(L, &g->l_registry, registry);
|
||||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||||
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
||||||
setthvalue(L, &temp, L); /* temp = L */
|
setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L);
|
||||||
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
|
/* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
|
||||||
/* registry[LUA_RIDX_GLOBALS] = table of globals */
|
sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
|
||||||
sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
|
|
||||||
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user