浏览代码

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)

Roberto Ierusalimschy 8 年之前
父节点
当前提交
6d998055c8
共有 5 个文件被更改,包括 24 次插入16 次删除
  1. 3 2
      ldo.c
  2. 2 2
      lstate.h
  3. 8 8
      lstring.c
  4. 8 1
      lstring.h
  5. 3 3
      ltm.c

+ 3 - 2
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 2.160 2017/05/23 12:50:11 roberto Exp roberto $
+** $Id: ldo.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -91,7 +91,8 @@ struct lua_longjmp {
 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
   switch (errcode) {
     case LUA_ERRMEM: {  /* memory error? */
-      setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
+      TString *memerrmsg = luaS_newliteral(L, MEMERRMSG);
+      setsvalue2s(L, oldtop, memerrmsg); /* reuse preregistered msg. */
       break;
     }
     case LUA_ERRERR: {

+ 2 - 2
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 2.142 2017/05/26 19:14:29 roberto Exp roberto $
+** $Id: lstate.h,v 2.143 2017/06/12 14:21:44 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -175,7 +175,7 @@ typedef struct global_State {
   lua_CFunction panic;  /* to be called in unprotected errors */
   struct lua_State *mainthread;
   const lua_Number *version;  /* pointer to version number */
-  TString *memerrmsg;  /* memory-error message */
+  TString *nfield;  /* string "n" (key in vararg tables) */
   TString *tmname[TM_N];  /* array with tag-method names */
   struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */
   TString *strcache[STRCACHE_N][STRCACHE_M];  /* cache for strings in API */

+ 8 - 8
lstring.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 2.55 2015/11/03 15:36:01 roberto Exp roberto $
+** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -22,9 +22,6 @@
 #include "lstring.h"
 
 
-#define MEMERRMSG       "not enough memory"
-
-
 /*
 ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
 ** compute its hash
@@ -105,7 +102,7 @@ void luaS_clearcache (global_State *g) {
   for (i = 0; i < STRCACHE_N; i++)
     for (j = 0; j < STRCACHE_M; j++) {
     if (iswhite(g->strcache[i][j]))  /* will entry be collected? */
-      g->strcache[i][j] = g->memerrmsg;  /* replace it with something fixed */
+      g->strcache[i][j] = g->nfield;  /* replace it with something fixed */
     }
 }
 
@@ -116,13 +113,16 @@ void luaS_clearcache (global_State *g) {
 void luaS_init (lua_State *L) {
   global_State *g = G(L);
   int i, j;
+  TString *memerrmsg;
   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
   /* pre-create memory-error message */
-  g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
-  luaC_fix(L, obj2gco(g->memerrmsg));  /* it should never be collected */
+  memerrmsg = luaS_newliteral(L, MEMERRMSG);
+  luaC_fix(L, obj2gco(memerrmsg));  /* it should never be collected */
+  g->nfield = luaS_newliteral(L, "n");  /* pre-create "n" field name */
+  luaC_fix(L, obj2gco(g->nfield));  /* it also should never be collected */
   for (i = 0; i < STRCACHE_N; i++)  /* fill cache with valid strings */
     for (j = 0; j < STRCACHE_M; j++)
-      g->strcache[i][j] = g->memerrmsg;
+      g->strcache[i][j] = g->nfield;
 }
 
 

+ 8 - 1
lstring.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.h,v 1.60 2015/09/08 15:41:05 roberto Exp roberto $
+** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp roberto $
 ** String table (keep all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -12,6 +12,13 @@
 #include "lstate.h"
 
 
+/*
+** Memory-allocation error message must be preallocated (it cannot
+** be created after memory is exausted)
+*/
+#define MEMERRMSG       "not enough memory"
+
+
 #define sizelstring(l)  (sizeof(union UTString) + ((l) + 1) * sizeof(char))
 
 #define sizeludata(l)	(sizeof(union UUdata) + (l))

+ 3 - 3
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 2.41 2017/05/13 12:57:20 roberto Exp roberto $
+** $Id: ltm.c,v 2.42 2017/06/29 15:06:44 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -188,7 +188,7 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) {
   luaH_resize(L, vtab, actual, 1);
   for (i = 0; i < actual; i++)  /* put extra arguments into vararg table */
     setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1));
-  setsvalue(L, &nname, luaS_newliteral(L, "n"));  /* get field 'n' */
+  setsvalue(L, &nname, G(L)->nfield);  /* get field 'n' */
   setivalue(luaH_set(L, vtab, &nname), actual);  /* store counter there */
   L->top -= actual;  /* remove extra elements from the stack */
   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) {
     int i;
     Table *h = hvalue(t);
     if (wanted < 0) {  /* get all? */
-      const TValue *ns = luaH_getstr(h, luaS_newliteral(L, "n"));
+      const TValue *ns = luaH_getstr(h, G(L)->nfield);
       int n = (ttisinteger(ns)) ? ivalue(ns) : 0;
       wanted = n;
       checkstackp(L, n, where);