Skip to content

Commit 7091c94

Browse files
committed
FIX: not using linear search for maps of small size as it was causing crashes in rare cases.
1 parent 8c9b3ab commit 7091c94

File tree

1 file changed

+4
-61
lines changed

1 file changed

+4
-61
lines changed

src/core/t-map.c

+4-61
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353

5454
#include "sys-core.h"
5555

56-
#define MIN_DICT 8 // size to switch to hashing
57-
5856

5957
/***********************************************************************
6058
**
@@ -74,14 +72,14 @@
7472
/*
7573
** Makes a MAP block (that holds both keys and values).
7674
** Size is the number of key-value pairs.
77-
** If size >= MIN_DICT, then a hash series is also created.
75+
** Hash series is also created.
7876
**
7977
***********************************************************************/
8078
{
8179
REBSER *blk = Make_Block(size*2);
8280
REBSER *ser = 0;
8381

84-
if (size >= MIN_DICT) ser = Make_Hash_Array(size);
82+
ser = Make_Hash_Array(size);
8583

8684
blk->series = ser;
8785

@@ -210,62 +208,7 @@
210208
REBVAL *v;
211209
REBCNT n;
212210

213-
if (IS_NONE(key)) return 0;
214-
215-
// We may not be large enough yet for the hash table to
216-
// be worthwhile, so just do a linear search:
217-
if (!hser) {
218-
if (series->tail < MIN_DICT*2) {
219-
v = BLK_HEAD(series);
220-
if (ANY_WORD(key)) {
221-
for (n = 0; n < series->tail; n += 2, v += 2) {
222-
if (ANY_WORD(v) && SAME_SYM(key, v)) {
223-
if (val) *++v = *val;
224-
return n/2+1;
225-
}
226-
}
227-
}
228-
else if (ANY_BINSTR(key)) {
229-
for (n = 0; n < series->tail; n += 2, v += 2) {
230-
if (VAL_TYPE(key) == VAL_TYPE(v) && 0 == Compare_String_Vals(key, v, (REBOOL)!IS_BINARY(v))) {
231-
if (val) {
232-
*++v = *val;
233-
// VAL_SERIES(v) = Copy_Series_Value(val);
234-
// VAL_INDEX(v) = 0;
235-
}
236-
return n/2+1;
237-
}
238-
}
239-
}
240-
else if (IS_INTEGER(key)) {
241-
for (n = 0; n < series->tail; n += 2, v += 2) {
242-
if (IS_INTEGER(v) && VAL_INT64(key) == VAL_INT64(v)) {
243-
if (val) *++v = *val;
244-
return n/2+1;
245-
}
246-
}
247-
}
248-
else if (IS_CHAR(key)) {
249-
for (n = 0; n < series->tail; n += 2, v += 2) {
250-
if (IS_CHAR(v) && VAL_CHAR(key) == VAL_CHAR(v)) {
251-
if (val) *++v = *val;
252-
return n/2+1;
253-
}
254-
}
255-
}
256-
else Trap_Type(key);
257-
258-
if (!val) return 0;
259-
Append_Val(series, key);
260-
Append_Val(series, val); // no Copy_Series_Value(val) on strings
261-
return series->tail/2;
262-
}
263-
264-
// Add hash table:
265-
//Print("hash added %d", series->tail);
266-
series->series = hser = Make_Hash_Array(series->tail);
267-
Rehash_Hash(series);
268-
}
211+
if (IS_NONE(key) || hser == NULL) return 0;
269212

270213
// Get hash table, expand it if needed:
271214
if (series->tail > hser->tail/2) {
@@ -427,7 +370,7 @@
427370
REBSER *ser = 0;
428371
REBCNT size = SERIES_TAIL(blk);
429372

430-
if (size >= MIN_DICT) ser = Make_Hash_Array(size);
373+
ser = Make_Hash_Array(size);
431374
blk->series = ser;
432375
Rehash_Hash(blk);
433376
}

0 commit comments

Comments
 (0)