107#elif defined RUBY_EXPORT
109#include "internal/bits.h"
110#include "internal/gc.h"
111#include "internal/hash.h"
112#include "internal/sanitizers.h"
113#include "internal/set_table.h"
114#include "internal/st.h"
115#include "ruby_assert.h"
125#define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
126#define EXPECT(expr, val) __builtin_expect(expr, val)
127#define ATTRIBUTE_UNUSED __attribute__((unused))
129#define PREFETCH(addr, write_p)
130#define EXPECT(expr, val) (expr)
131#define ATTRIBUTE_UNUSED
135typedef st_index_t st_hash_t;
143#define type_numhash st_hashtype_num
149static int st_strcmp(st_data_t, st_data_t);
150static st_index_t strhash(st_data_t);
156static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
157static st_index_t strcasehash(st_data_t);
159 st_locale_insensitive_strcasecmp_i,
166#define ST_INIT_VAL 0xafafafafafafafaf
167#define ST_INIT_VAL_BYTE 0xafa
174#define malloc ruby_xmalloc
175#define calloc ruby_xcalloc
176#define realloc ruby_xrealloc
177#define sized_realloc ruby_xrealloc_sized
178#define free ruby_xfree
179#define sized_free ruby_xfree_sized
180#define free_fixed_ptr(v) ruby_xfree_sized((v), sizeof(*(v)))
182#define sized_realloc(ptr, new_size, old_size) realloc(ptr, new_size)
183#define sized_free(v, s) free(v)
184#define free_fixed_ptr(v) free(v)
187#define EQUAL(tab,x,y) ((x) == (y) || (*(tab)->type->compare)((x),(y)) == 0)
188#define PTR_EQUAL(tab, ptr, hash_val, key_) \
189 ((ptr)->hash == (hash_val) && EQUAL((tab), (key_), (ptr)->key))
193#define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
195 unsigned int _old_rebuilds_num = (tab)->rebuilds_num; \
196 res = PTR_EQUAL(tab, ptr, hash_val, key); \
197 rebuilt_p = _old_rebuilds_num != (tab)->rebuilds_num; \
203 unsigned char entry_power;
207 unsigned char bin_power;
209 unsigned char size_ind;
212 st_index_t bins_words;
216#if SIZEOF_ST_INDEX_T == 8
235 {16, 17, 2, 0x10000},
236 {17, 18, 2, 0x20000},
237 {18, 19, 2, 0x40000},
238 {19, 20, 2, 0x80000},
239 {20, 21, 2, 0x100000},
240 {21, 22, 2, 0x200000},
241 {22, 23, 2, 0x400000},
242 {23, 24, 2, 0x800000},
243 {24, 25, 2, 0x1000000},
244 {25, 26, 2, 0x2000000},
245 {26, 27, 2, 0x4000000},
246 {27, 28, 2, 0x8000000},
247 {28, 29, 2, 0x10000000},
248 {29, 30, 2, 0x20000000},
249 {30, 31, 2, 0x40000000},
250 {31, 32, 2, 0x80000000},
251 {32, 33, 3, 0x200000000},
252 {33, 34, 3, 0x400000000},
253 {34, 35, 3, 0x800000000},
254 {35, 36, 3, 0x1000000000},
255 {36, 37, 3, 0x2000000000},
256 {37, 38, 3, 0x4000000000},
257 {38, 39, 3, 0x8000000000},
258 {39, 40, 3, 0x10000000000},
259 {40, 41, 3, 0x20000000000},
260 {41, 42, 3, 0x40000000000},
261 {42, 43, 3, 0x80000000000},
262 {43, 44, 3, 0x100000000000},
263 {44, 45, 3, 0x200000000000},
264 {45, 46, 3, 0x400000000000},
265 {46, 47, 3, 0x800000000000},
266 {47, 48, 3, 0x1000000000000},
267 {48, 49, 3, 0x2000000000000},
268 {49, 50, 3, 0x4000000000000},
269 {50, 51, 3, 0x8000000000000},
270 {51, 52, 3, 0x10000000000000},
271 {52, 53, 3, 0x20000000000000},
272 {53, 54, 3, 0x40000000000000},
273 {54, 55, 3, 0x80000000000000},
274 {55, 56, 3, 0x100000000000000},
275 {56, 57, 3, 0x200000000000000},
276 {57, 58, 3, 0x400000000000000},
277 {58, 59, 3, 0x800000000000000},
278 {59, 60, 3, 0x1000000000000000},
279 {60, 61, 3, 0x2000000000000000},
280 {61, 62, 3, 0x4000000000000000},
281 {62, 63, 3, 0x8000000000000000},
304 {16, 17, 2, 0x20000},
305 {17, 18, 2, 0x40000},
306 {18, 19, 2, 0x80000},
307 {19, 20, 2, 0x100000},
308 {20, 21, 2, 0x200000},
309 {21, 22, 2, 0x400000},
310 {22, 23, 2, 0x800000},
311 {23, 24, 2, 0x1000000},
312 {24, 25, 2, 0x2000000},
313 {25, 26, 2, 0x4000000},
314 {26, 27, 2, 0x8000000},
315 {27, 28, 2, 0x10000000},
316 {28, 29, 2, 0x20000000},
317 {29, 30, 2, 0x40000000},
318 {30, 31, 2, 0x80000000},
324#define RESERVED_HASH_VAL (~(st_hash_t) 0)
325#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
327static inline st_hash_t
328normalize_hash_value(st_hash_t hash)
332 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
336static inline st_hash_t
337do_hash(st_data_t key,
st_table *tab)
339 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
340 return normalize_hash_value(hash);
344#define MINIMAL_POWER2 2
346#if MINIMAL_POWER2 < 2
347#error "MINIMAL_POWER2 should be >= 2"
352#define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
356get_power2(st_index_t size)
358 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
360 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
371static inline st_index_t
372get_bin(st_index_t *bins,
int s, st_index_t n)
374 return (s == 0 ? ((
unsigned char *) bins)[n]
375 : s == 1 ? ((unsigned short *) bins)[n]
376 : s == 2 ? ((unsigned int *) bins)[n]
377 : ((st_index_t *) bins)[n]);
383set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
385 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
386 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
387 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
388 else ((st_index_t *) bins)[n] = v;
401#define MARK_BIN_EMPTY(tab, i) (set_bin((tab)->bins, get_size_ind(tab), i, EMPTY_BIN))
405#define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
406#define UNDEFINED_BIN_IND (~(st_index_t) 0)
410#define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
411#define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
416#define MARK_BIN_DELETED(tab, i) \
418 set_bin((tab)->bins, get_size_ind(tab), i, DELETED_BIN); \
423#define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
424#define DELETED_BIN_P(b) ((b) == DELETED_BIN)
425#define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
429#define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
430#define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
431#define IND_EMPTY_OR_DELETED_BIN_P(tab, i) (EMPTY_OR_DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
435#define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
436#define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
439static inline unsigned int
442 return tab->size_ind;
446static inline st_index_t
449 return ((st_index_t) 1)<<tab->bin_power;
453static inline st_index_t
456 return get_bins_num(tab) - 1;
461static inline st_index_t
462hash_bin(st_hash_t hash_value,
st_table *tab)
464 return hash_value & bins_mask(tab);
468static inline st_index_t
469get_allocated_entries(
const st_table *tab)
471 return ((st_index_t) 1)<<tab->entry_power;
475static inline st_index_t
478 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
485 memset(tab->bins, 0, bins_size(tab));
492 tab->num_entries = 0;
493 tab->entries_start = tab->entries_bound = 0;
494 if (tab->bins != NULL)
495 initialize_bins(tab);
503 int all, total, num, str, strcase;
508static int init_st = 0;
515 char fname[10+
sizeof(long)*3];
517 if (!collision.total)
return;
518 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
521 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
522 ((
double)collision.all / (collision.total)) * 100);
523 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
529st_init_existing_table_with_size(
st_table *tab,
const struct st_hash_type *type, st_index_t size)
536 const char *e = getenv(
"ST_HASH_LOG");
537 if (!e || !*e) init_st = 1;
546 n = get_power2(size);
553 tab->entry_power = n;
554 tab->bin_power = features[n].bin_power;
555 tab->size_ind = features[n].size_ind;
556 if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
559 tab->bins = (st_index_t *) malloc(bins_size(tab));
561 if (tab->bins == NULL) {
567 tab->entries = (
st_table_entry *) malloc(get_allocated_entries(tab)
570 if (tab->entries == NULL) {
576 tab->rebuilds_num = 0;
581st_init_existing_numtable_with_size(
st_table *tab, st_index_t size)
583 return st_init_existing_table_with_size(tab, &type_numhash, size);
590st_init_table_with_size(
const struct st_hash_type *type, st_index_t size)
599 st_init_existing_table_with_size(tab, type, size);
601 if (st_init_existing_table_with_size(tab, type, size) == NULL) {
611st_table_size(
const struct st_table *tbl)
613 return tbl->num_entries;
621 return st_init_table_with_size(type, 0);
627st_init_numtable(
void)
629 return st_init_table(&type_numhash);
634st_init_numtable_with_size(st_index_t size)
636 return st_init_table_with_size(&type_numhash, size);
642st_init_strtable(
void)
644 return st_init_table(&type_strhash);
649st_init_strtable_with_size(st_index_t size)
651 return st_init_table_with_size(&type_strhash, size);
655st_init_existing_strtable_with_size(
st_table *tab, st_index_t size)
657 return st_init_existing_table_with_size(tab, &type_strhash, size);
664st_init_strcasetable(
void)
666 return st_init_table(&type_strcasehash);
672st_init_strcasetable_with_size(st_index_t size)
674 return st_init_table_with_size(&type_strcasehash, size);
686st_entries_memsize(
const st_table *tab)
694 return tab->bins == NULL ? 0 : bins_size(tab);
700 sized_free(tab->entries, st_entries_memsize(tab));
706 sized_free(tab->bins, st_bins_memsize(tab));
710st_free_embedded_table(
st_table *tab)
713 st_free_entries(tab);
720 st_free_embedded_table(tab);
730 + st_bins_memsize(tab)
731 + st_entries_memsize(tab));
735find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
738find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
741find_table_bin_ind_direct(
st_table *table, st_hash_t hash_value, st_data_t key);
744find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
745 st_data_t key, st_index_t *bin_ind);
752 if (type == &type_numhash) {
755 else if (type == &type_strhash) {
758 else if (type == &type_strcasehash) {
763#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
764#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
765#define collision_check 0
774#define REBUILD_THRESHOLD 4
776#if REBUILD_THRESHOLD < 2
777#error "REBUILD_THRESHOLD should be >= 2"
780static void rebuild_table_with(
st_table *
const new_tab,
st_table *
const tab);
781static void rebuild_move_table(
st_table *
const new_tab,
st_table *
const tab);
782static void rebuild_cleanup(
st_table *
const tab);
791 if ((2 * tab->num_entries <= get_allocated_entries(tab)
792 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
793 || tab->num_entries < (1 << MINIMAL_POWER2)) {
795 tab->num_entries = 0;
796 if (tab->bins != NULL)
797 initialize_bins(tab);
798 rebuild_table_with(tab, tab);
805 new_tab = st_init_table_with_size(tab->type,
806 2 * tab->num_entries - 1);
807 rebuild_table_with(new_tab, tab);
808 rebuild_move_table(new_tab, tab);
810 rebuild_cleanup(tab);
817 unsigned int size_ind;
823 new_entries = new_tab->entries;
826 bins = new_tab->bins;
827 size_ind = get_size_ind(new_tab);
828 st_index_t bound = tab->entries_bound;
831 for (i = tab->entries_start; i < bound; i++) {
832 curr_entry_ptr = &entries[i];
833 PREFETCH(entries + i + 1, 0);
834 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
836 if (&new_entries[ni] != curr_entry_ptr)
837 new_entries[ni] = *curr_entry_ptr;
838 if (EXPECT(bins != NULL, 1)) {
839 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
840 curr_entry_ptr->key);
841 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
843 new_tab->num_entries++;
847 assert(new_tab->num_entries == tab->num_entries);
854 st_free_entries(tab);
856 tab->entry_power = new_tab->entry_power;
857 tab->bin_power = new_tab->bin_power;
858 tab->size_ind = new_tab->size_ind;
859 tab->bins = new_tab->bins;
860 tab->entries = new_tab->entries;
861 free_fixed_ptr(new_tab);
867 tab->entries_start = 0;
868 tab->entries_bound = tab->num_entries;
884static inline st_index_t
885secondary_hash(st_index_t ind,
st_table *tab, st_index_t *perturb)
888 ind = (ind << 2) + ind + *perturb + 1;
889 return hash_bin(ind, tab);
896static inline st_index_t
897find_entry(
st_table *tab, st_hash_t hash_value, st_data_t key)
903 bound = tab->entries_bound;
904 entries = tab->entries;
905 for (i = tab->entries_start; i < bound; i++) {
906 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
907 if (EXPECT(rebuilt_p, 0))
908 return REBUILT_TABLE_ENTRY_IND;
912 return UNDEFINED_ENTRY_IND;
924find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
928#ifdef QUADRATIC_PROBE
936 ind = hash_bin(hash_value, tab);
937#ifdef QUADRATIC_PROBE
940 perturb = hash_value;
944 bin = get_bin(tab->bins, get_size_ind(tab), ind);
945 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
946 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
947 if (EXPECT(rebuilt_p, 0))
948 return REBUILT_TABLE_ENTRY_IND;
952 else if (EMPTY_BIN_P(bin))
953 return UNDEFINED_ENTRY_IND;
954#ifdef QUADRATIC_PROBE
955 ind = hash_bin(ind + d, tab);
958 ind = secondary_hash(ind, tab, &perturb);
970find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
974#ifdef QUADRATIC_PROBE
982 ind = hash_bin(hash_value, tab);
983#ifdef QUADRATIC_PROBE
986 perturb = hash_value;
990 bin = get_bin(tab->bins, get_size_ind(tab), ind);
991 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
992 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
993 if (EXPECT(rebuilt_p, 0))
994 return REBUILT_TABLE_BIN_IND;
998 else if (EMPTY_BIN_P(bin))
999 return UNDEFINED_BIN_IND;
1000#ifdef QUADRATIC_PROBE
1001 ind = hash_bin(ind + d, tab);
1004 ind = secondary_hash(ind, tab, &perturb);
1015find_table_bin_ind_direct(
st_table *tab, st_hash_t hash_value, st_data_t key)
1018#ifdef QUADRATIC_PROBE
1025 ind = hash_bin(hash_value, tab);
1026#ifdef QUADRATIC_PROBE
1029 perturb = hash_value;
1033 bin = get_bin(tab->bins, get_size_ind(tab), ind);
1034 if (EMPTY_OR_DELETED_BIN_P(bin))
1036#ifdef QUADRATIC_PROBE
1037 ind = hash_bin(ind + d, tab);
1040 ind = secondary_hash(ind, tab, &perturb);
1056find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
1057 st_data_t key, st_index_t *bin_ind)
1059 int eq_p, rebuilt_p;
1061 st_hash_t curr_hash_value = *hash_value;
1062#ifdef QUADRATIC_PROBE
1067 st_index_t entry_index;
1068 st_index_t first_deleted_bin_ind;
1071 ind = hash_bin(curr_hash_value, tab);
1072#ifdef QUADRATIC_PROBE
1075 perturb = curr_hash_value;
1078 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1079 entries = tab->entries;
1081 entry_index = get_bin(tab->bins, get_size_ind(tab), ind);
1082 if (EMPTY_BIN_P(entry_index)) {
1084 entry_index = UNDEFINED_ENTRY_IND;
1085 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1087 ind = first_deleted_bin_ind;
1088 MARK_BIN_EMPTY(tab, ind);
1092 else if (! DELETED_BIN_P(entry_index)) {
1093 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1094 if (EXPECT(rebuilt_p, 0))
1095 return REBUILT_TABLE_ENTRY_IND;
1099 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1100 first_deleted_bin_ind = ind;
1101#ifdef QUADRATIC_PROBE
1102 ind = hash_bin(ind + d, tab);
1105 ind = secondary_hash(ind, tab, &perturb);
1116st_lookup(
st_table *tab, st_data_t key, st_data_t *value)
1119 st_hash_t hash = do_hash(key, tab);
1122 if (tab->bins == NULL) {
1123 bin = find_entry(tab, hash, key);
1124 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1126 if (bin == UNDEFINED_ENTRY_IND)
1130 bin = find_table_entry_ind(tab, hash, key);
1131 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1133 if (bin == UNDEFINED_ENTRY_IND)
1138 *value = tab->entries[bin].record;
1145st_get_key(
st_table *tab, st_data_t key, st_data_t *result)
1148 st_hash_t hash = do_hash(key, tab);
1151 if (tab->bins == NULL) {
1152 bin = find_entry(tab, hash, key);
1153 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1155 if (bin == UNDEFINED_ENTRY_IND)
1159 bin = find_table_entry_ind(tab, hash, key);
1160 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1162 if (bin == UNDEFINED_ENTRY_IND)
1167 *result = tab->entries[bin].key;
1173rebuild_table_if_necessary (
st_table *tab)
1175 st_index_t bound = tab->entries_bound;
1177 if (bound == get_allocated_entries(tab))
1185st_insert(
st_table *tab, st_data_t key, st_data_t value)
1190 st_hash_t hash_value;
1194 hash_value = do_hash(key, tab);
1196 rebuild_table_if_necessary(tab);
1197 if (tab->bins == NULL) {
1198 bin = find_entry(tab, hash_value, key);
1199 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1201 new_p = bin == UNDEFINED_ENTRY_IND;
1204 bin_ind = UNDEFINED_BIN_IND;
1207 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1209 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1211 new_p = bin == UNDEFINED_ENTRY_IND;
1215 ind = tab->entries_bound++;
1216 entry = &tab->entries[ind];
1217 entry->hash = hash_value;
1219 entry->record = value;
1220 if (bin_ind != UNDEFINED_BIN_IND)
1221 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1224 tab->entries[bin].record = value;
1231st_add_direct_with_hash(
st_table *tab,
1232 st_data_t key, st_data_t value, st_hash_t hash)
1238 assert(hash != RESERVED_HASH_VAL);
1240 rebuild_table_if_necessary(tab);
1241 ind = tab->entries_bound++;
1242 entry = &tab->entries[ind];
1245 entry->record = value;
1247 if (tab->bins != NULL) {
1248 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1249 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1254rb_st_add_direct_with_hash(
st_table *tab,
1255 st_data_t key, st_data_t value, st_hash_t hash)
1257 st_add_direct_with_hash(tab, key, value, normalize_hash_value(hash));
1263st_add_direct(
st_table *tab, st_data_t key, st_data_t value)
1265 st_hash_t hash_value;
1267 hash_value = do_hash(key, tab);
1268 st_add_direct_with_hash(tab, key, value, hash_value);
1275st_insert2(
st_table *tab, st_data_t key, st_data_t value,
1276 st_data_t (*func)(st_data_t))
1281 st_hash_t hash_value;
1285 hash_value = do_hash(key, tab);
1287 rebuild_table_if_necessary (tab);
1288 if (tab->bins == NULL) {
1289 bin = find_entry(tab, hash_value, key);
1290 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1292 new_p = bin == UNDEFINED_ENTRY_IND;
1295 bin_ind = UNDEFINED_BIN_IND;
1298 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1300 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1302 new_p = bin == UNDEFINED_ENTRY_IND;
1307 ind = tab->entries_bound++;
1308 entry = &tab->entries[ind];
1309 entry->hash = hash_value;
1311 entry->record = value;
1312 if (bin_ind != UNDEFINED_BIN_IND)
1313 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1316 tab->entries[bin].record = value;
1324 *new_tab = *old_tab;
1325 if (old_tab->bins == NULL)
1326 new_tab->bins = NULL;
1328 new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
1330 if (new_tab->bins == NULL) {
1335 new_tab->entries = (
st_table_entry *) malloc(get_allocated_entries(old_tab)
1338 if (new_tab->entries == NULL) {
1343 get_allocated_entries(old_tab));
1344 if (old_tab->bins != NULL)
1345 MEMCPY(new_tab->bins, old_tab->bins,
char, bins_size(old_tab));
1358 if (new_tab == NULL)
1362 if (st_replace(new_tab, old_tab) == NULL) {
1363 st_free_table(new_tab);
1373update_range_for_deleted(
st_table *tab, st_index_t n)
1377 if (tab->entries_start == n) {
1378 st_index_t start = n + 1;
1379 st_index_t bound = tab->entries_bound;
1381 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1382 tab->entries_start = start;
1391st_general_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1398 hash = do_hash(*key, tab);
1400 if (tab->bins == NULL) {
1401 bin = find_entry(tab, hash, *key);
1402 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1404 if (bin == UNDEFINED_ENTRY_IND) {
1405 if (value != 0) *value = 0;
1410 bin_ind = find_table_bin_ind(tab, hash, *key);
1411 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1413 if (bin_ind == UNDEFINED_BIN_IND) {
1414 if (value != 0) *value = 0;
1417 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1418 MARK_BIN_DELETED(tab, bin_ind);
1420 entry = &tab->entries[bin];
1422 if (value != 0) *value = entry->record;
1423 MARK_ENTRY_DELETED(entry);
1425 update_range_for_deleted(tab, bin);
1430st_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1432 return st_general_delete(tab, key, value);
1441st_delete_safe(
st_table *tab, st_data_t *key, st_data_t *value,
1442 st_data_t never ATTRIBUTE_UNUSED)
1444 return st_general_delete(tab, key, value);
1452st_shift(
st_table *tab, st_data_t *key, st_data_t *value)
1454 st_index_t i, bound;
1459 entries = tab->entries;
1460 bound = tab->entries_bound;
1461 for (i = tab->entries_start; i < bound; i++) {
1462 curr_entry_ptr = &entries[i];
1463 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1464 st_hash_t entry_hash = curr_entry_ptr->hash;
1465 st_data_t entry_key = curr_entry_ptr->key;
1467 if (value != 0) *value = curr_entry_ptr->record;
1470 if (tab->bins == NULL) {
1471 bin = find_entry(tab, entry_hash, entry_key);
1472 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1473 entries = tab->entries;
1476 curr_entry_ptr = &entries[bin];
1479 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1480 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1481 entries = tab->entries;
1484 curr_entry_ptr = &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind)
1486 MARK_BIN_DELETED(tab, bin_ind);
1488 MARK_ENTRY_DELETED(curr_entry_ptr);
1490 update_range_for_deleted(tab, i);
1494 if (value != 0) *value = 0;
1500st_cleanup_safe(
st_table *tab ATTRIBUTE_UNUSED,
1501 st_data_t never ATTRIBUTE_UNUSED)
1515st_update(
st_table *tab, st_data_t key,
1516 st_update_callback_func *func, st_data_t arg)
1522 st_data_t value = 0, old_key;
1523 int retval, existing;
1524 st_hash_t hash = do_hash(key, tab);
1527 entries = tab->entries;
1528 if (tab->bins == NULL) {
1529 bin = find_entry(tab, hash, key);
1530 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1532 existing = bin != UNDEFINED_ENTRY_IND;
1533 entry = &entries[bin];
1534 bin_ind = UNDEFINED_BIN_IND;
1537 bin_ind = find_table_bin_ind(tab, hash, key);
1538 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1540 existing = bin_ind != UNDEFINED_BIN_IND;
1542 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1543 entry = &entries[bin];
1548 value = entry->record;
1552 unsigned int rebuilds_num = tab->rebuilds_num;
1554 retval = (*func)(&key, &value, arg, existing);
1558 assert(rebuilds_num == tab->rebuilds_num);
1564 st_add_direct_with_hash(tab, key, value, hash);
1567 if (old_key != key) {
1570 entry->record = value;
1574 if (bin_ind != UNDEFINED_BIN_IND)
1575 MARK_BIN_DELETED(tab, bin_ind);
1576 MARK_ENTRY_DELETED(entry);
1578 update_range_for_deleted(tab, bin);
1594st_general_foreach(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1600 enum st_retval retval;
1601 st_index_t i, rebuilds_num;
1604 int error_p, packed_p = tab->bins == NULL;
1606 entries = tab->entries;
1609 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1610 curr_entry_ptr = &entries[i];
1611 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1613 key = curr_entry_ptr->key;
1614 rebuilds_num = tab->rebuilds_num;
1615 hash = curr_entry_ptr->hash;
1616 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1618 if (retval == ST_REPLACE && replace) {
1620 value = curr_entry_ptr->record;
1621 retval = (*replace)(&key, &value, arg, TRUE);
1622 curr_entry_ptr->key = key;
1623 curr_entry_ptr->record = value;
1626 if (rebuilds_num != tab->rebuilds_num) {
1628 entries = tab->entries;
1629 packed_p = tab->bins == NULL;
1631 i = find_entry(tab, hash, key);
1632 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1634 error_p = i == UNDEFINED_ENTRY_IND;
1637 i = find_table_entry_ind(tab, hash, key);
1638 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1640 error_p = i == UNDEFINED_ENTRY_IND;
1643 if (error_p && check_p) {
1645 retval = (*func)(0, 0, arg, 1);
1648 curr_entry_ptr = &entries[i];
1661 st_data_t key = curr_entry_ptr->key;
1665 bin = find_entry(tab, hash, key);
1666 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1668 if (bin == UNDEFINED_ENTRY_IND)
1672 bin_ind = find_table_bin_ind(tab, hash, key);
1673 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1675 if (bin_ind == UNDEFINED_BIN_IND)
1677 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1678 MARK_BIN_DELETED(tab, bin_ind);
1680 curr_entry_ptr = &entries[bin];
1681 MARK_ENTRY_DELETED(curr_entry_ptr);
1683 update_range_for_deleted(tab, bin);
1692st_foreach_with_replace(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1694 return st_general_foreach(tab, func, replace, arg, TRUE);
1698 st_foreach_callback_func *func;
1703apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1705 const struct functor *f = (
void *)d;
1706 return f->func(k, v, f->arg);
1710st_foreach(
st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1712 const struct functor f = { func, arg };
1713 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1718st_foreach_check(
st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1719 st_data_t never ATTRIBUTE_UNUSED)
1721 return st_general_foreach(tab, func, 0, arg, TRUE);
1726static inline st_index_t
1727st_general_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1729 st_index_t i, bound;
1730 st_data_t key, *keys_start, *keys_end;
1733 bound = tab->entries_bound;
1735 keys_end = keys + size;
1736 for (i = tab->entries_start; i < bound; i++) {
1737 if (keys == keys_end)
1739 curr_entry_ptr = &entries[i];
1740 key = curr_entry_ptr->key;
1741 if (! DELETED_ENTRY_P(curr_entry_ptr))
1745 return keys - keys_start;
1749st_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1751 return st_general_keys(tab, keys, size);
1756st_keys_check(
st_table *tab, st_data_t *keys, st_index_t size,
1757 st_data_t never ATTRIBUTE_UNUSED)
1759 return st_general_keys(tab, keys, size);
1764static inline st_index_t
1765st_general_values(
st_table *tab, st_data_t *values, st_index_t size)
1767 st_index_t i, bound;
1768 st_data_t *values_start, *values_end;
1771 values_start = values;
1772 values_end = values + size;
1773 bound = tab->entries_bound;
1774 for (i = tab->entries_start; i < bound; i++) {
1775 if (values == values_end)
1777 curr_entry_ptr = &entries[i];
1778 if (! DELETED_ENTRY_P(curr_entry_ptr))
1779 *values++ = curr_entry_ptr->record;
1782 return values - values_start;
1786st_values(
st_table *tab, st_data_t *values, st_index_t size)
1788 return st_general_values(tab, values, size);
1793st_values_check(
st_table *tab, st_data_t *values, st_index_t size,
1794 st_data_t never ATTRIBUTE_UNUSED)
1796 return st_general_values(tab, values, size);
1799#define FNV1_32A_INIT 0x811c9dc5
1804#define FNV_32_PRIME 0x01000193
1807#ifndef UNALIGNED_WORD_ACCESS
1808# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1809 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1810 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1811 defined(__mc68020__)
1812# define UNALIGNED_WORD_ACCESS 1
1815#ifndef UNALIGNED_WORD_ACCESS
1816# define UNALIGNED_WORD_ACCESS 0
1822#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1823#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1825#if ST_INDEX_BITS <= 32
1826#define C1 (st_index_t)0xcc9e2d51
1827#define C2 (st_index_t)0x1b873593
1829#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1830#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1832NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1833NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1834NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1836static inline st_index_t
1837murmur_step(st_index_t h, st_index_t k)
1839#if ST_INDEX_BITS <= 32
1855static inline st_index_t
1856murmur_finish(st_index_t h)
1858#if ST_INDEX_BITS <= 32
1862 const st_index_t c1 = 0x85ebca6b;
1863 const st_index_t c2 = 0xc2b2ae35;
1869 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1870 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1872#if ST_INDEX_BITS > 64
1889st_hash(
const void *ptr,
size_t len, st_index_t h)
1891 const char *data = ptr;
1895#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1896#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1897#if SIZEOF_ST_INDEX_T > 4
1898#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1899#if SIZEOF_ST_INDEX_T > 8
1900#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1901 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1902#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1904#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1906#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1909 if (
len >=
sizeof(st_index_t)) {
1910#if !UNALIGNED_WORD_ACCESS
1911 int align = (int)((st_data_t)data %
sizeof(st_index_t));
1917#ifdef WORDS_BIGENDIAN
1918# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1919 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1921# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1922 t |= data_at(n) << CHAR_BIT*(n)
1928#ifdef WORDS_BIGENDIAN
1929 t >>= (CHAR_BIT * align) - CHAR_BIT;
1931 t <<= (CHAR_BIT * align);
1934 data +=
sizeof(st_index_t)-align;
1935 len -=
sizeof(st_index_t)-align;
1937 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1938 sr = CHAR_BIT * align;
1940 while (
len >=
sizeof(st_index_t)) {
1941 d = *(st_index_t *)data;
1942#ifdef WORDS_BIGENDIAN
1943 t = (t << sr) | (d >> sl);
1945 t = (t >> sr) | (d << sl);
1947 h = murmur_step(h, t);
1949 data +=
sizeof(st_index_t);
1950 len -=
sizeof(st_index_t);
1953 pack =
len < (size_t)align ? (
int)
len : align;
1956#ifdef WORDS_BIGENDIAN
1957# define UNALIGNED_ADD(n) case (n) + 1: \
1958 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1960# define UNALIGNED_ADD(n) case (n) + 1: \
1961 d |= data_at(n) << CHAR_BIT*(n)
1966#ifdef WORDS_BIGENDIAN
1967 t = (t << sr) | (d >> sl);
1969 t = (t >> sr) | (d << sl);
1972 if (
len < (
size_t)align)
goto skip_tail;
1974 h = murmur_step(h, t);
1980#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
1981#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
1983#define aligned_data data
1987 h = murmur_step(h, *(st_index_t *)aligned_data);
1988 data +=
sizeof(st_index_t);
1989 len -=
sizeof(st_index_t);
1990 }
while (
len >=
sizeof(st_index_t));
1996#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
1998#if SIZEOF_ST_INDEX_T > 4
1999 case 7: t |= data_at(6) << 48;
2000 case 6: t |= data_at(5) << 40;
2001 case 5: t |= data_at(4) << 32;
2003 t |= (st_index_t)*(uint32_t*)aligned_data;
2007 case 3: t |= data_at(2) << 16;
2008 case 2: t |= data_at(1) << 8;
2009 case 1: t |= data_at(0);
2011#ifdef WORDS_BIGENDIAN
2012# define UNALIGNED_ADD(n) case (n) + 1: \
2013 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
2015# define UNALIGNED_ADD(n) case (n) + 1: \
2016 t |= data_at(n) << CHAR_BIT*(n)
2024 h ^= t; h -= ROTL(t, 7);
2030 return murmur_finish(h);
2034st_hash_uint32(st_index_t h, uint32_t i)
2036 return murmur_step(h, i);
2039NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
2041st_hash_uint(st_index_t h, st_index_t i)
2046#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
2047 h = murmur_step(h, i >> 8*8);
2049 h = murmur_step(h, i);
2054st_hash_end(st_index_t h)
2056 h = murmur_finish(h);
2062rb_st_hash_start(st_index_t h)
2068strhash(st_data_t arg)
2070 register const char *
string = (
const char *)arg;
2071 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
2075st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
2082 if (c1 ==
'\0' || c2 ==
'\0') {
2083 if (c1 !=
'\0')
return 1;
2084 if (c2 !=
'\0')
return -1;
2087 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2088 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2099st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
2104 for (i = 0; i < n; i++) {
2107 if (c1 ==
'\0' || c2 ==
'\0') {
2108 if (c1 !=
'\0')
return 1;
2109 if (c2 !=
'\0')
return -1;
2112 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2113 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2125st_strcmp(st_data_t lhs, st_data_t rhs)
2127 const char *s1 = (
char *)lhs;
2128 const char *s2 = (
char *)rhs;
2129 return strcmp(s1, s2);
2133st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2135 const char *s1 = (
char *)lhs;
2136 const char *s2 = (
char *)rhs;
2137 return st_locale_insensitive_strcasecmp(s1, s2);
2140NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2142strcasehash(st_data_t arg)
2144 register const char *
string = (
const char *)arg;
2145 register st_index_t hval = FNV1_32A_INIT;
2151 unsigned int c = (
unsigned char)*
string++;
2152 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2156 hval *= FNV_32_PRIME;
2162st_numcmp(st_data_t x, st_data_t y)
2168st_numhash(st_data_t n)
2170 enum {s1 = 11, s2 = 3};
2171 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2179st_expand_table(
st_table *tab, st_index_t siz)
2184 if (siz <= get_allocated_entries(tab))
2187 tmp = st_init_table_with_size(tab->type, siz);
2188 n = get_allocated_entries(tab);
2191 st_free_entries(tab);
2194 tab->entry_power = tmp->entry_power;
2195 tab->bin_power = tmp->bin_power;
2196 tab->size_ind = tmp->size_ind;
2197 tab->entries = tmp->entries;
2199 tab->rebuilds_num++;
2200 free_fixed_ptr(tmp);
2208 int eq_p, rebuilt_p;
2215 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2216 p = &tab->entries[i];
2217 if (DELETED_ENTRY_P(p))
2219 for (j = i + 1; j < tab->entries_bound; j++) {
2220 q = &tab->entries[j];
2221 if (DELETED_ENTRY_P(q))
2223 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2224 if (EXPECT(rebuilt_p, 0))
2228 MARK_ENTRY_DELETED(q);
2230 update_range_for_deleted(tab, j);
2242 int eq_p, rebuilt_p;
2246 tab->bins = malloc(bins_size(tab));
2248 unsigned int const size_ind = get_size_ind(tab);
2249 initialize_bins(tab);
2250 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2253#ifdef QUADRATIC_PROBE
2256 st_index_t perturb = p->hash;
2259 if (DELETED_ENTRY_P(p))
2262 ind = hash_bin(p->hash, tab);
2264 st_index_t bin = get_bin(tab->bins, size_ind, ind);
2265 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2267 set_bin(tab->bins, size_ind, ind, i + ENTRY_BASE);
2272 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2273 if (EXPECT(rebuilt_p, 0))
2277 q->record = p->record;
2278 MARK_ENTRY_DELETED(p);
2280 update_range_for_deleted(tab, bin);
2285#ifdef QUADRATIC_PROBE
2286 ind = hash_bin(ind + d, tab);
2289 ind = secondary_hash(ind, tab, &perturb);
2307 if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2308 rebuilt_p = st_rehash_linear(tab);
2310 rebuilt_p = st_rehash_indexed(tab);
2311 }
while (rebuilt_p);
2315st_stringify(
VALUE key)
2318 rb_hash_key_str(key) : key;
2324 st_data_t k = st_stringify(key);
2326 e.hash = do_hash(k, tab);
2330 tab->entries[tab->entries_bound++] = e;
2341 for (i = 0; i < argc; ) {
2342 st_data_t k = st_stringify(argv[i++]);
2343 st_data_t v = argv[i++];
2344 st_insert(tab, k, v);
2356 for (i = 0; i < argc; ) {
2357 VALUE key = argv[i++];
2358 VALUE val = argv[i++];
2359 st_insert_single(tab, hash, key, val);
2369rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2371 st_index_t n, size = argc / 2;
2372 st_table *tab = RHASH_ST_TABLE(hash);
2374 tab = RHASH_TBL_RAW(hash);
2375 n = tab->entries_bound + size;
2376 st_expand_table(tab, n);
2377 if (UNLIKELY(tab->num_entries))
2378 st_insert_generic(tab, argc, argv, hash);
2380 st_insert_single(tab, hash, argv[0], argv[1]);
2381 else if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2382 st_insert_linear(tab, argc, argv, hash);
2384 st_insert_generic(tab, argc, argv, hash);
2390 st_index_t num = tab->num_entries;
2391 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2393 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2394 rebuild_table_with(new_tab, tab);
2395 rebuild_move_table(new_tab, tab);
2396 rebuild_cleanup(tab);
2404struct set_table_entry {
2410static inline st_hash_t
2411set_do_hash(st_data_t key,
set_table *tab)
2413 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
2414 return normalize_hash_value(hash);
2418static inline unsigned int
2421 return tab->size_ind;
2425static inline st_index_t
2428 return ((st_index_t) 1)<<tab->bin_power;
2432static inline st_index_t
2435 return set_get_bins_num(tab) - 1;
2440static inline st_index_t
2441set_hash_bin(st_hash_t hash_value,
set_table *tab)
2443 return hash_value & set_bins_mask(tab);
2447static inline st_index_t
2448set_get_allocated_entries(
const set_table *tab)
2450 return ((st_index_t) 1)<<tab->entry_power;
2454set_allocated_entries_size(
const set_table *tab)
2456 return set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2462 return tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS;
2466static inline st_index_t
2469 if (set_has_bins(tab)) {
2470 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
2476static inline st_index_t *
2479 if (set_has_bins(tab)) {
2480 return (st_index_t *)(((
char *)tab->
entries) + set_allocated_entries_size(tab));
2490 memset(set_bins_ptr(tab), 0, set_bins_size(tab));
2497 tab->num_entries = 0;
2498 tab->entries_start = tab->entries_bound = 0;
2499 if (set_bins_ptr(tab) != NULL)
2500 set_initialize_bins(tab);
2506 size_t memsize = set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2507 if (set_has_bins(tab)) {
2508 memsize += set_bins_size(tab);
2521 const char *e = getenv(
"ST_HASH_LOG");
2522 if (!e || !*e) init_st = 1;
2531 n = get_power2(size);
2534 tab->entry_power = n;
2535 tab->bin_power = features[n].bin_power;
2536 tab->size_ind = features[n].size_ind;
2538 tab->
entries = (set_table_entry *)malloc(set_entries_memsize(tab));
2539 set_make_tab_empty(tab);
2540 tab->rebuilds_num = 0;
2550 if (tab == NULL) tab = malloc(
sizeof(
set_table));
2552 set_init_existing_table_with_size(tab, type, size);
2558set_init_numtable(
void)
2560 return set_init_table_with_size(NULL, &type_numhash, 0);
2564set_init_numtable_with_size(st_index_t size)
2566 return set_init_table_with_size(NULL, &type_numhash, size);
2570set_init_embedded_numtable_with_size(
set_table *tab, st_index_t size)
2572 return set_init_existing_table_with_size(tab, &type_numhash, size);
2576set_table_size(
const struct set_table *tbl)
2578 return tbl->num_entries;
2585 set_make_tab_empty(tab);
2586 tab->rebuilds_num++;
2592 sized_free(tab->
entries, set_entries_memsize(tab));
2600 set_free_embedded_table(tab);
2601 free_fixed_ptr(tab);
2609 + (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS ? 0 : set_bins_size(tab))
2610 + set_get_allocated_entries(tab) * sizeof(set_table_entry));
2614set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2617set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2620set_find_table_bin_ind_direct(
set_table *table, st_hash_t hash_value, st_data_t key);
2623set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2624 st_data_t key, st_index_t *bin_ind);
2628static void set_rebuild_cleanup(
set_table *
const tab);
2637 if ((2 * tab->num_entries <= set_get_allocated_entries(tab)
2638 && REBUILD_THRESHOLD * tab->num_entries > set_get_allocated_entries(tab))
2639 || tab->num_entries < (1 << MINIMAL_POWER2)) {
2641 tab->num_entries = 0;
2642 if (set_has_bins(tab))
2643 set_initialize_bins(tab);
2644 set_rebuild_table_with(tab, tab);
2651 new_tab = set_init_table_with_size(NULL, tab->type,
2652 2 * tab->num_entries - 1);
2653 set_rebuild_table_with(new_tab, tab);
2654 set_rebuild_move_table(new_tab, tab);
2656 set_rebuild_cleanup(tab);
2663 unsigned int size_ind;
2664 set_table_entry *new_entries;
2665 set_table_entry *curr_entry_ptr;
2669 new_entries = new_tab->
entries;
2672 bins = set_bins_ptr(new_tab);
2673 size_ind = set_get_size_ind(new_tab);
2674 st_index_t bound = tab->entries_bound;
2675 set_table_entry *entries = tab->
entries;
2677 for (i = tab->entries_start; i < bound; i++) {
2678 curr_entry_ptr = &entries[i];
2679 PREFETCH(entries + i + 1, 0);
2680 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
2682 if (&new_entries[ni] != curr_entry_ptr)
2683 new_entries[ni] = *curr_entry_ptr;
2684 if (EXPECT(bins != NULL, 1)) {
2685 bin_ind = set_find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
2686 curr_entry_ptr->key);
2687 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
2689 new_tab->num_entries++;
2693 assert(new_tab->num_entries == tab->num_entries);
2699 sized_free(tab->
entries, set_entries_memsize(tab));
2702 tab->entry_power = new_tab->entry_power;
2703 tab->bin_power = new_tab->bin_power;
2704 tab->size_ind = new_tab->size_ind;
2706 free_fixed_ptr(new_tab);
2710set_rebuild_cleanup(
set_table *
const tab)
2712 tab->entries_start = 0;
2713 tab->entries_bound = tab->num_entries;
2714 tab->rebuilds_num++;
2729static inline st_index_t
2730set_secondary_hash(st_index_t ind,
set_table *tab, st_index_t *perturb)
2733 ind = (ind << 2) + ind + *perturb + 1;
2734 return set_hash_bin(ind, tab);
2741static inline st_index_t
2742set_find_entry(
set_table *tab, st_hash_t hash_value, st_data_t key)
2744 int eq_p, rebuilt_p;
2745 st_index_t i, bound;
2746 set_table_entry *entries;
2748 bound = tab->entries_bound;
2750 for (i = tab->entries_start; i < bound; i++) {
2751 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
2752 if (EXPECT(rebuilt_p, 0))
2753 return REBUILT_TABLE_ENTRY_IND;
2757 return UNDEFINED_ENTRY_IND;
2769set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2771 int eq_p, rebuilt_p;
2773#ifdef QUADRATIC_PROBE
2779 set_table_entry *entries = tab->
entries;
2781 ind = set_hash_bin(hash_value, tab);
2782#ifdef QUADRATIC_PROBE
2785 perturb = hash_value;
2788 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2789 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2790 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2791 if (EXPECT(rebuilt_p, 0))
2792 return REBUILT_TABLE_ENTRY_IND;
2796 else if (EMPTY_BIN_P(bin))
2797 return UNDEFINED_ENTRY_IND;
2798#ifdef QUADRATIC_PROBE
2799 ind = set_hash_bin(ind + d, tab);
2802 ind = set_secondary_hash(ind, tab, &perturb);
2813set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2815 int eq_p, rebuilt_p;
2817#ifdef QUADRATIC_PROBE
2823 set_table_entry *entries = tab->
entries;
2825 ind = set_hash_bin(hash_value, tab);
2826#ifdef QUADRATIC_PROBE
2829 perturb = hash_value;
2832 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2833 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2834 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2835 if (EXPECT(rebuilt_p, 0))
2836 return REBUILT_TABLE_BIN_IND;
2840 else if (EMPTY_BIN_P(bin))
2841 return UNDEFINED_BIN_IND;
2842#ifdef QUADRATIC_PROBE
2843 ind = set_hash_bin(ind + d, tab);
2846 ind = set_secondary_hash(ind, tab, &perturb);
2856set_find_table_bin_ind_direct(
set_table *tab, st_hash_t hash_value, st_data_t key)
2859#ifdef QUADRATIC_PROBE
2866 ind = set_hash_bin(hash_value, tab);
2867#ifdef QUADRATIC_PROBE
2870 perturb = hash_value;
2873 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2874 if (EMPTY_OR_DELETED_BIN_P(bin))
2876#ifdef QUADRATIC_PROBE
2877 ind = set_hash_bin(ind + d, tab);
2880 ind = set_secondary_hash(ind, tab, &perturb);
2887#define MARK_SET_BIN_EMPTY(tab, i) (set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, EMPTY_BIN))
2899set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2900 st_data_t key, st_index_t *bin_ind)
2902 int eq_p, rebuilt_p;
2904 st_hash_t curr_hash_value = *hash_value;
2905#ifdef QUADRATIC_PROBE
2910 st_index_t entry_index;
2911 st_index_t firset_deleted_bin_ind;
2912 set_table_entry *entries;
2914 ind = set_hash_bin(curr_hash_value, tab);
2915#ifdef QUADRATIC_PROBE
2918 perturb = curr_hash_value;
2920 firset_deleted_bin_ind = UNDEFINED_BIN_IND;
2923 entry_index = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2924 if (EMPTY_BIN_P(entry_index)) {
2926 entry_index = UNDEFINED_ENTRY_IND;
2927 if (firset_deleted_bin_ind != UNDEFINED_BIN_IND) {
2929 ind = firset_deleted_bin_ind;
2930 MARK_SET_BIN_EMPTY(tab, ind);
2934 else if (! DELETED_BIN_P(entry_index)) {
2935 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
2936 if (EXPECT(rebuilt_p, 0))
2937 return REBUILT_TABLE_ENTRY_IND;
2941 else if (firset_deleted_bin_ind == UNDEFINED_BIN_IND)
2942 firset_deleted_bin_ind = ind;
2943#ifdef QUADRATIC_PROBE
2944 ind = set_hash_bin(ind + d, tab);
2947 ind = set_secondary_hash(ind, tab, &perturb);
2957set_table_lookup(
set_table *tab, st_data_t key)
2960 st_hash_t hash = set_do_hash(key, tab);
2963 if (!set_has_bins(tab)) {
2964 bin = set_find_entry(tab, hash, key);
2965 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2967 if (bin == UNDEFINED_ENTRY_IND)
2971 bin = set_find_table_entry_ind(tab, hash, key);
2972 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2974 if (bin == UNDEFINED_ENTRY_IND)
2983set_rebuild_table_if_necessary (
set_table *tab)
2985 st_index_t bound = tab->entries_bound;
2987 if (bound == set_get_allocated_entries(tab))
2988 set_rebuild_table(tab);
2995set_insert(
set_table *tab, st_data_t key)
2997 set_table_entry *entry;
3000 st_hash_t hash_value;
3004 hash_value = set_do_hash(key, tab);
3006 set_rebuild_table_if_necessary(tab);
3007 if (!set_has_bins(tab)) {
3008 bin = set_find_entry(tab, hash_value, key);
3009 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3011 new_p = bin == UNDEFINED_ENTRY_IND;
3014 bin_ind = UNDEFINED_BIN_IND;
3017 bin = set_find_table_bin_ptr_and_reserve(tab, &hash_value,
3019 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3021 new_p = bin == UNDEFINED_ENTRY_IND;
3025 ind = tab->entries_bound++;
3027 entry->hash = hash_value;
3029 if (bin_ind != UNDEFINED_BIN_IND)
3030 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
3040 *new_tab = *old_tab;
3041 size_t memsize = set_allocated_entries_size(old_tab) + set_bins_size(old_tab);
3042 new_tab->
entries = (set_table_entry *)malloc(memsize);
3053 if (set_replace(new_tab, old_tab) == NULL) {
3054 set_free_table(new_tab);
3064set_update_range_for_deleted(
set_table *tab, st_index_t n)
3068 if (tab->entries_start == n) {
3069 st_index_t start = n + 1;
3070 st_index_t bound = tab->entries_bound;
3071 set_table_entry *entries = tab->
entries;
3072 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
3073 tab->entries_start = start;
3080#define MARK_SET_BIN_DELETED(tab, i) \
3082 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, DELETED_BIN); \
3088set_table_delete(
set_table *tab, st_data_t *key)
3090 set_table_entry *entry;
3095 hash = set_do_hash(*key, tab);
3097 if (!set_has_bins(tab)) {
3098 bin = set_find_entry(tab, hash, *key);
3099 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3101 if (bin == UNDEFINED_ENTRY_IND) {
3106 bin_ind = set_find_table_bin_ind(tab, hash, *key);
3107 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3109 if (bin_ind == UNDEFINED_BIN_IND) {
3112 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3113 MARK_SET_BIN_DELETED(tab, bin_ind);
3117 MARK_ENTRY_DELETED(entry);
3119 set_update_range_for_deleted(tab, bin);
3132set_general_foreach(
set_table *tab, set_foreach_check_callback_func *func,
3133 set_update_callback_func *replace, st_data_t arg,
3138 set_table_entry *entries, *curr_entry_ptr;
3139 enum st_retval retval;
3140 st_index_t i, rebuilds_num;
3143 int error_p, packed_p = !set_has_bins(tab);
3148 for (i = tab->entries_start; i < tab->entries_bound; i++) {
3149 curr_entry_ptr = &entries[i];
3150 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
3152 key = curr_entry_ptr->key;
3153 rebuilds_num = tab->rebuilds_num;
3154 hash = curr_entry_ptr->hash;
3155 retval = (*func)(key, arg, 0);
3157 if (retval == ST_REPLACE && replace) {
3158 retval = (*replace)(&key, arg, TRUE);
3159 curr_entry_ptr->key = key;
3162 if (rebuilds_num != tab->rebuilds_num) {
3165 packed_p = !set_has_bins(tab);
3167 i = set_find_entry(tab, hash, key);
3168 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3170 error_p = i == UNDEFINED_ENTRY_IND;
3173 i = set_find_table_entry_ind(tab, hash, key);
3174 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3176 error_p = i == UNDEFINED_ENTRY_IND;
3179 if (error_p && check_p) {
3181 retval = (*func)(0, arg, 1);
3184 curr_entry_ptr = &entries[i];
3197 st_data_t key = curr_entry_ptr->key;
3201 bin = set_find_entry(tab, hash, key);
3202 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3204 if (bin == UNDEFINED_ENTRY_IND)
3208 bin_ind = set_find_table_bin_ind(tab, hash, key);
3209 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3211 if (bin_ind == UNDEFINED_BIN_IND)
3213 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3214 MARK_SET_BIN_DELETED(tab, bin_ind);
3216 curr_entry_ptr = &entries[bin];
3217 MARK_ENTRY_DELETED(curr_entry_ptr);
3219 set_update_range_for_deleted(tab, bin);
3228set_foreach_with_replace(
set_table *tab, set_foreach_check_callback_func *func, set_update_callback_func *replace, st_data_t arg)
3230 return set_general_foreach(tab, func, replace, arg, TRUE);
3234 set_foreach_callback_func *func;
3239set_apply_functor(st_data_t k, st_data_t d,
int _)
3241 const struct set_functor *f = (
void *)d;
3242 return f->func(k, f->arg);
3246set_table_foreach(
set_table *tab, set_foreach_callback_func *func, st_data_t arg)
3248 const struct set_functor f = { func, arg };
3249 return set_general_foreach(tab, set_apply_functor, NULL, (st_data_t)&f, FALSE);
3254set_foreach_check(
set_table *tab, set_foreach_check_callback_func *func, st_data_t arg,
3255 st_data_t never ATTRIBUTE_UNUSED)
3257 return set_general_foreach(tab, func, NULL, arg, TRUE);
3263set_keys(
set_table *tab, st_data_t *keys, st_index_t size)
3265 st_index_t i, bound;
3266 st_data_t key, *keys_start, *keys_end;
3267 set_table_entry *curr_entry_ptr, *entries = tab->
entries;
3269 bound = tab->entries_bound;
3271 keys_end = keys + size;
3272 for (i = tab->entries_start; i < bound; i++) {
3273 if (keys == keys_end)
3275 curr_entry_ptr = &entries[i];
3276 key = curr_entry_ptr->key;
3277 if (! DELETED_ENTRY_P(curr_entry_ptr))
3281 return keys - keys_start;
3287 st_index_t num = tab->num_entries;
3288 if (REBUILD_THRESHOLD * num <= set_get_allocated_entries(tab)) {
3290 set_table *new_tab = set_init_table_with_size(NULL, tab->type, 2 * num);
3291 set_rebuild_table_with(new_tab, tab);
3292 set_rebuild_move_table(new_tab, tab);
3293 set_rebuild_cleanup(tab);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
#define Qundef
Old name of RUBY_Qundef.
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.
int len
Length of the buffer.
#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.
set_table_entry * entries
Array of size 2^entry_power.
uintptr_t VALUE
Type that represents a Ruby object.