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 /lapi.c | |
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.
Diffstat (limited to '')
-rw-r--r-- | lapi.c | 25 |
1 files changed, 17 insertions, 8 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); |