lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


in reference with my previous email, I found a cleaner and more compact implementation to have the move-to-front effect when looking into the string cache

TString *luaS_new(lua_State *L, const char *str) {
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
  int j;
  TString **p = G(L)->strcache[i];
  TString *ptemp;
  for(j = 1; j < STRCACHE_M; j++) {
    if (strcmp(str, getstr(p[0])) == 0) return p[0]; /* hit? */    
    ptemp = p[0]; p[0] = p[j]; p[j] = ptemp; /* swap */
  }

  /* new element is first in the list */
  p[0] = luaS_newlstr(L, str, strlen(str));
  return p[0];  
}


the small improvement can probably be seen only if STRCACHE_M is larger than 2 (today it is set to 2 in the code I can see)

   Andrea



--
Andrea Vitali