diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-07-27 10:50:16 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-07-27 10:50:16 -0300 |
commit | 6d998055c80a0fe8d4698ba02becb1813203eee9 (patch) | |
tree | 7f39b89a0d383bc8a96708c5b1cc36a036099a2a | |
parent | 11769b203f052f7423575c1a66901cb9eb277206 (diff) | |
download | lua-6d998055c80a0fe8d4698ba02becb1813203eee9.tar.gz lua-6d998055c80a0fe8d4698ba02becb1813203eee9.tar.bz2 lua-6d998055c80a0fe8d4698ba02becb1813203eee9.zip |
no more reference 'memerrmsg' + new reference to "n"
(both can be retrieved by 'luaS_newliteral' without creating anything,
because they are fixed, but "n" deserves fast access while 'memerrmsg'
does not)
-rw-r--r-- | ldo.c | 5 | ||||
-rw-r--r-- | lstate.h | 4 | ||||
-rw-r--r-- | lstring.c | 16 | ||||
-rw-r--r-- | lstring.h | 9 | ||||
-rw-r--r-- | ltm.c | 6 |
5 files changed, 24 insertions, 16 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 2.160 2017/05/23 12:50:11 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -91,7 +91,8 @@ struct lua_longjmp { | |||
91 | static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { | 91 | static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { |
92 | switch (errcode) { | 92 | switch (errcode) { |
93 | case LUA_ERRMEM: { /* memory error? */ | 93 | case LUA_ERRMEM: { /* memory error? */ |
94 | setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ | 94 | TString *memerrmsg = luaS_newliteral(L, MEMERRMSG); |
95 | setsvalue2s(L, oldtop, memerrmsg); /* reuse preregistered msg. */ | ||
95 | break; | 96 | break; |
96 | } | 97 | } |
97 | case LUA_ERRERR: { | 98 | case LUA_ERRERR: { |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.h,v 2.142 2017/05/26 19:14:29 roberto Exp roberto $ | 2 | ** $Id: lstate.h,v 2.143 2017/06/12 14:21:44 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -175,7 +175,7 @@ typedef struct global_State { | |||
175 | lua_CFunction panic; /* to be called in unprotected errors */ | 175 | lua_CFunction panic; /* to be called in unprotected errors */ |
176 | struct lua_State *mainthread; | 176 | struct lua_State *mainthread; |
177 | const lua_Number *version; /* pointer to version number */ | 177 | const lua_Number *version; /* pointer to version number */ |
178 | TString *memerrmsg; /* memory-error message */ | 178 | TString *nfield; /* string "n" (key in vararg tables) */ |
179 | TString *tmname[TM_N]; /* array with tag-method names */ | 179 | TString *tmname[TM_N]; /* array with tag-method names */ |
180 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ | 180 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ |
181 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ | 181 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 2.55 2015/11/03 15:36:01 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -22,9 +22,6 @@ | |||
22 | #include "lstring.h" | 22 | #include "lstring.h" |
23 | 23 | ||
24 | 24 | ||
25 | #define MEMERRMSG "not enough memory" | ||
26 | |||
27 | |||
28 | /* | 25 | /* |
29 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to | 26 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to |
30 | ** compute its hash | 27 | ** compute its hash |
@@ -105,7 +102,7 @@ void luaS_clearcache (global_State *g) { | |||
105 | for (i = 0; i < STRCACHE_N; i++) | 102 | for (i = 0; i < STRCACHE_N; i++) |
106 | for (j = 0; j < STRCACHE_M; j++) { | 103 | for (j = 0; j < STRCACHE_M; j++) { |
107 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ | 104 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */ |
108 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ | 105 | g->strcache[i][j] = g->nfield; /* replace it with something fixed */ |
109 | } | 106 | } |
110 | } | 107 | } |
111 | 108 | ||
@@ -116,13 +113,16 @@ void luaS_clearcache (global_State *g) { | |||
116 | void luaS_init (lua_State *L) { | 113 | void luaS_init (lua_State *L) { |
117 | global_State *g = G(L); | 114 | global_State *g = G(L); |
118 | int i, j; | 115 | int i, j; |
116 | TString *memerrmsg; | ||
119 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | 117 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
120 | /* pre-create memory-error message */ | 118 | /* pre-create memory-error message */ |
121 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); | 119 | memerrmsg = luaS_newliteral(L, MEMERRMSG); |
122 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ | 120 | luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */ |
121 | g->nfield = luaS_newliteral(L, "n"); /* pre-create "n" field name */ | ||
122 | luaC_fix(L, obj2gco(g->nfield)); /* it also should never be collected */ | ||
123 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ | 123 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ |
124 | for (j = 0; j < STRCACHE_M; j++) | 124 | for (j = 0; j < STRCACHE_M; j++) |
125 | g->strcache[i][j] = g->memerrmsg; | 125 | g->strcache[i][j] = g->nfield; |
126 | } | 126 | } |
127 | 127 | ||
128 | 128 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.h,v 1.60 2015/09/08 15:41:05 roberto Exp roberto $ | 2 | ** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp roberto $ |
3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -12,6 +12,13 @@ | |||
12 | #include "lstate.h" | 12 | #include "lstate.h" |
13 | 13 | ||
14 | 14 | ||
15 | /* | ||
16 | ** Memory-allocation error message must be preallocated (it cannot | ||
17 | ** be created after memory is exausted) | ||
18 | */ | ||
19 | #define MEMERRMSG "not enough memory" | ||
20 | |||
21 | |||
15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) | 22 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) |
16 | 23 | ||
17 | #define sizeludata(l) (sizeof(union UUdata) + (l)) | 24 | #define sizeludata(l) (sizeof(union UUdata) + (l)) |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltm.c,v 2.41 2017/05/13 12:57:20 roberto Exp roberto $ | 2 | ** $Id: ltm.c,v 2.42 2017/06/29 15:06:44 roberto Exp roberto $ |
3 | ** Tag methods | 3 | ** Tag methods |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -188,7 +188,7 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { | |||
188 | luaH_resize(L, vtab, actual, 1); | 188 | luaH_resize(L, vtab, actual, 1); |
189 | for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ | 189 | for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ |
190 | setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1)); | 190 | setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1)); |
191 | setsvalue(L, &nname, luaS_newliteral(L, "n")); /* get field 'n' */ | 191 | setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */ |
192 | setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ | 192 | setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ |
193 | L->top -= actual; /* remove extra elements from the stack */ | 193 | L->top -= actual; /* remove extra elements from the stack */ |
194 | sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ | 194 | sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ |
@@ -202,7 +202,7 @@ void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) { | |||
202 | int i; | 202 | int i; |
203 | Table *h = hvalue(t); | 203 | Table *h = hvalue(t); |
204 | if (wanted < 0) { /* get all? */ | 204 | if (wanted < 0) { /* get all? */ |
205 | const TValue *ns = luaH_getstr(h, luaS_newliteral(L, "n")); | 205 | const TValue *ns = luaH_getstr(h, G(L)->nfield); |
206 | int n = (ttisinteger(ns)) ? ivalue(ns) : 0; | 206 | int n = (ttisinteger(ns)) ? ivalue(ns) : 0; |
207 | wanted = n; | 207 | wanted = n; |
208 | checkstackp(L, n, where); | 208 | checkstackp(L, n, where); |