浏览代码

code for string cache generalized for "associative sets" (compiler
will optimize away or inline the extra loops)

Roberto Ierusalimschy 10 年之前
父节点
当前提交
9fae7b6d3f
共有 4 个文件被更改,包括 36 次插入23 次删除
  1. 7 4
      llimits.h
  2. 2 2
      lstate.h
  3. 22 16
      lstring.c
  4. 5 1
      ltests.h

+ 7 - 4
llimits.h

@@ -1,5 +1,5 @@
 /*
-** $Id: llimits.h,v 1.136 2015/07/15 15:57:13 roberto Exp roberto $
+** $Id: llimits.h,v 1.137 2015/09/08 16:53:56 roberto Exp roberto $
 ** Limits, basic types, and some other 'installation-dependent' definitions
 ** See Copyright Notice in lua.h
 */
@@ -190,10 +190,13 @@ typedef unsigned long Instruction;
 
 
 /*
-** Size of cache for strings in the API (better be a prime)
+** Size of cache for strings in the API. 'N' is the number of
+** sets (better be a prime) and "M" is the size of each set (M == 1
+** makes a direct cache.)
 */
-#if !defined(STRCACHE_SIZE)
-#define STRCACHE_SIZE		127
+#if !defined(STRCACHE_N)
+#define STRCACHE_N		63
+#define STRCACHE_M		2
 #endif
 
 

+ 2 - 2
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 2.123 2015/07/04 16:33:17 roberto Exp roberto $
+** $Id: lstate.h,v 2.124 2015/09/08 15:41:05 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -140,7 +140,7 @@ typedef struct global_State {
   TString *memerrmsg;  /* memory-error message */
   TString *tmname[TM_N];  /* array with tag-method names */
   struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */
-  TString *strcache[STRCACHE_SIZE][1];  /* cache for strings in API */
+  TString *strcache[STRCACHE_N][STRCACHE_M];  /* cache for strings in API */
 } global_State;
 
 

+ 22 - 16
lstring.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 2.51 2015/09/08 15:41:05 roberto Exp roberto $
+** $Id: lstring.c,v 2.52 2015/09/17 15:51:05 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -92,11 +92,12 @@ void luaS_resize (lua_State *L, int newsize) {
 ** a non-collectable string.)
 */
 void luaS_clearcache (global_State *g) {
-  int i;
-  for (i = 0; i < STRCACHE_SIZE; i++) {
-    if (iswhite(g->strcache[i][0]))  /* will entry be collected? */
-      g->strcache[i][0] = g->memerrmsg;  /* replace it with something fixed */
-  }
+  int i, j;
+  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 */
+    }
 }
 
 
@@ -105,13 +106,14 @@ void luaS_clearcache (global_State *g) {
 */
 void luaS_init (lua_State *L) {
   global_State *g = G(L);
-  int i;
+  int i, j;
   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 */
-  for (i = 0; i < STRCACHE_SIZE; i++)  /* fill cache with valid strings */
-    g->strcache[i][0] = g->memerrmsg;
+  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;
 }
 
 
@@ -205,15 +207,19 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
 ** check hits.
 */
 TString *luaS_new (lua_State *L, const char *str) {
-  unsigned int i = point2uint(str) % STRCACHE_SIZE;  /* hash */
+  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
+  int j;
   TString **p = G(L)->strcache[i];
-  if (strcmp(str, getstr(p[0])) == 0)  /* hit? */
-    return p[0];  /* that it is */
-  else {  /* normal route */
-    TString *s = luaS_newlstr(L, str, strlen(str));
-    p[0] = s;
-    return s;
+  for (j = 0; j < STRCACHE_M; j++) {
+    if (strcmp(str, getstr(p[j])) == 0)  /* hit? */
+      return p[j];  /* that is it */
   }
+  /* normal route */
+  for (j = STRCACHE_M - 1; j > 0; j--)
+    p[j] = p[j - 1];  /* move out last element */
+  /* new element is first in the list */
+  p[0] = luaS_newlstr(L, str, strlen(str));
+  return p[0];
 }
 
 

+ 5 - 1
ltests.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.h,v 2.47 2014/12/26 14:44:44 roberto Exp roberto $
+** $Id: ltests.h,v 2.48 2015/06/18 14:27:44 roberto Exp roberto $
 ** Internal Header for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -118,5 +118,9 @@ LUA_API void *debug_realloc (void *ud, void *block,
 #undef LUAI_USER_ALIGNMENT_T
 #define LUAI_USER_ALIGNMENT_T   union { char b[sizeof(void*) * 8]; }
 
+
+#define STRCACHE_N	23
+#define STRCACHE_M	5
+
 #endif