lua-users home
lua-l archive

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


I have had a look at lstring.c and I have few questions that I will write in a separate email

For the cache, I noticed that last element is moved out if there is the need to insert something new. The loop to move out last element can be merged with the loop to search. If this is done move-to-front can be implemented so that subsequent search can be faster.

I have never used GitHub and do not know how to contribute however this is the implementation that I would suggest (I have done a quick test by isolating and compiling just this and seems to work fine).

/* same as before but with move-to-front */
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 *pprev = p[0]; /* does not matter */
  TString *ptest;
  for(j = 0; j < STRCACHE_M; j++) {
    ptest = p[j];
    p[j] = pprev; /* copy previous element, move out last element */
    if (strcmp(str, getstr(ptest)) == 0) { /* hit? */  
      p[0] = ptest; /* hit is now first in the list */
      return p[0];
    }
    pprev = ptest;
  }

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

--
Andrea Vitali