108#include "internal/bits.h"
109#include "internal/hash.h"
110#include "internal/sanitizers.h"
121#define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
122#define EXPECT(expr, val) __builtin_expect(expr, val)
123#define ATTRIBUTE_UNUSED __attribute__((unused))
125#define PREFETCH(addr, write_p)
126#define EXPECT(expr, val) (expr)
127#define ATTRIBUTE_UNUSED
131typedef st_index_t st_hash_t;
139#define type_numhash st_hashtype_num
145static int st_strcmp(st_data_t, st_data_t);
146static st_index_t strhash(st_data_t);
152static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
153static st_index_t strcasehash(st_data_t);
155 st_locale_insensitive_strcasecmp_i,
162#define ST_INIT_VAL 0xafafafafafafafaf
163#define ST_INIT_VAL_BYTE 0xafa
170#define malloc ruby_xmalloc
171#define calloc ruby_xcalloc
172#define realloc ruby_xrealloc
173#define free ruby_xfree
176#define EQUAL(tab,x,y) ((x) == (y) || (*(tab)->type->compare)((x),(y)) == 0)
177#define PTR_EQUAL(tab, ptr, hash_val, key_) \
178 ((ptr)->hash == (hash_val) && EQUAL((tab), (key_), (ptr)->key))
182#define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
184 unsigned int _old_rebuilds_num = (tab)->rebuilds_num; \
185 res = PTR_EQUAL(tab, ptr, hash_val, key); \
186 rebuilt_p = _old_rebuilds_num != (tab)->rebuilds_num; \
192 unsigned char entry_power;
196 unsigned char bin_power;
198 unsigned char size_ind;
201 st_index_t bins_words;
205#if SIZEOF_ST_INDEX_T == 8
224 {16, 17, 2, 0x10000},
225 {17, 18, 2, 0x20000},
226 {18, 19, 2, 0x40000},
227 {19, 20, 2, 0x80000},
228 {20, 21, 2, 0x100000},
229 {21, 22, 2, 0x200000},
230 {22, 23, 2, 0x400000},
231 {23, 24, 2, 0x800000},
232 {24, 25, 2, 0x1000000},
233 {25, 26, 2, 0x2000000},
234 {26, 27, 2, 0x4000000},
235 {27, 28, 2, 0x8000000},
236 {28, 29, 2, 0x10000000},
237 {29, 30, 2, 0x20000000},
238 {30, 31, 2, 0x40000000},
239 {31, 32, 2, 0x80000000},
240 {32, 33, 3, 0x200000000},
241 {33, 34, 3, 0x400000000},
242 {34, 35, 3, 0x800000000},
243 {35, 36, 3, 0x1000000000},
244 {36, 37, 3, 0x2000000000},
245 {37, 38, 3, 0x4000000000},
246 {38, 39, 3, 0x8000000000},
247 {39, 40, 3, 0x10000000000},
248 {40, 41, 3, 0x20000000000},
249 {41, 42, 3, 0x40000000000},
250 {42, 43, 3, 0x80000000000},
251 {43, 44, 3, 0x100000000000},
252 {44, 45, 3, 0x200000000000},
253 {45, 46, 3, 0x400000000000},
254 {46, 47, 3, 0x800000000000},
255 {47, 48, 3, 0x1000000000000},
256 {48, 49, 3, 0x2000000000000},
257 {49, 50, 3, 0x4000000000000},
258 {50, 51, 3, 0x8000000000000},
259 {51, 52, 3, 0x10000000000000},
260 {52, 53, 3, 0x20000000000000},
261 {53, 54, 3, 0x40000000000000},
262 {54, 55, 3, 0x80000000000000},
263 {55, 56, 3, 0x100000000000000},
264 {56, 57, 3, 0x200000000000000},
265 {57, 58, 3, 0x400000000000000},
266 {58, 59, 3, 0x800000000000000},
267 {59, 60, 3, 0x1000000000000000},
268 {60, 61, 3, 0x2000000000000000},
269 {61, 62, 3, 0x4000000000000000},
270 {62, 63, 3, 0x8000000000000000},
293 {16, 17, 2, 0x20000},
294 {17, 18, 2, 0x40000},
295 {18, 19, 2, 0x80000},
296 {19, 20, 2, 0x100000},
297 {20, 21, 2, 0x200000},
298 {21, 22, 2, 0x400000},
299 {22, 23, 2, 0x800000},
300 {23, 24, 2, 0x1000000},
301 {24, 25, 2, 0x2000000},
302 {25, 26, 2, 0x4000000},
303 {26, 27, 2, 0x8000000},
304 {27, 28, 2, 0x10000000},
305 {28, 29, 2, 0x20000000},
306 {29, 30, 2, 0x40000000},
307 {30, 31, 2, 0x80000000},
313#define RESERVED_HASH_VAL (~(st_hash_t) 0)
314#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
317static inline st_hash_t
318do_hash(st_data_t key,
st_table *tab)
320 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
324 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
328#define MINIMAL_POWER2 2
330#if MINIMAL_POWER2 < 2
331#error "MINIMAL_POWER2 should be >= 2"
336#define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
340get_power2(st_index_t size)
342 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
344 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
355static inline st_index_t
356get_bin(st_index_t *bins,
int s, st_index_t n)
358 return (s == 0 ? ((
unsigned char *) bins)[n]
359 : s == 1 ? ((unsigned short *) bins)[n]
360 : s == 2 ? ((unsigned int *) bins)[n]
361 : ((st_index_t *) bins)[n]);
367set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
369 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
370 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
371 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
372 else ((st_index_t *) bins)[n] = v;
385#define MARK_BIN_EMPTY(tab, i) (set_bin((tab)->bins, get_size_ind(tab), i, EMPTY_BIN))
389#define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
390#define UNDEFINED_BIN_IND (~(st_index_t) 0)
394#define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
395#define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
400#define MARK_BIN_DELETED(tab, i) \
402 set_bin((tab)->bins, get_size_ind(tab), i, DELETED_BIN); \
407#define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
408#define DELETED_BIN_P(b) ((b) == DELETED_BIN)
409#define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
413#define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
414#define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
415#define IND_EMPTY_OR_DELETED_BIN_P(tab, i) (EMPTY_OR_DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
419#define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
420#define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
423static inline unsigned int
426 return tab->size_ind;
430static inline st_index_t
433 return ((st_index_t) 1)<<tab->bin_power;
437static inline st_index_t
440 return get_bins_num(tab) - 1;
445static inline st_index_t
446hash_bin(st_hash_t hash_value,
st_table *tab)
448 return hash_value & bins_mask(tab);
452static inline st_index_t
453get_allocated_entries(
const st_table *tab)
455 return ((st_index_t) 1)<<tab->entry_power;
459static inline st_index_t
462 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
469 memset(tab->bins, 0, bins_size(tab));
476 tab->num_entries = 0;
477 tab->entries_start = tab->entries_bound = 0;
478 if (tab->bins != NULL)
479 initialize_bins(tab);
487 int all, total, num, str, strcase;
492static int init_st = 0;
499 char fname[10+
sizeof(long)*3];
501 if (!collision.total)
return;
502 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
505 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
506 ((
double)collision.all / (collision.total)) * 100);
507 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
524 const char *e = getenv(
"ST_HASH_LOG");
525 if (!e || !*e) init_st = 1;
534 n = get_power2(size);
545 tab->entry_power = n;
546 tab->bin_power = features[n].bin_power;
547 tab->size_ind = features[n].size_ind;
548 if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
551 tab->bins = (st_index_t *) malloc(bins_size(tab));
553 if (tab->bins == NULL) {
559 tab->entries = (
st_table_entry *) malloc(get_allocated_entries(tab)
562 if (tab->entries == NULL) {
568 tab->rebuilds_num = 0;
573st_table_size(
const struct st_table *tbl)
575 return tbl->num_entries;
583 return st_init_table_with_size(
type, 0);
589st_init_numtable(
void)
591 return st_init_table(&type_numhash);
596st_init_numtable_with_size(st_index_t size)
598 return st_init_table_with_size(&type_numhash, size);
604st_init_strtable(
void)
606 return st_init_table(&type_strhash);
611st_init_strtable_with_size(st_index_t size)
613 return st_init_table_with_size(&type_strhash, size);
619st_init_strcasetable(
void)
621 return st_init_table(&type_strcasehash);
627st_init_strcasetable_with_size(st_index_t size)
629 return st_init_table_with_size(&type_strcasehash, size);
644 if (tab->bins != NULL)
655 + (tab->bins == NULL ? 0 : bins_size(tab))
660find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
663find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
666find_table_bin_ind_direct(
st_table *table, st_hash_t hash_value, st_data_t key);
669find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
670 st_data_t key, st_index_t *bin_ind);
677 if (
type == &type_numhash) {
680 else if (
type == &type_strhash) {
683 else if (
type == &type_strcasehash) {
688#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
689#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
690#define collision_check 0
699#define REBUILD_THRESHOLD 4
701#if REBUILD_THRESHOLD < 2
702#error "REBUILD_THRESHOLD should be >= 2"
714 if ((2 * tab->num_entries <= get_allocated_entries(tab)
715 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
716 || tab->num_entries < (1 << MINIMAL_POWER2)) {
718 tab->num_entries = 0;
719 if (tab->bins != NULL)
720 initialize_bins(tab);
721 rebuild_table_with(tab, tab);
728 new_tab = st_init_table_with_size(tab->type,
729 2 * tab->num_entries - 1);
730 rebuild_table_with(new_tab, tab);
738 unsigned int size_ind;
744 new_entries = new_tab->entries;
747 bins = new_tab->bins;
748 size_ind = get_size_ind(new_tab);
749 st_index_t bound = tab->entries_bound;
752 for (i = tab->entries_start; i < bound; i++) {
753 curr_entry_ptr = &entries[i];
754 PREFETCH(entries + i + 1, 0);
755 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
757 if (&new_entries[ni] != curr_entry_ptr)
758 new_entries[ni] = *curr_entry_ptr;
759 if (EXPECT(bins != NULL, 1)) {
760 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
761 curr_entry_ptr->key);
762 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
764 new_tab->num_entries++;
767 if (new_tab != tab) {
768 tab->entry_power = new_tab->entry_power;
769 tab->bin_power = new_tab->bin_power;
770 tab->size_ind = new_tab->size_ind;
771 if (tab->bins != NULL)
773 tab->bins = new_tab->bins;
775 tab->entries = new_tab->entries;
778 tab->entries_start = 0;
779 tab->entries_bound = tab->num_entries;
795static inline st_index_t
796secondary_hash(st_index_t ind,
st_table *tab, st_index_t *perterb)
799 ind = (ind << 2) + ind + *perterb + 1;
800 return hash_bin(ind, tab);
807static inline st_index_t
808find_entry(
st_table *tab, st_hash_t hash_value, st_data_t key)
814 bound = tab->entries_bound;
815 entries = tab->entries;
816 for (i = tab->entries_start; i < bound; i++) {
817 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
818 if (EXPECT(rebuilt_p, 0))
819 return REBUILT_TABLE_ENTRY_IND;
823 return UNDEFINED_ENTRY_IND;
835find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
839#ifdef QUADRATIC_PROBE
847 ind = hash_bin(hash_value, tab);
848#ifdef QUADRATIC_PROBE
855 bin = get_bin(tab->bins, get_size_ind(tab), ind);
856 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
857 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
858 if (EXPECT(rebuilt_p, 0))
859 return REBUILT_TABLE_ENTRY_IND;
863 else if (EMPTY_BIN_P(bin))
864 return UNDEFINED_ENTRY_IND;
865#ifdef QUADRATIC_PROBE
866 ind = hash_bin(ind + d, tab);
869 ind = secondary_hash(ind, tab, &peterb);
881find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
885#ifdef QUADRATIC_PROBE
893 ind = hash_bin(hash_value, tab);
894#ifdef QUADRATIC_PROBE
901 bin = get_bin(tab->bins, get_size_ind(tab), ind);
902 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
903 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
904 if (EXPECT(rebuilt_p, 0))
905 return REBUILT_TABLE_BIN_IND;
909 else if (EMPTY_BIN_P(bin))
910 return UNDEFINED_BIN_IND;
911#ifdef QUADRATIC_PROBE
912 ind = hash_bin(ind + d, tab);
915 ind = secondary_hash(ind, tab, &peterb);
926find_table_bin_ind_direct(
st_table *tab, st_hash_t hash_value, st_data_t key)
929#ifdef QUADRATIC_PROBE
936 ind = hash_bin(hash_value, tab);
937#ifdef QUADRATIC_PROBE
944 bin = get_bin(tab->bins, get_size_ind(tab), ind);
945 if (EMPTY_OR_DELETED_BIN_P(bin))
947#ifdef QUADRATIC_PROBE
948 ind = hash_bin(ind + d, tab);
951 ind = secondary_hash(ind, tab, &peterb);
967find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
968 st_data_t key, st_index_t *bin_ind)
972 st_hash_t curr_hash_value = *hash_value;
973#ifdef QUADRATIC_PROBE
978 st_index_t entry_index;
979 st_index_t first_deleted_bin_ind;
982 ind = hash_bin(curr_hash_value, tab);
983#ifdef QUADRATIC_PROBE
986 peterb = curr_hash_value;
989 first_deleted_bin_ind = UNDEFINED_BIN_IND;
990 entries = tab->entries;
992 entry_index = get_bin(tab->bins, get_size_ind(tab), ind);
993 if (EMPTY_BIN_P(entry_index)) {
995 entry_index = UNDEFINED_ENTRY_IND;
996 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
998 ind = first_deleted_bin_ind;
999 MARK_BIN_EMPTY(tab, ind);
1003 else if (! DELETED_BIN_P(entry_index)) {
1004 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1005 if (EXPECT(rebuilt_p, 0))
1006 return REBUILT_TABLE_ENTRY_IND;
1010 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1011 first_deleted_bin_ind = ind;
1012#ifdef QUADRATIC_PROBE
1013 ind = hash_bin(ind + d, tab);
1016 ind = secondary_hash(ind, tab, &peterb);
1027st_lookup(
st_table *tab, st_data_t key, st_data_t *value)
1030 st_hash_t hash = do_hash(key, tab);
1033 if (tab->bins == NULL) {
1034 bin = find_entry(tab, hash, key);
1035 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1037 if (bin == UNDEFINED_ENTRY_IND)
1041 bin = find_table_entry_ind(tab, hash, key);
1042 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1044 if (bin == UNDEFINED_ENTRY_IND)
1049 *value = tab->entries[bin].record;
1056st_get_key(
st_table *tab, st_data_t key, st_data_t *result)
1059 st_hash_t hash = do_hash(key, tab);
1062 if (tab->bins == NULL) {
1063 bin = find_entry(tab, hash, key);
1064 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1066 if (bin == UNDEFINED_ENTRY_IND)
1070 bin = find_table_entry_ind(tab, hash, key);
1071 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1073 if (bin == UNDEFINED_ENTRY_IND)
1078 *result = tab->entries[bin].key;
1084rebuild_table_if_necessary (
st_table *tab)
1086 st_index_t bound = tab->entries_bound;
1088 if (bound == get_allocated_entries(tab))
1096st_insert(
st_table *tab, st_data_t key, st_data_t value)
1101 st_hash_t hash_value;
1105 hash_value = do_hash(key, tab);
1107 rebuild_table_if_necessary(tab);
1108 if (tab->bins == NULL) {
1109 bin = find_entry(tab, hash_value, key);
1110 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1112 new_p = bin == UNDEFINED_ENTRY_IND;
1115 bin_ind = UNDEFINED_BIN_IND;
1118 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1120 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1122 new_p = bin == UNDEFINED_ENTRY_IND;
1126 ind = tab->entries_bound++;
1127 entry = &tab->entries[ind];
1128 entry->hash = hash_value;
1130 entry->record = value;
1131 if (bin_ind != UNDEFINED_BIN_IND)
1132 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1135 tab->entries[bin].record = value;
1142st_add_direct_with_hash(
st_table *tab,
1143 st_data_t key, st_data_t value, st_hash_t hash)
1149 rebuild_table_if_necessary(tab);
1150 ind = tab->entries_bound++;
1151 entry = &tab->entries[ind];
1154 entry->record = value;
1156 if (tab->bins != NULL) {
1157 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1158 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1165st_add_direct(
st_table *tab, st_data_t key, st_data_t value)
1167 st_hash_t hash_value;
1169 hash_value = do_hash(key, tab);
1170 st_add_direct_with_hash(tab, key, value, hash_value);
1177st_insert2(
st_table *tab, st_data_t key, st_data_t value,
1178 st_data_t (*func)(st_data_t))
1183 st_hash_t hash_value;
1187 hash_value = do_hash(key, tab);
1189 rebuild_table_if_necessary (tab);
1190 if (tab->bins == NULL) {
1191 bin = find_entry(tab, hash_value, key);
1192 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1194 new_p = bin == UNDEFINED_ENTRY_IND;
1197 bin_ind = UNDEFINED_BIN_IND;
1200 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1202 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1204 new_p = bin == UNDEFINED_ENTRY_IND;
1209 ind = tab->entries_bound++;
1210 entry = &tab->entries[ind];
1211 entry->hash = hash_value;
1213 entry->record = value;
1214 if (bin_ind != UNDEFINED_BIN_IND)
1215 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1218 tab->entries[bin].record = value;
1230 if (new_tab == NULL)
1233 *new_tab = *old_tab;
1234 if (old_tab->bins == NULL)
1235 new_tab->bins = NULL;
1237 new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
1239 if (new_tab->bins == NULL) {
1245 new_tab->entries = (
st_table_entry *) malloc(get_allocated_entries(old_tab)
1248 if (new_tab->entries == NULL) {
1249 st_free_table(new_tab);
1254 get_allocated_entries(old_tab));
1255 if (old_tab->bins != NULL)
1256 MEMCPY(new_tab->bins, old_tab->bins,
char, bins_size(old_tab));
1263update_range_for_deleted(
st_table *tab, st_index_t n)
1267 if (tab->entries_start == n) {
1268 st_index_t start = n + 1;
1269 st_index_t bound = tab->entries_bound;
1271 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1272 tab->entries_start = start;
1281st_general_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1288 hash = do_hash(*key, tab);
1290 if (tab->bins == NULL) {
1291 bin = find_entry(tab, hash, *key);
1292 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1294 if (bin == UNDEFINED_ENTRY_IND) {
1295 if (value != 0) *value = 0;
1300 bin_ind = find_table_bin_ind(tab, hash, *key);
1301 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1303 if (bin_ind == UNDEFINED_BIN_IND) {
1304 if (value != 0) *value = 0;
1307 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1308 MARK_BIN_DELETED(tab, bin_ind);
1310 entry = &tab->entries[bin];
1312 if (value != 0) *value = entry->record;
1313 MARK_ENTRY_DELETED(entry);
1315 update_range_for_deleted(tab, bin);
1320st_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1322 return st_general_delete(tab, key, value);
1331st_delete_safe(
st_table *tab, st_data_t *key, st_data_t *value,
1332 st_data_t never ATTRIBUTE_UNUSED)
1334 return st_general_delete(tab, key, value);
1342st_shift(
st_table *tab, st_data_t *key, st_data_t *value)
1344 st_index_t i, bound;
1349 entries = tab->entries;
1350 bound = tab->entries_bound;
1351 for (i = tab->entries_start; i < bound; i++) {
1352 curr_entry_ptr = &entries[i];
1353 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1354 st_hash_t entry_hash = curr_entry_ptr->hash;
1355 st_data_t entry_key = curr_entry_ptr->key;
1357 if (value != 0) *value = curr_entry_ptr->record;
1360 if (tab->bins == NULL) {
1361 bin = find_entry(tab, entry_hash, entry_key);
1362 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1363 entries = tab->entries;
1366 curr_entry_ptr = &entries[bin];
1369 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1370 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1371 entries = tab->entries;
1374 curr_entry_ptr = &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind)
1376 MARK_BIN_DELETED(tab, bin_ind);
1378 MARK_ENTRY_DELETED(curr_entry_ptr);
1380 update_range_for_deleted(tab, i);
1384 if (value != 0) *value = 0;
1390st_cleanup_safe(
st_table *tab ATTRIBUTE_UNUSED,
1391 st_data_t never ATTRIBUTE_UNUSED)
1405st_update(
st_table *tab, st_data_t key,
1406 st_update_callback_func *func, st_data_t arg)
1412 st_data_t value = 0, old_key;
1413 int retval, existing;
1414 st_hash_t hash = do_hash(key, tab);
1417 entries = tab->entries;
1418 if (tab->bins == NULL) {
1419 bin = find_entry(tab, hash, key);
1420 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1422 existing = bin != UNDEFINED_ENTRY_IND;
1423 entry = &entries[bin];
1424 bin_ind = UNDEFINED_BIN_IND;
1427 bin_ind = find_table_bin_ind(tab, hash, key);
1428 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1430 existing = bin_ind != UNDEFINED_BIN_IND;
1432 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1433 entry = &entries[bin];
1438 value = entry->record;
1441 retval = (*func)(&key, &value, arg, existing);
1445 st_add_direct_with_hash(tab, key, value, hash);
1448 if (old_key != key) {
1451 entry->record = value;
1455 if (bin_ind != UNDEFINED_BIN_IND)
1456 MARK_BIN_DELETED(tab, bin_ind);
1457 MARK_ENTRY_DELETED(entry);
1459 update_range_for_deleted(tab, bin);
1475st_general_foreach(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1481 enum st_retval retval;
1482 st_index_t i, rebuilds_num;
1485 int error_p, packed_p = tab->bins == NULL;
1487 entries = tab->entries;
1490 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1491 curr_entry_ptr = &entries[i];
1492 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1494 key = curr_entry_ptr->key;
1495 rebuilds_num = tab->rebuilds_num;
1496 hash = curr_entry_ptr->hash;
1497 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1499 if (retval == ST_REPLACE && replace) {
1501 value = curr_entry_ptr->record;
1502 retval = (*replace)(&key, &value, arg, TRUE);
1503 curr_entry_ptr->key = key;
1504 curr_entry_ptr->record = value;
1507 if (rebuilds_num != tab->rebuilds_num) {
1509 entries = tab->entries;
1510 packed_p = tab->bins == NULL;
1512 i = find_entry(tab, hash, key);
1513 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1515 error_p = i == UNDEFINED_ENTRY_IND;
1518 i = find_table_entry_ind(tab, hash, key);
1519 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1521 error_p = i == UNDEFINED_ENTRY_IND;
1524 if (error_p && check_p) {
1526 retval = (*func)(0, 0, arg, 1);
1529 curr_entry_ptr = &entries[i];
1542 st_data_t key = curr_entry_ptr->key;
1546 bin = find_entry(tab, hash, key);
1547 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1549 if (bin == UNDEFINED_ENTRY_IND)
1553 bin_ind = find_table_bin_ind(tab, hash, key);
1554 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1556 if (bin_ind == UNDEFINED_BIN_IND)
1558 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1559 MARK_BIN_DELETED(tab, bin_ind);
1561 curr_entry_ptr = &entries[bin];
1562 MARK_ENTRY_DELETED(curr_entry_ptr);
1564 update_range_for_deleted(tab, bin);
1573st_foreach_with_replace(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1575 return st_general_foreach(tab, func, replace, arg, TRUE);
1579 st_foreach_callback_func *func;
1584apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1586 const struct functor *f = (
void *)d;
1587 return f->func(k, v, f->arg);
1591st_foreach(
st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1593 const struct functor f = { func, arg };
1594 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1599st_foreach_check(
st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1600 st_data_t never ATTRIBUTE_UNUSED)
1602 return st_general_foreach(tab, func, 0, arg, TRUE);
1607static inline st_index_t
1608st_general_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1610 st_index_t i, bound;
1611 st_data_t key, *keys_start, *keys_end;
1614 bound = tab->entries_bound;
1616 keys_end = keys + size;
1617 for (i = tab->entries_start; i < bound; i++) {
1618 if (keys == keys_end)
1620 curr_entry_ptr = &entries[i];
1621 key = curr_entry_ptr->key;
1622 if (! DELETED_ENTRY_P(curr_entry_ptr))
1626 return keys - keys_start;
1630st_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1632 return st_general_keys(tab, keys, size);
1637st_keys_check(
st_table *tab, st_data_t *keys, st_index_t size,
1638 st_data_t never ATTRIBUTE_UNUSED)
1640 return st_general_keys(tab, keys, size);
1645static inline st_index_t
1646st_general_values(
st_table *tab, st_data_t *values, st_index_t size)
1648 st_index_t i, bound;
1649 st_data_t *values_start, *values_end;
1652 values_start = values;
1653 values_end = values + size;
1654 bound = tab->entries_bound;
1655 for (i = tab->entries_start; i < bound; i++) {
1656 if (values == values_end)
1658 curr_entry_ptr = &entries[i];
1659 if (! DELETED_ENTRY_P(curr_entry_ptr))
1660 *values++ = curr_entry_ptr->record;
1663 return values - values_start;
1667st_values(
st_table *tab, st_data_t *values, st_index_t size)
1669 return st_general_values(tab, values, size);
1674st_values_check(
st_table *tab, st_data_t *values, st_index_t size,
1675 st_data_t never ATTRIBUTE_UNUSED)
1677 return st_general_values(tab, values, size);
1680#define FNV1_32A_INIT 0x811c9dc5
1685#define FNV_32_PRIME 0x01000193
1688#ifndef UNALIGNED_WORD_ACCESS
1689# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1690 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1691 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1692 defined(__mc68020__)
1693# define UNALIGNED_WORD_ACCESS 1
1696#ifndef UNALIGNED_WORD_ACCESS
1697# define UNALIGNED_WORD_ACCESS 0
1703#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1704#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1706#if ST_INDEX_BITS <= 32
1707#define C1 (st_index_t)0xcc9e2d51
1708#define C2 (st_index_t)0x1b873593
1710#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1711#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1713NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1714NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1715NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1717static inline st_index_t
1718murmur_step(st_index_t h, st_index_t k)
1720#if ST_INDEX_BITS <= 32
1736static inline st_index_t
1737murmur_finish(st_index_t h)
1739#if ST_INDEX_BITS <= 32
1743 const st_index_t c1 = 0x85ebca6b;
1744 const st_index_t c2 = 0xc2b2ae35;
1750 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1751 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1753#if ST_INDEX_BITS > 64
1770st_hash(
const void *ptr,
size_t len, st_index_t h)
1772 const char *data = ptr;
1776#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1777#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1778#if SIZEOF_ST_INDEX_T > 4
1779#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1780#if SIZEOF_ST_INDEX_T > 8
1781#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1782 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1783#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1785#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1787#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1790 if (len >=
sizeof(st_index_t)) {
1791#if !UNALIGNED_WORD_ACCESS
1792 int align = (int)((st_data_t)data %
sizeof(st_index_t));
1798#ifdef WORDS_BIGENDIAN
1799# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1800 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1802# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1803 t |= data_at(n) << CHAR_BIT*(n)
1809#ifdef WORDS_BIGENDIAN
1810 t >>= (CHAR_BIT * align) - CHAR_BIT;
1812 t <<= (CHAR_BIT * align);
1815 data +=
sizeof(st_index_t)-align;
1816 len -=
sizeof(st_index_t)-align;
1818 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1819 sr = CHAR_BIT * align;
1821 while (len >=
sizeof(st_index_t)) {
1822 d = *(st_index_t *)data;
1823#ifdef WORDS_BIGENDIAN
1824 t = (t << sr) | (d >> sl);
1826 t = (t >> sr) | (d << sl);
1828 h = murmur_step(h, t);
1830 data +=
sizeof(st_index_t);
1831 len -=
sizeof(st_index_t);
1834 pack = len < (size_t)align ? (
int)len : align;
1837#ifdef WORDS_BIGENDIAN
1838# define UNALIGNED_ADD(n) case (n) + 1: \
1839 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1841# define UNALIGNED_ADD(n) case (n) + 1: \
1842 d |= data_at(n) << CHAR_BIT*(n)
1847#ifdef WORDS_BIGENDIAN
1848 t = (t << sr) | (d >> sl);
1850 t = (t >> sr) | (d << sl);
1853 if (len < (
size_t)align)
goto skip_tail;
1855 h = murmur_step(h, t);
1861#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
1862#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
1864#define aligned_data data
1868 h = murmur_step(h, *(st_index_t *)aligned_data);
1869 data +=
sizeof(st_index_t);
1870 len -=
sizeof(st_index_t);
1871 }
while (len >=
sizeof(st_index_t));
1877#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
1879#if SIZEOF_ST_INDEX_T > 4
1880 case 7: t |= data_at(6) << 48;
1881 case 6: t |= data_at(5) << 40;
1882 case 5: t |= data_at(4) << 32;
1884 t |= (st_index_t)*(uint32_t*)aligned_data;
1888 case 3: t |= data_at(2) << 16;
1889 case 2: t |= data_at(1) << 8;
1890 case 1: t |= data_at(0);
1892#ifdef WORDS_BIGENDIAN
1893# define UNALIGNED_ADD(n) case (n) + 1: \
1894 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1896# define UNALIGNED_ADD(n) case (n) + 1: \
1897 t |= data_at(n) << CHAR_BIT*(n)
1905 h ^= t; h -= ROTL(t, 7);
1911 return murmur_finish(h);
1915st_hash_uint32(st_index_t h, uint32_t i)
1917 return murmur_step(h, i);
1920NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
1922st_hash_uint(st_index_t h, st_index_t i)
1927#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1928 h = murmur_step(h, i >> 8*8);
1930 h = murmur_step(h, i);
1935st_hash_end(st_index_t h)
1937 h = murmur_finish(h);
1943rb_st_hash_start(st_index_t h)
1949strhash(st_data_t arg)
1951 register const char *
string = (
const char *)arg;
1952 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
1956st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
1963 if (c1 ==
'\0' || c2 ==
'\0') {
1964 if (c1 !=
'\0')
return 1;
1965 if (c2 !=
'\0')
return -1;
1968 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
1969 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
1980st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
1985 for (i = 0; i < n; i++) {
1988 if (c1 ==
'\0' || c2 ==
'\0') {
1989 if (c1 !=
'\0')
return 1;
1990 if (c2 !=
'\0')
return -1;
1993 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
1994 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2006st_strcmp(st_data_t lhs, st_data_t rhs)
2008 const char *s1 = (
char *)lhs;
2009 const char *s2 = (
char *)rhs;
2010 return strcmp(s1, s2);
2014st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2016 const char *s1 = (
char *)lhs;
2017 const char *s2 = (
char *)rhs;
2018 return st_locale_insensitive_strcasecmp(s1, s2);
2021NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2023strcasehash(st_data_t arg)
2025 register const char *
string = (
const char *)arg;
2026 register st_index_t hval = FNV1_32A_INIT;
2032 unsigned int c = (
unsigned char)*
string++;
2033 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2037 hval *= FNV_32_PRIME;
2043st_numcmp(st_data_t x, st_data_t y)
2049st_numhash(st_data_t n)
2051 enum {s1 = 11, s2 = 3};
2052 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2059st_expand_table(
st_table *tab, st_index_t siz)
2064 if (siz <= get_allocated_entries(tab))
2067 tmp = st_init_table_with_size(tab->type, siz);
2068 n = get_allocated_entries(tab);
2071 if (tab->bins != NULL)
2073 if (tmp->bins != NULL)
2075 tab->entry_power = tmp->entry_power;
2076 tab->bin_power = tmp->bin_power;
2077 tab->size_ind = tmp->size_ind;
2078 tab->entries = tmp->entries;
2080 tab->rebuilds_num++;
2089 int eq_p, rebuilt_p;
2096 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2097 p = &tab->entries[i];
2098 if (DELETED_ENTRY_P(p))
2100 for (j = i + 1; j < tab->entries_bound; j++) {
2101 q = &tab->entries[j];
2102 if (DELETED_ENTRY_P(q))
2104 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2105 if (EXPECT(rebuilt_p, 0))
2109 MARK_ENTRY_DELETED(q);
2111 update_range_for_deleted(tab, j);
2123 int eq_p, rebuilt_p;
2125 st_index_t
const n = bins_size(tab);
2126 unsigned int const size_ind = get_size_ind(tab);
2127 st_index_t *bins = realloc(tab->bins, n);
2129 initialize_bins(tab);
2130 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2133#ifdef QUADRATIC_PROBE
2136 st_index_t peterb = p->hash;
2139 if (DELETED_ENTRY_P(p))
2142 ind = hash_bin(p->hash, tab);
2144 st_index_t bin = get_bin(bins, size_ind, ind);
2145 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2147 set_bin(bins, size_ind, ind, i + ENTRY_BASE);
2152 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2153 if (EXPECT(rebuilt_p, 0))
2157 q->record = p->record;
2158 MARK_ENTRY_DELETED(p);
2160 update_range_for_deleted(tab, bin);
2165#ifdef QUADRATIC_PROBE
2166 ind = hash_bin(ind + d, tab);
2169 ind = secondary_hash(ind, tab, &peterb);
2187 if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2188 rebuilt_p = st_rehash_linear(tab);
2190 rebuilt_p = st_rehash_indexed(tab);
2191 }
while (rebuilt_p);
2196st_stringify(
VALUE key)
2199 rb_hash_key_str(key) : key;
2205 st_data_t k = st_stringify(key);
2207 e.hash = do_hash(k, tab);
2211 tab->entries[tab->entries_bound++] = e;
2222 for (i = 0; i < argc; ) {
2223 st_data_t k = st_stringify(argv[i++]);
2224 st_data_t v = argv[i++];
2225 st_insert(tab, k, v);
2237 for (i = 0; i < argc; ) {
2238 VALUE key = argv[i++];
2239 VALUE val = argv[i++];
2240 st_insert_single(tab, hash, key, val);
2250rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2252 st_index_t n, size = argc / 2;
2253 st_table *tab = RHASH_ST_TABLE(hash);
2255 tab = RHASH_TBL_RAW(hash);
2256 n = tab->entries_bound + size;
2257 st_expand_table(tab, n);
2258 if (UNLIKELY(tab->num_entries))
2259 st_insert_generic(tab, argc, argv, hash);
2261 st_insert_single(tab, hash, argv[0], argv[1]);
2262 else if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2263 st_insert_linear(tab, argc, argv, hash);
2265 st_insert_generic(tab, argc, argv, hash);
2270rb_st_nth_key(
st_table *tab, st_index_t index)
2272 if (LIKELY(tab->entries_start == 0 &&
2273 tab->num_entries == tab->entries_bound &&
2274 index < tab->num_entries)) {
2275 return tab->entries[index].key;
2285 st_index_t num = tab->num_entries;
2286 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2288 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2289 rebuild_table_with(new_tab, tab);
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
#define Qundef
Old name of RUBY_Qundef.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cString
String class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define _(args)
This was a transition path from K&R to ANSI.
uintptr_t VALUE
Type that represents a Ruby object.