summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lobject.h5
-rw-r--r--lstate.c4
-rw-r--r--lstate.h3
-rw-r--r--lstring.c121
-rw-r--r--ltests.c20
5 files changed, 61 insertions, 92 deletions
diff --git a/lobject.h b/lobject.h
index e9a20915..f33eadc1 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 2.80 2013/08/18 16:12:18 roberto Exp roberto $ 2** $Id: lobject.h,v 2.81 2013/08/27 18:53:35 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -310,8 +310,9 @@ typedef union TString {
310 struct { 310 struct {
311 CommonHeader; 311 CommonHeader;
312 lu_byte extra; /* reserved words for short strings; "has hash" for longs */ 312 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
313 unsigned int hash;
314 size_t len; /* number of characters in string */ 313 size_t len; /* number of characters in string */
314 union TString *hnext; /* linked list for hash table */
315 unsigned int hash;
315 } tsv; 316 } tsv;
316} TString; 317} TString;
317 318
diff --git a/lstate.c b/lstate.c
index e81d968d..3c79eb6c 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.109 2013/08/30 19:14:26 roberto Exp roberto $ 2** $Id: lstate.c,v 2.110 2013/09/03 15:37:10 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -289,7 +289,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
289 g->gcrunning = 0; /* no GC while building state */ 289 g->gcrunning = 0; /* no GC while building state */
290 g->GCestimate = 0; 290 g->GCestimate = 0;
291 g->GCthreshold = 10000; 291 g->GCthreshold = 10000;
292 g->strt.size = g->strt.nuse = g->strt.empty = 0; 292 g->strt.size = g->strt.nuse = 0;
293 g->strt.hash = NULL; 293 g->strt.hash = NULL;
294 setnilvalue(&g->l_registry); 294 setnilvalue(&g->l_registry);
295 luaZ_initbuffer(L, &g->buff); 295 luaZ_initbuffer(L, &g->buff);
diff --git a/lstate.h b/lstate.h
index 38bbb982..40a23643 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 2.92 2013/08/30 19:14:26 roberto Exp roberto $ 2** $Id: lstate.h,v 2.93 2013/09/03 15:37:10 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -53,7 +53,6 @@ struct lua_longjmp; /* defined in ldo.c */
53typedef struct stringtable { 53typedef struct stringtable {
54 TString **hash; 54 TString **hash;
55 int nuse; /* number of elements */ 55 int nuse; /* number of elements */
56 int empty; /* number of available empty slots */
57 int size; 56 int size;
58} stringtable; 57} stringtable;
59 58
diff --git a/lstring.c b/lstring.c
index c76b6724..4adfbf41 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 2.32 2013/08/27 20:04:00 roberto Exp roberto $ 2** $Id: lstring.c,v 2.33 2013/08/28 18:30:26 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -20,13 +20,6 @@
20#include "lstring.h" 20#include "lstring.h"
21 21
22 22
23/* mark for vacant places in hash table */
24#define VACANTK cast(TString *, cast(size_t, -1))
25
26
27/* second hash (for double hash) */
28#define h2(h1,hash,size) lmod(h1 + ((hash % 61) | 1), size)
29
30 23
31/* 24/*
32** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 25** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
@@ -74,30 +67,32 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
74void luaS_resize (lua_State *L, int newsize) { 67void luaS_resize (lua_State *L, int newsize) {
75 int i; 68 int i;
76 stringtable *tb = &G(L)->strt; 69 stringtable *tb = &G(L)->strt;
77 TString **oldhash = tb->hash; 70 if (newsize > tb->size) { /* grow table if needed */
78 int oldsize = tb->size; 71 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
79 tb->hash = luaM_newvector(L, newsize, TString *); 72 for (i = tb->size; i < newsize; i++)
80 tb->size = newsize; 73 tb->hash[i] = NULL;
81 /* keep load factor below 75% */ 74 }
82 tb->empty = newsize/2 + newsize/4 - tb->nuse; 75 for (i = 0; i < tb->size; i++) { /* rehash */
83 for (i = 0; i < newsize; i++) tb->hash[i] = NULL; 76 TString *p = tb->hash[i];
84 tb->nuse = 0; 77 tb->hash[i] = NULL;
85 /* rehash */ 78 while (p) { /* for each node in the list */
86 for (i = 0; i < oldsize; i++) { 79 TString *hnext = p->tsv.hnext; /* save next */
87 TString *ts = oldhash[i]; 80 unsigned int h = lmod(p->tsv.hash, newsize); /* new position */
88 if (ts != NULL && ts != VACANTK) { 81 p->tsv.hnext = tb->hash[h]; /* chain it */
89 unsigned int hash = ts->tsv.hash; 82 tb->hash[h] = p;
90 int h1 = lmod(hash, tb->size); 83 p = hnext;
91 while (tb->hash[h1] != NULL)
92 h1 = h2(h1, hash, tb->size);
93 tb->hash[h1] = ts;
94 tb->nuse++;
95 } 84 }
96 } 85 }
97 luaM_freearray(L, oldhash, oldsize); 86 if (newsize < tb->size) { /* shrink table if needed */
87 /* vanishing slice should be empty */
88 lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
89 luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
90 }
91 tb->size = newsize;
98} 92}
99 93
100 94
95
101/* 96/*
102** creates a new string object 97** creates a new string object
103*/ 98*/
@@ -116,32 +111,15 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
116} 111}
117 112
118 113
119static void rehash (lua_State *L, stringtable *tb) {
120 int size = tb->size;
121 if (tb->nuse < size / 2) { /* using less than half the size? */
122 if (tb->nuse < size / 4) /* using less than half of that? */
123 size /= 2; /* shrink table */
124 /* else keep size (but reorganize table) */
125 }
126 else { /* table must grow */
127 if (size >= MAX_INT/2) /* avoid arith. overflow */
128 luaD_throw(L, LUA_ERRMEM); /* regular errors need new strings... */
129 size *= 2;
130 }
131 luaS_resize(L, size);
132}
133
134
135LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) { 114LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
136 stringtable *tb = &G(L)->strt; 115 stringtable *tb = &G(L)->strt;
137 unsigned int hash = ts->tsv.hash; 116 TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
138 int h1 = lmod(hash, tb->size); 117 while (*p != ts) /* find previous element */
139 while (tb->hash[h1] != ts) { 118 p = &(*p)->tsv.hnext;
140 lua_assert(tb->hash[h1] != NULL); 119 *p = (*p)->tsv.hnext; /* remove element from its list */
141 h1 = h2(h1, hash, tb->size);
142 }
143 tb->hash[h1] = VACANTK;
144 tb->nuse--; 120 tb->nuse--;
121 if (tb->nuse < tb->size/4)
122 luaS_resize(L, tb->size/2);
145} 123}
146 124
147 125
@@ -150,39 +128,26 @@ LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
150*/ 128*/
151static TString *internshrstr (lua_State *L, const char *str, size_t l) { 129static TString *internshrstr (lua_State *L, const char *str, size_t l) {
152 TString *ts; 130 TString *ts;
153 unsigned int hash = luaS_hash(str, l, G(L)->seed); 131 global_State *g = G(L);
154 stringtable *tb = &G(L)->strt; 132 unsigned int h = luaS_hash(str, l, g->seed);
155 int vacant = -1; 133 TString **list = &g->strt.hash[lmod(h, g->strt.size)];
156 int h1; 134 for (ts = *list; ts != NULL; ts = ts->tsv.hnext) {
157 h1 = lmod(hash, tb->size); /* previous call can changed 'size' */ 135 if (l == ts->tsv.len &&
158 while ((ts = tb->hash[h1]) != NULL) { /* search the string in hash table */ 136 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
159 if (ts == VACANTK) {
160 if (vacant < 0) vacant = h1; /* keep track of first vacant place */
161 }
162 else if (l == ts->tsv.len &&
163 (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
164 /* found! */ 137 /* found! */
165 if (isdead(G(L), obj2gco(ts))) /* dead (but was not collected yet)? */ 138 if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */
166 changewhite(obj2gco(ts)); /* resurrect it */ 139 changewhite(obj2gco(ts)); /* resurrect it */
167 if (vacant >= 0) { /* is there a better place for this string? */ 140 return ts;
168 tb->hash[vacant] = ts; /* move it up the line */
169 tb->hash[h1] = VACANTK;
170 }
171 return ts; /* found */
172 } 141 }
173 h1 = h2(h1, hash, tb->size);
174 } 142 }
175 if (tb->empty <= 0) { /* no more empty spaces? */ 143 if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
176 rehash(L, tb); 144 luaS_resize(L, g->strt.size * 2);
177 return internshrstr(L, str, l); /* recompute insertion with new size */ 145 list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
178 } 146 }
179 ts = createstrobj(L, str, l, LUA_TSHRSTR, hash); 147 ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
180 tb->nuse++; 148 ts->tsv.hnext = *list;
181 if (vacant < 0) /* found no vacant place? */ 149 *list = ts;
182 tb->empty--; /* will have to use the empty place */ 150 g->strt.nuse++;
183 else
184 h1 = vacant; /* use vacant place */
185 tb->hash[h1] = ts;
186 return ts; 151 return ts;
187} 152}
188 153
diff --git a/ltests.c b/ltests.c
index 9edeb515..7d6d3e19 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.153 2013/09/03 15:37:10 roberto Exp roberto $ 2** $Id: ltests.c,v 2.154 2013/09/04 15:34:24 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -730,17 +730,21 @@ static int table_query (lua_State *L) {
730 730
731static int string_query (lua_State *L) { 731static int string_query (lua_State *L) {
732 stringtable *tb = &G(L)->strt; 732 stringtable *tb = &G(L)->strt;
733 int s = luaL_optint(L, 2, 0) - 1; 733 int s = luaL_optint(L, 1, 0) - 1;
734 if (s < 0) { 734 if (s == -1) {
735 lua_pushinteger(L ,tb->nuse);
736 lua_pushinteger(L ,tb->size); 735 lua_pushinteger(L ,tb->size);
736 lua_pushinteger(L ,tb->nuse);
737 return 2; 737 return 2;
738 } 738 }
739 else if (s < tb->size) { 739 else if (s < tb->size) {
740 TString *ts = tb->hash[s]; 740 TString *ts;
741 setsvalue2s(L, L->top, ts); 741 int n = 0;
742 api_incr_top(L); 742 for (ts = tb->hash[s]; ts != NULL; ts = ts->tsv.hnext) {
743 return 1; 743 setsvalue2s(L, L->top, ts);
744 api_incr_top(L);
745 n++;
746 }
747 return n;
744 } 748 }
745 else return 0; 749 else return 0;
746} 750}