mirror of
https://github.com/lua/lua.git
synced 2025-01-14 05:43:00 +08:00
small bug: nuse may change when table is rehashed;
3/2 is a good fraction for hash limit (instead of 0.7, using floats)
This commit is contained in:
parent
1d420c2c11
commit
8e226e6a09
21
ltable.c
21
ltable.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.c,v 1.13 1998/07/12 16:15:19 roberto Exp roberto $
|
** $Id: ltable.c,v 1.14 1998/08/10 21:36:32 roberto Exp roberto $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -20,8 +20,6 @@
|
|||||||
#define nodevector(t) ((t)->node)
|
#define nodevector(t) ((t)->node)
|
||||||
|
|
||||||
|
|
||||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
|
||||||
|
|
||||||
#define TagDefault LUA_T_ARRAY;
|
#define TagDefault LUA_T_ARRAY;
|
||||||
|
|
||||||
|
|
||||||
@ -107,10 +105,9 @@ void luaH_free (Hash *frees)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Hash *luaH_new (int nhash)
|
Hash *luaH_new (int nhash) {
|
||||||
{
|
|
||||||
Hash *t = luaM_new(Hash);
|
Hash *t = luaM_new(Hash);
|
||||||
nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
|
nhash = luaO_redimension(nhash*3/2);
|
||||||
nodevector(t) = hashnodecreate(nhash);
|
nodevector(t) = hashnodecreate(nhash);
|
||||||
nhash(t) = nhash;
|
nhash(t) = nhash;
|
||||||
nuse(t) = 0;
|
nuse(t) = 0;
|
||||||
@ -133,18 +130,20 @@ static int newsize (Hash *t) {
|
|||||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rehash (Hash *t)
|
static void rehash (Hash *t) {
|
||||||
{
|
|
||||||
int nold = nhash(t);
|
int nold = nhash(t);
|
||||||
Node *vold = nodevector(t);
|
Node *vold = nodevector(t);
|
||||||
int nnew = newsize(t);
|
int nnew = newsize(t);
|
||||||
int i;
|
int i;
|
||||||
nodevector(t) = hashnodecreate(nnew);
|
nodevector(t) = hashnodecreate(nnew);
|
||||||
nhash(t) = nnew;
|
nhash(t) = nnew;
|
||||||
|
nuse(t) = 0;
|
||||||
for (i=0; i<nold; i++) {
|
for (i=0; i<nold; i++) {
|
||||||
Node *n = vold+i;
|
Node *n = vold+i;
|
||||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
|
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
|
||||||
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
|
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
|
||||||
|
nuse(t)++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
L->nblocks += gcsize(nnew)-gcsize(nold);
|
L->nblocks += gcsize(nnew)-gcsize(nold);
|
||||||
luaM_free(vold);
|
luaM_free(vold);
|
||||||
@ -170,11 +169,11 @@ TObject *luaH_set (Hash *t, TObject *ref)
|
|||||||
{
|
{
|
||||||
Node *n = node(t, present(t, ref));
|
Node *n = node(t, present(t, ref));
|
||||||
if (ttype(ref(n)) == LUA_T_NIL) {
|
if (ttype(ref(n)) == LUA_T_NIL) {
|
||||||
nuse(t)++;
|
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
||||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
|
|
||||||
rehash(t);
|
rehash(t);
|
||||||
n = node(t, present(t, ref));
|
n = node(t, present(t, ref));
|
||||||
}
|
}
|
||||||
|
nuse(t)++;
|
||||||
*ref(n) = *ref;
|
*ref(n) = *ref;
|
||||||
ttype(val(n)) = LUA_T_NIL;
|
ttype(val(n)) = LUA_T_NIL;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user