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_sized_xrealloc
178#define free ruby_xfree
179#define sized_free ruby_sized_xfree
180#define free_fixed_ptr(v) ruby_sized_xfree((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;
584st_init_table_with_size(
const struct st_hash_type *type, st_index_t size)
593 st_init_existing_table_with_size(tab, type, size);
595 if (st_init_existing_table_with_size(tab, type, size) == NULL) {
605st_table_size(
const struct st_table *tbl)
607 return tbl->num_entries;
615 return st_init_table_with_size(type, 0);
621st_init_numtable(
void)
623 return st_init_table(&type_numhash);
628st_init_numtable_with_size(st_index_t size)
630 return st_init_table_with_size(&type_numhash, size);
636st_init_strtable(
void)
638 return st_init_table(&type_strhash);
643st_init_strtable_with_size(st_index_t size)
645 return st_init_table_with_size(&type_strhash, size);
651st_init_strcasetable(
void)
653 return st_init_table(&type_strcasehash);
659st_init_strcasetable_with_size(st_index_t size)
661 return st_init_table_with_size(&type_strcasehash, size);
673st_entries_memsize(
const st_table *tab)
681 return tab->bins == NULL ? 0 : bins_size(tab);
687 sized_free(tab->entries, st_entries_memsize(tab));
693 sized_free(tab->bins, st_bins_memsize(tab));
697st_free_embedded_table(
st_table *tab)
700 st_free_entries(tab);
707 st_free_embedded_table(tab);
717 + st_bins_memsize(tab)
718 + st_entries_memsize(tab));
722find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
725find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
728find_table_bin_ind_direct(
st_table *table, st_hash_t hash_value, st_data_t key);
731find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
732 st_data_t key, st_index_t *bin_ind);
739 if (type == &type_numhash) {
742 else if (type == &type_strhash) {
745 else if (type == &type_strcasehash) {
750#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
751#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
752#define collision_check 0
761#define REBUILD_THRESHOLD 4
763#if REBUILD_THRESHOLD < 2
764#error "REBUILD_THRESHOLD should be >= 2"
767static void rebuild_table_with(
st_table *
const new_tab,
st_table *
const tab);
768static void rebuild_move_table(
st_table *
const new_tab,
st_table *
const tab);
769static void rebuild_cleanup(
st_table *
const tab);
778 if ((2 * tab->num_entries <= get_allocated_entries(tab)
779 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
780 || tab->num_entries < (1 << MINIMAL_POWER2)) {
782 tab->num_entries = 0;
783 if (tab->bins != NULL)
784 initialize_bins(tab);
785 rebuild_table_with(tab, tab);
792 new_tab = st_init_table_with_size(tab->type,
793 2 * tab->num_entries - 1);
794 rebuild_table_with(new_tab, tab);
795 rebuild_move_table(new_tab, tab);
797 rebuild_cleanup(tab);
804 unsigned int size_ind;
810 new_entries = new_tab->entries;
813 bins = new_tab->bins;
814 size_ind = get_size_ind(new_tab);
815 st_index_t bound = tab->entries_bound;
818 for (i = tab->entries_start; i < bound; i++) {
819 curr_entry_ptr = &entries[i];
820 PREFETCH(entries + i + 1, 0);
821 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
823 if (&new_entries[ni] != curr_entry_ptr)
824 new_entries[ni] = *curr_entry_ptr;
825 if (EXPECT(bins != NULL, 1)) {
826 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
827 curr_entry_ptr->key);
828 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
830 new_tab->num_entries++;
834 assert(new_tab->num_entries == tab->num_entries);
841 st_free_entries(tab);
843 tab->entry_power = new_tab->entry_power;
844 tab->bin_power = new_tab->bin_power;
845 tab->size_ind = new_tab->size_ind;
846 tab->bins = new_tab->bins;
847 tab->entries = new_tab->entries;
848 free_fixed_ptr(new_tab);
854 tab->entries_start = 0;
855 tab->entries_bound = tab->num_entries;
871static inline st_index_t
872secondary_hash(st_index_t ind,
st_table *tab, st_index_t *perturb)
875 ind = (ind << 2) + ind + *perturb + 1;
876 return hash_bin(ind, tab);
883static inline st_index_t
884find_entry(
st_table *tab, st_hash_t hash_value, st_data_t key)
890 bound = tab->entries_bound;
891 entries = tab->entries;
892 for (i = tab->entries_start; i < bound; i++) {
893 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
894 if (EXPECT(rebuilt_p, 0))
895 return REBUILT_TABLE_ENTRY_IND;
899 return UNDEFINED_ENTRY_IND;
911find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
915#ifdef QUADRATIC_PROBE
923 ind = hash_bin(hash_value, tab);
924#ifdef QUADRATIC_PROBE
927 perturb = hash_value;
931 bin = get_bin(tab->bins, get_size_ind(tab), ind);
932 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
933 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
934 if (EXPECT(rebuilt_p, 0))
935 return REBUILT_TABLE_ENTRY_IND;
939 else if (EMPTY_BIN_P(bin))
940 return UNDEFINED_ENTRY_IND;
941#ifdef QUADRATIC_PROBE
942 ind = hash_bin(ind + d, tab);
945 ind = secondary_hash(ind, tab, &perturb);
957find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
961#ifdef QUADRATIC_PROBE
969 ind = hash_bin(hash_value, tab);
970#ifdef QUADRATIC_PROBE
973 perturb = hash_value;
977 bin = get_bin(tab->bins, get_size_ind(tab), ind);
978 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
979 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
980 if (EXPECT(rebuilt_p, 0))
981 return REBUILT_TABLE_BIN_IND;
985 else if (EMPTY_BIN_P(bin))
986 return UNDEFINED_BIN_IND;
987#ifdef QUADRATIC_PROBE
988 ind = hash_bin(ind + d, tab);
991 ind = secondary_hash(ind, tab, &perturb);
1002find_table_bin_ind_direct(
st_table *tab, st_hash_t hash_value, st_data_t key)
1005#ifdef QUADRATIC_PROBE
1012 ind = hash_bin(hash_value, tab);
1013#ifdef QUADRATIC_PROBE
1016 perturb = hash_value;
1020 bin = get_bin(tab->bins, get_size_ind(tab), ind);
1021 if (EMPTY_OR_DELETED_BIN_P(bin))
1023#ifdef QUADRATIC_PROBE
1024 ind = hash_bin(ind + d, tab);
1027 ind = secondary_hash(ind, tab, &perturb);
1043find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
1044 st_data_t key, st_index_t *bin_ind)
1046 int eq_p, rebuilt_p;
1048 st_hash_t curr_hash_value = *hash_value;
1049#ifdef QUADRATIC_PROBE
1054 st_index_t entry_index;
1055 st_index_t first_deleted_bin_ind;
1058 ind = hash_bin(curr_hash_value, tab);
1059#ifdef QUADRATIC_PROBE
1062 perturb = curr_hash_value;
1065 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1066 entries = tab->entries;
1068 entry_index = get_bin(tab->bins, get_size_ind(tab), ind);
1069 if (EMPTY_BIN_P(entry_index)) {
1071 entry_index = UNDEFINED_ENTRY_IND;
1072 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1074 ind = first_deleted_bin_ind;
1075 MARK_BIN_EMPTY(tab, ind);
1079 else if (! DELETED_BIN_P(entry_index)) {
1080 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1081 if (EXPECT(rebuilt_p, 0))
1082 return REBUILT_TABLE_ENTRY_IND;
1086 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1087 first_deleted_bin_ind = ind;
1088#ifdef QUADRATIC_PROBE
1089 ind = hash_bin(ind + d, tab);
1092 ind = secondary_hash(ind, tab, &perturb);
1103st_lookup(
st_table *tab, st_data_t key, st_data_t *value)
1106 st_hash_t hash = do_hash(key, tab);
1109 if (tab->bins == NULL) {
1110 bin = find_entry(tab, hash, key);
1111 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1113 if (bin == UNDEFINED_ENTRY_IND)
1117 bin = find_table_entry_ind(tab, hash, key);
1118 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1120 if (bin == UNDEFINED_ENTRY_IND)
1125 *value = tab->entries[bin].record;
1132st_get_key(
st_table *tab, st_data_t key, st_data_t *result)
1135 st_hash_t hash = do_hash(key, tab);
1138 if (tab->bins == NULL) {
1139 bin = find_entry(tab, hash, key);
1140 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1142 if (bin == UNDEFINED_ENTRY_IND)
1146 bin = find_table_entry_ind(tab, hash, key);
1147 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1149 if (bin == UNDEFINED_ENTRY_IND)
1154 *result = tab->entries[bin].key;
1160rebuild_table_if_necessary (
st_table *tab)
1162 st_index_t bound = tab->entries_bound;
1164 if (bound == get_allocated_entries(tab))
1172st_insert(
st_table *tab, st_data_t key, st_data_t value)
1177 st_hash_t hash_value;
1181 hash_value = do_hash(key, tab);
1183 rebuild_table_if_necessary(tab);
1184 if (tab->bins == NULL) {
1185 bin = find_entry(tab, hash_value, key);
1186 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1188 new_p = bin == UNDEFINED_ENTRY_IND;
1191 bin_ind = UNDEFINED_BIN_IND;
1194 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1196 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1198 new_p = bin == UNDEFINED_ENTRY_IND;
1202 ind = tab->entries_bound++;
1203 entry = &tab->entries[ind];
1204 entry->hash = hash_value;
1206 entry->record = value;
1207 if (bin_ind != UNDEFINED_BIN_IND)
1208 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1211 tab->entries[bin].record = value;
1218st_add_direct_with_hash(
st_table *tab,
1219 st_data_t key, st_data_t value, st_hash_t hash)
1225 assert(hash != RESERVED_HASH_VAL);
1227 rebuild_table_if_necessary(tab);
1228 ind = tab->entries_bound++;
1229 entry = &tab->entries[ind];
1232 entry->record = value;
1234 if (tab->bins != NULL) {
1235 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1236 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1241rb_st_add_direct_with_hash(
st_table *tab,
1242 st_data_t key, st_data_t value, st_hash_t hash)
1244 st_add_direct_with_hash(tab, key, value, normalize_hash_value(hash));
1250st_add_direct(
st_table *tab, st_data_t key, st_data_t value)
1252 st_hash_t hash_value;
1254 hash_value = do_hash(key, tab);
1255 st_add_direct_with_hash(tab, key, value, hash_value);
1262st_insert2(
st_table *tab, st_data_t key, st_data_t value,
1263 st_data_t (*func)(st_data_t))
1268 st_hash_t hash_value;
1272 hash_value = do_hash(key, tab);
1274 rebuild_table_if_necessary (tab);
1275 if (tab->bins == NULL) {
1276 bin = find_entry(tab, hash_value, key);
1277 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1279 new_p = bin == UNDEFINED_ENTRY_IND;
1282 bin_ind = UNDEFINED_BIN_IND;
1285 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1287 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1289 new_p = bin == UNDEFINED_ENTRY_IND;
1294 ind = tab->entries_bound++;
1295 entry = &tab->entries[ind];
1296 entry->hash = hash_value;
1298 entry->record = value;
1299 if (bin_ind != UNDEFINED_BIN_IND)
1300 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1303 tab->entries[bin].record = value;
1311 *new_tab = *old_tab;
1312 if (old_tab->bins == NULL)
1313 new_tab->bins = NULL;
1315 new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
1317 if (new_tab->bins == NULL) {
1322 new_tab->entries = (
st_table_entry *) malloc(get_allocated_entries(old_tab)
1325 if (new_tab->entries == NULL) {
1330 get_allocated_entries(old_tab));
1331 if (old_tab->bins != NULL)
1332 MEMCPY(new_tab->bins, old_tab->bins,
char, bins_size(old_tab));
1345 if (new_tab == NULL)
1349 if (st_replace(new_tab, old_tab) == NULL) {
1350 st_free_table(new_tab);
1360update_range_for_deleted(
st_table *tab, st_index_t n)
1364 if (tab->entries_start == n) {
1365 st_index_t start = n + 1;
1366 st_index_t bound = tab->entries_bound;
1368 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1369 tab->entries_start = start;
1378st_general_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1385 hash = do_hash(*key, tab);
1387 if (tab->bins == NULL) {
1388 bin = find_entry(tab, hash, *key);
1389 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1391 if (bin == UNDEFINED_ENTRY_IND) {
1392 if (value != 0) *value = 0;
1397 bin_ind = find_table_bin_ind(tab, hash, *key);
1398 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1400 if (bin_ind == UNDEFINED_BIN_IND) {
1401 if (value != 0) *value = 0;
1404 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1405 MARK_BIN_DELETED(tab, bin_ind);
1407 entry = &tab->entries[bin];
1409 if (value != 0) *value = entry->record;
1410 MARK_ENTRY_DELETED(entry);
1412 update_range_for_deleted(tab, bin);
1417st_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1419 return st_general_delete(tab, key, value);
1428st_delete_safe(
st_table *tab, st_data_t *key, st_data_t *value,
1429 st_data_t never ATTRIBUTE_UNUSED)
1431 return st_general_delete(tab, key, value);
1439st_shift(
st_table *tab, st_data_t *key, st_data_t *value)
1441 st_index_t i, bound;
1446 entries = tab->entries;
1447 bound = tab->entries_bound;
1448 for (i = tab->entries_start; i < bound; i++) {
1449 curr_entry_ptr = &entries[i];
1450 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1451 st_hash_t entry_hash = curr_entry_ptr->hash;
1452 st_data_t entry_key = curr_entry_ptr->key;
1454 if (value != 0) *value = curr_entry_ptr->record;
1457 if (tab->bins == NULL) {
1458 bin = find_entry(tab, entry_hash, entry_key);
1459 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1460 entries = tab->entries;
1463 curr_entry_ptr = &entries[bin];
1466 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1467 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1468 entries = tab->entries;
1471 curr_entry_ptr = &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind)
1473 MARK_BIN_DELETED(tab, bin_ind);
1475 MARK_ENTRY_DELETED(curr_entry_ptr);
1477 update_range_for_deleted(tab, i);
1481 if (value != 0) *value = 0;
1487st_cleanup_safe(
st_table *tab ATTRIBUTE_UNUSED,
1488 st_data_t never ATTRIBUTE_UNUSED)
1502st_update(
st_table *tab, st_data_t key,
1503 st_update_callback_func *func, st_data_t arg)
1509 st_data_t value = 0, old_key;
1510 int retval, existing;
1511 st_hash_t hash = do_hash(key, tab);
1514 entries = tab->entries;
1515 if (tab->bins == NULL) {
1516 bin = find_entry(tab, hash, key);
1517 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1519 existing = bin != UNDEFINED_ENTRY_IND;
1520 entry = &entries[bin];
1521 bin_ind = UNDEFINED_BIN_IND;
1524 bin_ind = find_table_bin_ind(tab, hash, key);
1525 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1527 existing = bin_ind != UNDEFINED_BIN_IND;
1529 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1530 entry = &entries[bin];
1535 value = entry->record;
1539 unsigned int rebuilds_num = tab->rebuilds_num;
1541 retval = (*func)(&key, &value, arg, existing);
1545 assert(rebuilds_num == tab->rebuilds_num);
1551 st_add_direct_with_hash(tab, key, value, hash);
1554 if (old_key != key) {
1557 entry->record = value;
1561 if (bin_ind != UNDEFINED_BIN_IND)
1562 MARK_BIN_DELETED(tab, bin_ind);
1563 MARK_ENTRY_DELETED(entry);
1565 update_range_for_deleted(tab, bin);
1581st_general_foreach(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1587 enum st_retval retval;
1588 st_index_t i, rebuilds_num;
1591 int error_p, packed_p = tab->bins == NULL;
1593 entries = tab->entries;
1596 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1597 curr_entry_ptr = &entries[i];
1598 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1600 key = curr_entry_ptr->key;
1601 rebuilds_num = tab->rebuilds_num;
1602 hash = curr_entry_ptr->hash;
1603 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1605 if (retval == ST_REPLACE && replace) {
1607 value = curr_entry_ptr->record;
1608 retval = (*replace)(&key, &value, arg, TRUE);
1609 curr_entry_ptr->key = key;
1610 curr_entry_ptr->record = value;
1613 if (rebuilds_num != tab->rebuilds_num) {
1615 entries = tab->entries;
1616 packed_p = tab->bins == NULL;
1618 i = find_entry(tab, hash, key);
1619 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1621 error_p = i == UNDEFINED_ENTRY_IND;
1624 i = find_table_entry_ind(tab, hash, key);
1625 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1627 error_p = i == UNDEFINED_ENTRY_IND;
1630 if (error_p && check_p) {
1632 retval = (*func)(0, 0, arg, 1);
1635 curr_entry_ptr = &entries[i];
1648 st_data_t key = curr_entry_ptr->key;
1652 bin = find_entry(tab, hash, key);
1653 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1655 if (bin == UNDEFINED_ENTRY_IND)
1659 bin_ind = find_table_bin_ind(tab, hash, key);
1660 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1662 if (bin_ind == UNDEFINED_BIN_IND)
1664 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1665 MARK_BIN_DELETED(tab, bin_ind);
1667 curr_entry_ptr = &entries[bin];
1668 MARK_ENTRY_DELETED(curr_entry_ptr);
1670 update_range_for_deleted(tab, bin);
1679st_foreach_with_replace(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1681 return st_general_foreach(tab, func, replace, arg, TRUE);
1685 st_foreach_callback_func *func;
1690apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1692 const struct functor *f = (
void *)d;
1693 return f->func(k, v, f->arg);
1697st_foreach(
st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1699 const struct functor f = { func, arg };
1700 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1705st_foreach_check(
st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1706 st_data_t never ATTRIBUTE_UNUSED)
1708 return st_general_foreach(tab, func, 0, arg, TRUE);
1713static inline st_index_t
1714st_general_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1716 st_index_t i, bound;
1717 st_data_t key, *keys_start, *keys_end;
1720 bound = tab->entries_bound;
1722 keys_end = keys + size;
1723 for (i = tab->entries_start; i < bound; i++) {
1724 if (keys == keys_end)
1726 curr_entry_ptr = &entries[i];
1727 key = curr_entry_ptr->key;
1728 if (! DELETED_ENTRY_P(curr_entry_ptr))
1732 return keys - keys_start;
1736st_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1738 return st_general_keys(tab, keys, size);
1743st_keys_check(
st_table *tab, st_data_t *keys, st_index_t size,
1744 st_data_t never ATTRIBUTE_UNUSED)
1746 return st_general_keys(tab, keys, size);
1751static inline st_index_t
1752st_general_values(
st_table *tab, st_data_t *values, st_index_t size)
1754 st_index_t i, bound;
1755 st_data_t *values_start, *values_end;
1758 values_start = values;
1759 values_end = values + size;
1760 bound = tab->entries_bound;
1761 for (i = tab->entries_start; i < bound; i++) {
1762 if (values == values_end)
1764 curr_entry_ptr = &entries[i];
1765 if (! DELETED_ENTRY_P(curr_entry_ptr))
1766 *values++ = curr_entry_ptr->record;
1769 return values - values_start;
1773st_values(
st_table *tab, st_data_t *values, st_index_t size)
1775 return st_general_values(tab, values, size);
1780st_values_check(
st_table *tab, st_data_t *values, st_index_t size,
1781 st_data_t never ATTRIBUTE_UNUSED)
1783 return st_general_values(tab, values, size);
1786#define FNV1_32A_INIT 0x811c9dc5
1791#define FNV_32_PRIME 0x01000193
1794#ifndef UNALIGNED_WORD_ACCESS
1795# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1796 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1797 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1798 defined(__mc68020__)
1799# define UNALIGNED_WORD_ACCESS 1
1802#ifndef UNALIGNED_WORD_ACCESS
1803# define UNALIGNED_WORD_ACCESS 0
1809#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1810#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1812#if ST_INDEX_BITS <= 32
1813#define C1 (st_index_t)0xcc9e2d51
1814#define C2 (st_index_t)0x1b873593
1816#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1817#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1819NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1820NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1821NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1823static inline st_index_t
1824murmur_step(st_index_t h, st_index_t k)
1826#if ST_INDEX_BITS <= 32
1842static inline st_index_t
1843murmur_finish(st_index_t h)
1845#if ST_INDEX_BITS <= 32
1849 const st_index_t c1 = 0x85ebca6b;
1850 const st_index_t c2 = 0xc2b2ae35;
1856 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1857 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1859#if ST_INDEX_BITS > 64
1876st_hash(
const void *ptr,
size_t len, st_index_t h)
1878 const char *data = ptr;
1882#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1883#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1884#if SIZEOF_ST_INDEX_T > 4
1885#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1886#if SIZEOF_ST_INDEX_T > 8
1887#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1888 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1889#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1891#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1893#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1896 if (
len >=
sizeof(st_index_t)) {
1897#if !UNALIGNED_WORD_ACCESS
1898 int align = (int)((st_data_t)data %
sizeof(st_index_t));
1904#ifdef WORDS_BIGENDIAN
1905# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1906 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1908# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1909 t |= data_at(n) << CHAR_BIT*(n)
1915#ifdef WORDS_BIGENDIAN
1916 t >>= (CHAR_BIT * align) - CHAR_BIT;
1918 t <<= (CHAR_BIT * align);
1921 data +=
sizeof(st_index_t)-align;
1922 len -=
sizeof(st_index_t)-align;
1924 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1925 sr = CHAR_BIT * align;
1927 while (
len >=
sizeof(st_index_t)) {
1928 d = *(st_index_t *)data;
1929#ifdef WORDS_BIGENDIAN
1930 t = (t << sr) | (d >> sl);
1932 t = (t >> sr) | (d << sl);
1934 h = murmur_step(h, t);
1936 data +=
sizeof(st_index_t);
1937 len -=
sizeof(st_index_t);
1940 pack =
len < (size_t)align ? (
int)
len : align;
1943#ifdef WORDS_BIGENDIAN
1944# define UNALIGNED_ADD(n) case (n) + 1: \
1945 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1947# define UNALIGNED_ADD(n) case (n) + 1: \
1948 d |= data_at(n) << CHAR_BIT*(n)
1953#ifdef WORDS_BIGENDIAN
1954 t = (t << sr) | (d >> sl);
1956 t = (t >> sr) | (d << sl);
1959 if (
len < (
size_t)align)
goto skip_tail;
1961 h = murmur_step(h, t);
1967#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
1968#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
1970#define aligned_data data
1974 h = murmur_step(h, *(st_index_t *)aligned_data);
1975 data +=
sizeof(st_index_t);
1976 len -=
sizeof(st_index_t);
1977 }
while (
len >=
sizeof(st_index_t));
1983#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
1985#if SIZEOF_ST_INDEX_T > 4
1986 case 7: t |= data_at(6) << 48;
1987 case 6: t |= data_at(5) << 40;
1988 case 5: t |= data_at(4) << 32;
1990 t |= (st_index_t)*(uint32_t*)aligned_data;
1994 case 3: t |= data_at(2) << 16;
1995 case 2: t |= data_at(1) << 8;
1996 case 1: t |= data_at(0);
1998#ifdef WORDS_BIGENDIAN
1999# define UNALIGNED_ADD(n) case (n) + 1: \
2000 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
2002# define UNALIGNED_ADD(n) case (n) + 1: \
2003 t |= data_at(n) << CHAR_BIT*(n)
2011 h ^= t; h -= ROTL(t, 7);
2017 return murmur_finish(h);
2021st_hash_uint32(st_index_t h, uint32_t i)
2023 return murmur_step(h, i);
2026NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
2028st_hash_uint(st_index_t h, st_index_t i)
2033#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
2034 h = murmur_step(h, i >> 8*8);
2036 h = murmur_step(h, i);
2041st_hash_end(st_index_t h)
2043 h = murmur_finish(h);
2049rb_st_hash_start(st_index_t h)
2055strhash(st_data_t arg)
2057 register const char *
string = (
const char *)arg;
2058 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
2062st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
2069 if (c1 ==
'\0' || c2 ==
'\0') {
2070 if (c1 !=
'\0')
return 1;
2071 if (c2 !=
'\0')
return -1;
2074 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2075 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2086st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
2091 for (i = 0; i < n; i++) {
2094 if (c1 ==
'\0' || c2 ==
'\0') {
2095 if (c1 !=
'\0')
return 1;
2096 if (c2 !=
'\0')
return -1;
2099 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2100 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2112st_strcmp(st_data_t lhs, st_data_t rhs)
2114 const char *s1 = (
char *)lhs;
2115 const char *s2 = (
char *)rhs;
2116 return strcmp(s1, s2);
2120st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2122 const char *s1 = (
char *)lhs;
2123 const char *s2 = (
char *)rhs;
2124 return st_locale_insensitive_strcasecmp(s1, s2);
2127NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2129strcasehash(st_data_t arg)
2131 register const char *
string = (
const char *)arg;
2132 register st_index_t hval = FNV1_32A_INIT;
2138 unsigned int c = (
unsigned char)*
string++;
2139 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2143 hval *= FNV_32_PRIME;
2149st_numcmp(st_data_t x, st_data_t y)
2155st_numhash(st_data_t n)
2157 enum {s1 = 11, s2 = 3};
2158 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2166st_expand_table(
st_table *tab, st_index_t siz)
2171 if (siz <= get_allocated_entries(tab))
2174 tmp = st_init_table_with_size(tab->type, siz);
2175 n = get_allocated_entries(tab);
2178 st_free_entries(tab);
2181 tab->entry_power = tmp->entry_power;
2182 tab->bin_power = tmp->bin_power;
2183 tab->size_ind = tmp->size_ind;
2184 tab->entries = tmp->entries;
2186 tab->rebuilds_num++;
2187 free_fixed_ptr(tmp);
2195 int eq_p, rebuilt_p;
2202 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2203 p = &tab->entries[i];
2204 if (DELETED_ENTRY_P(p))
2206 for (j = i + 1; j < tab->entries_bound; j++) {
2207 q = &tab->entries[j];
2208 if (DELETED_ENTRY_P(q))
2210 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2211 if (EXPECT(rebuilt_p, 0))
2215 MARK_ENTRY_DELETED(q);
2217 update_range_for_deleted(tab, j);
2229 int eq_p, rebuilt_p;
2233 tab->bins = malloc(bins_size(tab));
2235 unsigned int const size_ind = get_size_ind(tab);
2236 initialize_bins(tab);
2237 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2240#ifdef QUADRATIC_PROBE
2243 st_index_t perturb = p->hash;
2246 if (DELETED_ENTRY_P(p))
2249 ind = hash_bin(p->hash, tab);
2251 st_index_t bin = get_bin(tab->bins, size_ind, ind);
2252 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2254 set_bin(tab->bins, size_ind, ind, i + ENTRY_BASE);
2259 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2260 if (EXPECT(rebuilt_p, 0))
2264 q->record = p->record;
2265 MARK_ENTRY_DELETED(p);
2267 update_range_for_deleted(tab, bin);
2272#ifdef QUADRATIC_PROBE
2273 ind = hash_bin(ind + d, tab);
2276 ind = secondary_hash(ind, tab, &perturb);
2294 if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2295 rebuilt_p = st_rehash_linear(tab);
2297 rebuilt_p = st_rehash_indexed(tab);
2298 }
while (rebuilt_p);
2302st_stringify(
VALUE key)
2305 rb_hash_key_str(key) : key;
2311 st_data_t k = st_stringify(key);
2313 e.hash = do_hash(k, tab);
2317 tab->entries[tab->entries_bound++] = e;
2328 for (i = 0; i < argc; ) {
2329 st_data_t k = st_stringify(argv[i++]);
2330 st_data_t v = argv[i++];
2331 st_insert(tab, k, v);
2343 for (i = 0; i < argc; ) {
2344 VALUE key = argv[i++];
2345 VALUE val = argv[i++];
2346 st_insert_single(tab, hash, key, val);
2356rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2358 st_index_t n, size = argc / 2;
2359 st_table *tab = RHASH_ST_TABLE(hash);
2361 tab = RHASH_TBL_RAW(hash);
2362 n = tab->entries_bound + size;
2363 st_expand_table(tab, n);
2364 if (UNLIKELY(tab->num_entries))
2365 st_insert_generic(tab, argc, argv, hash);
2367 st_insert_single(tab, hash, argv[0], argv[1]);
2368 else if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2369 st_insert_linear(tab, argc, argv, hash);
2371 st_insert_generic(tab, argc, argv, hash);
2377 st_index_t num = tab->num_entries;
2378 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2380 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2381 rebuild_table_with(new_tab, tab);
2382 rebuild_move_table(new_tab, tab);
2383 rebuild_cleanup(tab);
2391struct set_table_entry {
2397static inline st_hash_t
2398set_do_hash(st_data_t key,
set_table *tab)
2400 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
2401 return normalize_hash_value(hash);
2405static inline unsigned int
2408 return tab->size_ind;
2412static inline st_index_t
2415 return ((st_index_t) 1)<<tab->bin_power;
2419static inline st_index_t
2422 return set_get_bins_num(tab) - 1;
2427static inline st_index_t
2428set_hash_bin(st_hash_t hash_value,
set_table *tab)
2430 return hash_value & set_bins_mask(tab);
2434static inline st_index_t
2435set_get_allocated_entries(
const set_table *tab)
2437 return ((st_index_t) 1)<<tab->entry_power;
2441set_allocated_entries_size(
const set_table *tab)
2443 return set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2449 return tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS;
2453static inline st_index_t
2456 if (set_has_bins(tab)) {
2457 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
2463static inline st_index_t *
2466 if (set_has_bins(tab)) {
2467 return (st_index_t *)(((
char *)tab->
entries) + set_allocated_entries_size(tab));
2477 memset(set_bins_ptr(tab), 0, set_bins_size(tab));
2484 tab->num_entries = 0;
2485 tab->entries_start = tab->entries_bound = 0;
2486 if (set_bins_ptr(tab) != NULL)
2487 set_initialize_bins(tab);
2493 size_t memsize = set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2494 if (set_has_bins(tab)) {
2495 memsize += set_bins_size(tab);
2508 const char *e = getenv(
"ST_HASH_LOG");
2509 if (!e || !*e) init_st = 1;
2518 n = get_power2(size);
2521 tab->entry_power = n;
2522 tab->bin_power = features[n].bin_power;
2523 tab->size_ind = features[n].size_ind;
2525 tab->
entries = (set_table_entry *)malloc(set_entries_memsize(tab));
2526 set_make_tab_empty(tab);
2527 tab->rebuilds_num = 0;
2537 if (tab == NULL) tab = malloc(
sizeof(
set_table));
2539 set_init_existing_table_with_size(tab, type, size);
2545set_init_numtable(
void)
2547 return set_init_table_with_size(NULL, &type_numhash, 0);
2551set_init_numtable_with_size(st_index_t size)
2553 return set_init_table_with_size(NULL, &type_numhash, size);
2557set_table_size(
const struct set_table *tbl)
2559 return tbl->num_entries;
2566 set_make_tab_empty(tab);
2567 tab->rebuilds_num++;
2573 sized_free(tab->
entries, set_entries_memsize(tab));
2581 set_free_embedded_table(tab);
2582 free_fixed_ptr(tab);
2590 + (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS ? 0 : set_bins_size(tab))
2591 + set_get_allocated_entries(tab) * sizeof(set_table_entry));
2595set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2598set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2601set_find_table_bin_ind_direct(
set_table *table, st_hash_t hash_value, st_data_t key);
2604set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2605 st_data_t key, st_index_t *bin_ind);
2609static void set_rebuild_cleanup(
set_table *
const tab);
2618 if ((2 * tab->num_entries <= set_get_allocated_entries(tab)
2619 && REBUILD_THRESHOLD * tab->num_entries > set_get_allocated_entries(tab))
2620 || tab->num_entries < (1 << MINIMAL_POWER2)) {
2622 tab->num_entries = 0;
2623 if (set_has_bins(tab))
2624 set_initialize_bins(tab);
2625 set_rebuild_table_with(tab, tab);
2632 new_tab = set_init_table_with_size(NULL, tab->type,
2633 2 * tab->num_entries - 1);
2634 set_rebuild_table_with(new_tab, tab);
2635 set_rebuild_move_table(new_tab, tab);
2637 set_rebuild_cleanup(tab);
2644 unsigned int size_ind;
2645 set_table_entry *new_entries;
2646 set_table_entry *curr_entry_ptr;
2650 new_entries = new_tab->
entries;
2653 bins = set_bins_ptr(new_tab);
2654 size_ind = set_get_size_ind(new_tab);
2655 st_index_t bound = tab->entries_bound;
2656 set_table_entry *entries = tab->
entries;
2658 for (i = tab->entries_start; i < bound; i++) {
2659 curr_entry_ptr = &entries[i];
2660 PREFETCH(entries + i + 1, 0);
2661 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
2663 if (&new_entries[ni] != curr_entry_ptr)
2664 new_entries[ni] = *curr_entry_ptr;
2665 if (EXPECT(bins != NULL, 1)) {
2666 bin_ind = set_find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
2667 curr_entry_ptr->key);
2668 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
2670 new_tab->num_entries++;
2674 assert(new_tab->num_entries == tab->num_entries);
2680 sized_free(tab->
entries, set_entries_memsize(tab));
2683 tab->entry_power = new_tab->entry_power;
2684 tab->bin_power = new_tab->bin_power;
2685 tab->size_ind = new_tab->size_ind;
2687 free_fixed_ptr(new_tab);
2691set_rebuild_cleanup(
set_table *
const tab)
2693 tab->entries_start = 0;
2694 tab->entries_bound = tab->num_entries;
2695 tab->rebuilds_num++;
2710static inline st_index_t
2711set_secondary_hash(st_index_t ind,
set_table *tab, st_index_t *perturb)
2714 ind = (ind << 2) + ind + *perturb + 1;
2715 return set_hash_bin(ind, tab);
2722static inline st_index_t
2723set_find_entry(
set_table *tab, st_hash_t hash_value, st_data_t key)
2725 int eq_p, rebuilt_p;
2726 st_index_t i, bound;
2727 set_table_entry *entries;
2729 bound = tab->entries_bound;
2731 for (i = tab->entries_start; i < bound; i++) {
2732 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
2733 if (EXPECT(rebuilt_p, 0))
2734 return REBUILT_TABLE_ENTRY_IND;
2738 return UNDEFINED_ENTRY_IND;
2750set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2752 int eq_p, rebuilt_p;
2754#ifdef QUADRATIC_PROBE
2760 set_table_entry *entries = tab->
entries;
2762 ind = set_hash_bin(hash_value, tab);
2763#ifdef QUADRATIC_PROBE
2766 perturb = hash_value;
2769 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2770 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2771 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2772 if (EXPECT(rebuilt_p, 0))
2773 return REBUILT_TABLE_ENTRY_IND;
2777 else if (EMPTY_BIN_P(bin))
2778 return UNDEFINED_ENTRY_IND;
2779#ifdef QUADRATIC_PROBE
2780 ind = set_hash_bin(ind + d, tab);
2783 ind = set_secondary_hash(ind, tab, &perturb);
2794set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2796 int eq_p, rebuilt_p;
2798#ifdef QUADRATIC_PROBE
2804 set_table_entry *entries = tab->
entries;
2806 ind = set_hash_bin(hash_value, tab);
2807#ifdef QUADRATIC_PROBE
2810 perturb = hash_value;
2813 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2814 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2815 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2816 if (EXPECT(rebuilt_p, 0))
2817 return REBUILT_TABLE_BIN_IND;
2821 else if (EMPTY_BIN_P(bin))
2822 return UNDEFINED_BIN_IND;
2823#ifdef QUADRATIC_PROBE
2824 ind = set_hash_bin(ind + d, tab);
2827 ind = set_secondary_hash(ind, tab, &perturb);
2837set_find_table_bin_ind_direct(
set_table *tab, st_hash_t hash_value, st_data_t key)
2840#ifdef QUADRATIC_PROBE
2847 ind = set_hash_bin(hash_value, tab);
2848#ifdef QUADRATIC_PROBE
2851 perturb = hash_value;
2854 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2855 if (EMPTY_OR_DELETED_BIN_P(bin))
2857#ifdef QUADRATIC_PROBE
2858 ind = set_hash_bin(ind + d, tab);
2861 ind = set_secondary_hash(ind, tab, &perturb);
2868#define MARK_SET_BIN_EMPTY(tab, i) (set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, EMPTY_BIN))
2880set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2881 st_data_t key, st_index_t *bin_ind)
2883 int eq_p, rebuilt_p;
2885 st_hash_t curr_hash_value = *hash_value;
2886#ifdef QUADRATIC_PROBE
2891 st_index_t entry_index;
2892 st_index_t firset_deleted_bin_ind;
2893 set_table_entry *entries;
2895 ind = set_hash_bin(curr_hash_value, tab);
2896#ifdef QUADRATIC_PROBE
2899 perturb = curr_hash_value;
2901 firset_deleted_bin_ind = UNDEFINED_BIN_IND;
2904 entry_index = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2905 if (EMPTY_BIN_P(entry_index)) {
2907 entry_index = UNDEFINED_ENTRY_IND;
2908 if (firset_deleted_bin_ind != UNDEFINED_BIN_IND) {
2910 ind = firset_deleted_bin_ind;
2911 MARK_SET_BIN_EMPTY(tab, ind);
2915 else if (! DELETED_BIN_P(entry_index)) {
2916 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
2917 if (EXPECT(rebuilt_p, 0))
2918 return REBUILT_TABLE_ENTRY_IND;
2922 else if (firset_deleted_bin_ind == UNDEFINED_BIN_IND)
2923 firset_deleted_bin_ind = ind;
2924#ifdef QUADRATIC_PROBE
2925 ind = set_hash_bin(ind + d, tab);
2928 ind = set_secondary_hash(ind, tab, &perturb);
2938set_table_lookup(
set_table *tab, st_data_t key)
2941 st_hash_t hash = set_do_hash(key, tab);
2944 if (!set_has_bins(tab)) {
2945 bin = set_find_entry(tab, hash, key);
2946 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2948 if (bin == UNDEFINED_ENTRY_IND)
2952 bin = set_find_table_entry_ind(tab, hash, key);
2953 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2955 if (bin == UNDEFINED_ENTRY_IND)
2964set_rebuild_table_if_necessary (
set_table *tab)
2966 st_index_t bound = tab->entries_bound;
2968 if (bound == set_get_allocated_entries(tab))
2969 set_rebuild_table(tab);
2976set_insert(
set_table *tab, st_data_t key)
2978 set_table_entry *entry;
2981 st_hash_t hash_value;
2985 hash_value = set_do_hash(key, tab);
2987 set_rebuild_table_if_necessary(tab);
2988 if (!set_has_bins(tab)) {
2989 bin = set_find_entry(tab, hash_value, key);
2990 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2992 new_p = bin == UNDEFINED_ENTRY_IND;
2995 bin_ind = UNDEFINED_BIN_IND;
2998 bin = set_find_table_bin_ptr_and_reserve(tab, &hash_value,
3000 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3002 new_p = bin == UNDEFINED_ENTRY_IND;
3006 ind = tab->entries_bound++;
3008 entry->hash = hash_value;
3010 if (bin_ind != UNDEFINED_BIN_IND)
3011 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
3021 *new_tab = *old_tab;
3022 size_t memsize = set_allocated_entries_size(old_tab) + set_bins_size(old_tab);
3023 new_tab->
entries = (set_table_entry *)malloc(memsize);
3034 if (set_replace(new_tab, old_tab) == NULL) {
3035 set_free_table(new_tab);
3045set_update_range_for_deleted(
set_table *tab, st_index_t n)
3049 if (tab->entries_start == n) {
3050 st_index_t start = n + 1;
3051 st_index_t bound = tab->entries_bound;
3052 set_table_entry *entries = tab->
entries;
3053 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
3054 tab->entries_start = start;
3061#define MARK_SET_BIN_DELETED(tab, i) \
3063 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, DELETED_BIN); \
3069set_table_delete(
set_table *tab, st_data_t *key)
3071 set_table_entry *entry;
3076 hash = set_do_hash(*key, tab);
3078 if (!set_has_bins(tab)) {
3079 bin = set_find_entry(tab, hash, *key);
3080 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3082 if (bin == UNDEFINED_ENTRY_IND) {
3087 bin_ind = set_find_table_bin_ind(tab, hash, *key);
3088 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3090 if (bin_ind == UNDEFINED_BIN_IND) {
3093 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3094 MARK_SET_BIN_DELETED(tab, bin_ind);
3098 MARK_ENTRY_DELETED(entry);
3100 set_update_range_for_deleted(tab, bin);
3113set_general_foreach(
set_table *tab, set_foreach_check_callback_func *func,
3114 set_update_callback_func *replace, st_data_t arg,
3119 set_table_entry *entries, *curr_entry_ptr;
3120 enum st_retval retval;
3121 st_index_t i, rebuilds_num;
3124 int error_p, packed_p = !set_has_bins(tab);
3129 for (i = tab->entries_start; i < tab->entries_bound; i++) {
3130 curr_entry_ptr = &entries[i];
3131 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
3133 key = curr_entry_ptr->key;
3134 rebuilds_num = tab->rebuilds_num;
3135 hash = curr_entry_ptr->hash;
3136 retval = (*func)(key, arg, 0);
3138 if (retval == ST_REPLACE && replace) {
3139 retval = (*replace)(&key, arg, TRUE);
3140 curr_entry_ptr->key = key;
3143 if (rebuilds_num != tab->rebuilds_num) {
3146 packed_p = !set_has_bins(tab);
3148 i = set_find_entry(tab, hash, key);
3149 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3151 error_p = i == UNDEFINED_ENTRY_IND;
3154 i = set_find_table_entry_ind(tab, hash, key);
3155 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3157 error_p = i == UNDEFINED_ENTRY_IND;
3160 if (error_p && check_p) {
3162 retval = (*func)(0, arg, 1);
3165 curr_entry_ptr = &entries[i];
3178 st_data_t key = curr_entry_ptr->key;
3182 bin = set_find_entry(tab, hash, key);
3183 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3185 if (bin == UNDEFINED_ENTRY_IND)
3189 bin_ind = set_find_table_bin_ind(tab, hash, key);
3190 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3192 if (bin_ind == UNDEFINED_BIN_IND)
3194 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3195 MARK_SET_BIN_DELETED(tab, bin_ind);
3197 curr_entry_ptr = &entries[bin];
3198 MARK_ENTRY_DELETED(curr_entry_ptr);
3200 set_update_range_for_deleted(tab, bin);
3209set_foreach_with_replace(
set_table *tab, set_foreach_check_callback_func *func, set_update_callback_func *replace, st_data_t arg)
3211 return set_general_foreach(tab, func, replace, arg, TRUE);
3215 set_foreach_callback_func *func;
3220set_apply_functor(st_data_t k, st_data_t d,
int _)
3222 const struct set_functor *f = (
void *)d;
3223 return f->func(k, f->arg);
3227set_table_foreach(
set_table *tab, set_foreach_callback_func *func, st_data_t arg)
3229 const struct set_functor f = { func, arg };
3230 return set_general_foreach(tab, set_apply_functor, NULL, (st_data_t)&f, FALSE);
3235set_foreach_check(
set_table *tab, set_foreach_check_callback_func *func, st_data_t arg,
3236 st_data_t never ATTRIBUTE_UNUSED)
3238 return set_general_foreach(tab, func, NULL, arg, TRUE);
3244set_keys(
set_table *tab, st_data_t *keys, st_index_t size)
3246 st_index_t i, bound;
3247 st_data_t key, *keys_start, *keys_end;
3248 set_table_entry *curr_entry_ptr, *entries = tab->
entries;
3250 bound = tab->entries_bound;
3252 keys_end = keys + size;
3253 for (i = tab->entries_start; i < bound; i++) {
3254 if (keys == keys_end)
3256 curr_entry_ptr = &entries[i];
3257 key = curr_entry_ptr->key;
3258 if (! DELETED_ENTRY_P(curr_entry_ptr))
3262 return keys - keys_start;
3268 st_index_t num = tab->num_entries;
3269 if (REBUILD_THRESHOLD * num <= set_get_allocated_entries(tab)) {
3271 set_table *new_tab = set_init_table_with_size(NULL, tab->type, 2 * num);
3272 set_rebuild_table_with(new_tab, tab);
3273 set_rebuild_move_table(new_tab, tab);
3274 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.