Skip to content

Commit 9a90e20

Browse files
committed
FEAT: improved hashing
2 parents 185f453 + 0111789 commit 9a90e20

13 files changed

+572
-289
lines changed

make/rebol3.nest

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ core-files: [
182182
%core/n-data.c
183183
; %core/n-draw.c ;old source
184184
; %core/n-graphics.c ;old source
185+
%core/n-hash.c
185186
; %core/n-image.c ;optional, use: include-image-natives
186187
%core/n-io.c
187188
%core/n-loop.c
@@ -396,7 +397,7 @@ boot-host-files: [] ; may be used for custom boot script
396397
config: [ ;- this is list of configuration (optional) defines
397398
INCLUDE_MBEDTLS ;- replaced original checksum implementations
398399
COLOR_CONSOLE ;- use ANSI escape sequences in prompt and results
399-
;DEBUG_HASH_COLLISIONS
400+
DEBUG_HASH_COLLISIONS
400401

401402
;*** Unfinished features **************************************************/
402403
;INCLUDE_TASK ;- tasks are not implemented yet, so include it only on demand

src/core/c-word.c

+21-29
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@
142142
{
143143
REBCNT *hashes;
144144
REBVAL *word;
145-
REBINT hash;
145+
REBCNT key;
146+
REBCNT hash=0;
146147
REBCNT size;
147-
REBINT skip;
148-
REBCNT n;
148+
REBCNT n, i;
149149

150150
// Allocate a new hash table:
151151
Expand_Hash(PG_Word_Table.hashes);
@@ -156,13 +156,10 @@
156156
hashes = (REBCNT *)PG_Word_Table.hashes->data;
157157
size = PG_Word_Table.hashes->tail;
158158
for (n = 1; n < PG_Word_Table.series->tail; n++, word++) {
159-
hash = Hash_Word(VAL_SYM_NAME(word), -1);
160-
skip = (hash & 0x0000FFFF) % size;
161-
if (skip == 0) skip = 1;
162-
hash = (hash & 0x00FFFF00) % size;
163-
while (hashes[hash]) {
164-
hash += skip;
165-
if (hash >= (REBINT)size) hash -= size;
159+
key = CRC_Word(VAL_SYM_NAME(word), UNKNOWN);
160+
for (i = 0; i < size; i++) {
161+
hash = Hash_Probe(key, i, size);
162+
if (!hashes[hash]) break;
166163
}
167164
hashes[hash] = n;
168165
}
@@ -196,11 +193,11 @@
196193
**
197194
***********************************************************************/
198195
{
199-
REBINT hash;
200-
REBINT size;
201-
REBINT skip;
196+
REBCNT key;
197+
REBCNT hash = 0;
198+
REBLEN size;
202199
REBINT n;
203-
REBCNT h;
200+
REBCNT h=0, i;
204201
REBCNT *hashes;
205202
REBVAL *words;
206203
REBVAL *w;
@@ -225,28 +222,22 @@
225222
CLEAR_SERIES(Bind_Table);
226223
}
227224

228-
size = (REBINT)PG_Word_Table.hashes->tail;
225+
size = PG_Word_Table.hashes->tail;
229226
words = BLK_HEAD(PG_Word_Table.series);
230227
hashes = (REBCNT *)PG_Word_Table.hashes->data;
231228

232229
// Hash the word, including a skip factor for lookup:
233-
hash = Hash_Word(str, len);
234-
skip = (hash & 0x0000FFFF) % size;
235-
if (skip == 0) skip = 1;
236-
hash = (hash & 0x00FFFF00) % size;
237-
//Debug_Fmt("%s hash %d skip %d", str, hash, skip);
238-
230+
key = CRC_Word(str, len);
239231
// Search hash table for word match:
240-
while (NZ(h = hashes[hash])) {
241-
while ((n = Compare_UTF8(VAL_SYM_NAME(words+h), str, len)) >= 0) {
242-
//if (Match_String("script", str, len))
243-
// Debug_Fmt("---- %s %d %d\n", VAL_SYM_NAME(&words[h]), n, h);
232+
for (i = 0; i < size; i++) {
233+
hash = Hash_Probe(key, i, size);
234+
h = hashes[hash];
235+
if (!h) break;
236+
while ((n = Compare_UTF8(VAL_SYM_NAME(words + h), str, len)) >= 0) {
244237
if (n == 0) return h; // direct hit
245-
if (VAL_SYM_ALIAS(words+h)) h = VAL_SYM_ALIAS(words+h);
238+
if (VAL_SYM_ALIAS(words + h)) h = VAL_SYM_ALIAS(words + h);
246239
else goto make_sym; // Create new alias for word
247240
}
248-
hash += skip;
249-
if (hash >= size) hash -= size;
250241
}
251242

252243
make_sym:
@@ -337,7 +328,8 @@
337328
/*
338329
***********************************************************************/
339330
{
340-
if (num == 0 || num >= PG_Word_Table.series->tail) return (REBYTE*)"???";
331+
if (num == 0 || num >= PG_Word_Table.series->tail)
332+
return (REBYTE*)"???";
341333
return VAL_SYM_NAME(BLK_SKIP(PG_Word_Table.series, num));
342334
}
343335

0 commit comments

Comments
 (0)