diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-12 15:50:51 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-12 15:50:51 -0300 |
commit | d862da6d04111ce7e5b225040fbe7e526761f478 (patch) | |
tree | 5fec8b142e05a5e5c97d61161ad7053fb86a3cdb /lapi.c | |
parent | 7827c40c49d841daca2a40463b8a60f9a113f77e (diff) | |
download | lua-d862da6d04111ce7e5b225040fbe7e526761f478.tar.gz lua-d862da6d04111ce7e5b225040fbe7e526761f478.tar.bz2 lua-d862da6d04111ce7e5b225040fbe7e526761f478.zip |
Optimizations for 'lua_rawgeti' and 'lua_rawseti'
'lua_rawgeti' now uses "fast track"; 'lua_rawseti' still calls
'luaH_setint', but the latter was recoded to avoid extra overhead
when writing to the array part after 'alimit'.
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -102,7 +102,7 @@ static TValue *index2value (lua_State *L, int idx) { | |||
102 | /* | 102 | /* |
103 | ** Convert a valid actual index (not a pseudo-index) to its address. | 103 | ** Convert a valid actual index (not a pseudo-index) to its address. |
104 | */ | 104 | */ |
105 | l_sinline StkId index2stack (lua_State *L, int idx) { | 105 | static StkId index2stack (lua_State *L, int idx) { |
106 | CallInfo *ci = L->ci; | 106 | CallInfo *ci = L->ci; |
107 | if (idx > 0) { | 107 | if (idx > 0) { |
108 | StkId o = ci->func.p + idx; | 108 | StkId o = ci->func.p + idx; |
@@ -234,7 +234,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { | |||
234 | ** Note that we move(copy) only the value inside the stack. | 234 | ** Note that we move(copy) only the value inside the stack. |
235 | ** (We do not move additional fields that may exist.) | 235 | ** (We do not move additional fields that may exist.) |
236 | */ | 236 | */ |
237 | l_sinline void reverse (lua_State *L, StkId from, StkId to) { | 237 | static void reverse (lua_State *L, StkId from, StkId to) { |
238 | for (; from < to; from++, to--) { | 238 | for (; from < to; from++, to--) { |
239 | TValue temp; | 239 | TValue temp; |
240 | setobj(L, &temp, s2v(from)); | 240 | setobj(L, &temp, s2v(from)); |
@@ -664,7 +664,7 @@ LUA_API int lua_pushthread (lua_State *L) { | |||
664 | */ | 664 | */ |
665 | 665 | ||
666 | 666 | ||
667 | l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { | 667 | static int auxgetstr (lua_State *L, const TValue *t, const char *k) { |
668 | int hres; | 668 | int hres; |
669 | TString *str = luaS_new(L, k); | 669 | TString *str = luaS_new(L, k); |
670 | luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres); | 670 | luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres); |
@@ -683,7 +683,9 @@ l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { | |||
683 | 683 | ||
684 | static void getGlobalTable (lua_State *L, TValue *gt) { | 684 | static void getGlobalTable (lua_State *L, TValue *gt) { |
685 | Table *registry = hvalue(&G(L)->l_registry); | 685 | Table *registry = hvalue(&G(L)->l_registry); |
686 | luaH_getint(registry, LUA_RIDX_GLOBALS, gt); | 686 | int hres = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); |
687 | (void)hres; /* avoid warnings (not used) when checks are off */ | ||
688 | api_check(L, hres == HOK, "global table must exist"); | ||
687 | } | 689 | } |
688 | 690 | ||
689 | 691 | ||
@@ -740,7 +742,7 @@ l_sinline int finishrawget (lua_State *L, int hres) { | |||
740 | } | 742 | } |
741 | 743 | ||
742 | 744 | ||
743 | static Table *gettable (lua_State *L, int idx) { | 745 | l_sinline Table *gettable (lua_State *L, int idx) { |
744 | TValue *t = index2value(L, idx); | 746 | TValue *t = index2value(L, idx); |
745 | api_check(L, ttistable(t), "table expected"); | 747 | api_check(L, ttistable(t), "table expected"); |
746 | return hvalue(t); | 748 | return hvalue(t); |
@@ -761,9 +763,11 @@ LUA_API int lua_rawget (lua_State *L, int idx) { | |||
761 | 763 | ||
762 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { | 764 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { |
763 | Table *t; | 765 | Table *t; |
766 | int hres; | ||
764 | lua_lock(L); | 767 | lua_lock(L); |
765 | t = gettable(L, idx); | 768 | t = gettable(L, idx); |
766 | return finishrawget(L, luaH_getint(t, n, s2v(L->top.p))); | 769 | luaH_fastgeti(t, n, s2v(L->top.p), hres); |
770 | return finishrawget(L, hres); | ||
767 | } | 771 | } |
768 | 772 | ||
769 | 773 | ||
@@ -901,9 +905,8 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { | |||
901 | api_checknelems(L, 1); | 905 | api_checknelems(L, 1); |
902 | t = index2value(L, idx); | 906 | t = index2value(L, idx); |
903 | luaV_fastseti(t, n, s2v(L->top.p - 1), hres); | 907 | luaV_fastseti(t, n, s2v(L->top.p - 1), hres); |
904 | if (hres == HOK) { | 908 | if (hres == HOK) |
905 | luaV_finishfastset(L, t, s2v(L->top.p - 1)); | 909 | luaV_finishfastset(L, t, s2v(L->top.p - 1)); |
906 | } | ||
907 | else { | 910 | else { |
908 | TValue temp; | 911 | TValue temp; |
909 | setivalue(&temp, n); | 912 | setivalue(&temp, n); |