Browse Source

table sizes don't need to be primes; power of 2 gives the same performance.

Roberto Ierusalimschy 26 years ago
parent
commit
c5fa0895e9
1 changed files with 6 additions and 15 deletions
  1. 6 15
      lmem.c

+ 6 - 15
lmem.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lmem.c,v 1.19 1999/10/19 13:33:22 roberto Exp roberto $
+** $Id: lmem.c,v 1.20 1999/11/22 13:12:07 roberto Exp roberto $
 ** Interface to Memory Manager
 ** Interface to Memory Manager
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -10,6 +10,7 @@
 #define LUA_REENTRANT
 #define LUA_REENTRANT
 
 
 #include "lmem.h"
 #include "lmem.h"
+#include "lobject.h"
 #include "lstate.h"
 #include "lstate.h"
 #include "lua.h"
 #include "lua.h"
 
 
@@ -24,27 +25,17 @@
 #endif
 #endif
 
 
 
 
-#define MINSIZE	8	/* minimum size for "growing" vectors */
 
 
 
 
-
-
-static unsigned long power2 (unsigned long n) {
-  unsigned long p = MINSIZE;
-  while (p<=n) p<<=1;
-  return p;
-}
-
-
-void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size,
-                       const char *errormsg, unsigned long limit) {
+void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
+               int inc, int size, const char *errormsg, unsigned long limit) {
   unsigned long newn = nelems+inc;
   unsigned long newn = nelems+inc;
   if (newn >= limit) lua_error(L, errormsg);
   if (newn >= limit) lua_error(L, errormsg);
   if ((newn ^ nelems) <= nelems ||  /* still the same power of 2 limit? */
   if ((newn ^ nelems) <= nelems ||  /* still the same power of 2 limit? */
-       (nelems > 0 && newn < MINSIZE))  /* or block already is MINSIZE? */
+       (nelems > 0 && newn < MINPOWER2))  /* or block already is MINPOWER2? */
       return block;  /* do not need to reallocate */
       return block;  /* do not need to reallocate */
   else  /* it crossed a power of 2 boundary; grow to next power */
   else  /* it crossed a power of 2 boundary; grow to next power */
-    return luaM_realloc(L, block, power2(newn)*size);
+    return luaM_realloc(L, block, luaO_power2(newn)*size);
 }
 }