diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-11-26 18:23:40 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-11-26 18:23:40 -0300 |
commit | 65d2294454ab68d164154c7ce05caa50bd97d143 (patch) | |
tree | 6cd1a6d00eaf7156d1537d165de26abf82787782 | |
parent | 131e3fd814a6e818b412407a222186aab08f3525 (diff) | |
download | lua-65d2294454ab68d164154c7ce05caa50bd97d143.tar.gz lua-65d2294454ab68d164154c7ce05caa50bd97d143.tar.bz2 lua-65d2294454ab68d164154c7ce05caa50bd97d143.zip |
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.
-rw-r--r-- | lapi.c | 25 | ||||
-rw-r--r-- | lstate.c | 9 |
2 files changed, 20 insertions, 14 deletions
@@ -629,11 +629,21 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { | |||
629 | } | 629 | } |
630 | 630 | ||
631 | 631 | ||
632 | /* | ||
633 | ** Get the global table in the registry. Since all predefined | ||
634 | ** indices in the registry were inserted right when the registry | ||
635 | ** was created and never removed, they must always be in the array | ||
636 | ** part of the registry. | ||
637 | */ | ||
638 | #define getGtable(L) \ | ||
639 | (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) | ||
640 | |||
641 | |||
632 | LUA_API int lua_getglobal (lua_State *L, const char *name) { | 642 | LUA_API int lua_getglobal (lua_State *L, const char *name) { |
633 | Table *reg; | 643 | const TValue *G; |
634 | lua_lock(L); | 644 | lua_lock(L); |
635 | reg = hvalue(&G(L)->l_registry); | 645 | G = getGtable(L); |
636 | return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); | 646 | return auxgetstr(L, G, name); |
637 | } | 647 | } |
638 | 648 | ||
639 | 649 | ||
@@ -811,10 +821,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { | |||
811 | 821 | ||
812 | 822 | ||
813 | LUA_API void lua_setglobal (lua_State *L, const char *name) { | 823 | LUA_API void lua_setglobal (lua_State *L, const char *name) { |
814 | Table *reg; | 824 | const TValue *G; |
815 | lua_lock(L); /* unlock done in 'auxsetstr' */ | 825 | lua_lock(L); /* unlock done in 'auxsetstr' */ |
816 | reg = hvalue(&G(L)->l_registry); | 826 | G = getGtable(L); |
817 | auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); | 827 | auxsetstr(L, G, name); |
818 | } | 828 | } |
819 | 829 | ||
820 | 830 | ||
@@ -1063,8 +1073,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, | |||
1063 | LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ | 1073 | LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ |
1064 | if (f->nupvalues >= 1) { /* does it have an upvalue? */ | 1074 | if (f->nupvalues >= 1) { /* does it have an upvalue? */ |
1065 | /* get global table from registry */ | 1075 | /* get global table from registry */ |
1066 | Table *reg = hvalue(&G(L)->l_registry); | 1076 | const TValue *gt = getGtable(L); |
1067 | const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); | ||
1068 | /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ | 1077 | /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ |
1069 | setobj(L, f->upvals[0]->v, gt); | 1078 | setobj(L, f->upvals[0]->v, gt); |
1070 | luaC_barrier(L, f->upvals[0], gt); | 1079 | luaC_barrier(L, f->upvals[0], gt); |
@@ -213,17 +213,14 @@ static void freestack (lua_State *L) { | |||
213 | ** Create registry table and its predefined values | 213 | ** Create registry table and its predefined values |
214 | */ | 214 | */ |
215 | static void init_registry (lua_State *L, global_State *g) { | 215 | static void init_registry (lua_State *L, global_State *g) { |
216 | TValue temp; | ||
217 | /* create registry */ | 216 | /* create registry */ |
218 | Table *registry = luaH_new(L); | 217 | Table *registry = luaH_new(L); |
219 | sethvalue(L, &g->l_registry, registry); | 218 | sethvalue(L, &g->l_registry, registry); |
220 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); | 219 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); |
221 | /* registry[LUA_RIDX_MAINTHREAD] = L */ | 220 | /* registry[LUA_RIDX_MAINTHREAD] = L */ |
222 | setthvalue(L, &temp, L); /* temp = L */ | 221 | setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L); |
223 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); | 222 | /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ |
224 | /* registry[LUA_RIDX_GLOBALS] = table of globals */ | 223 | sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); |
225 | sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ | ||
226 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); | ||
227 | } | 224 | } |
228 | 225 | ||
229 | 226 | ||