Ruby 4.1.0dev (2026-11-28 revision 14f57d20f9e18994d04e1327b8037a9f9edd04d2)
id_table.c (14f57d20f9e18994d04e1327b8037a9f9edd04d2)
1/* This file is included by symbol.c */
2
3#include "id_table.h"
4
5#ifndef ID_TABLE_DEBUG
6#define ID_TABLE_DEBUG 0
7#endif
8
9#if ID_TABLE_DEBUG == 0
10#undef NDEBUG
11#define NDEBUG
12#endif
13#include "ruby_assert.h"
14
15typedef rb_id_serial_t id_key_t;
16
17static inline ID
18key2id(id_key_t key)
19{
20 return id_serial_to_id(key);
21}
22
23static inline id_key_t
24id2key(ID id)
25{
26 return rb_id_to_serial(id);
27}
28
29/* simple open addressing with quadratic probing.
30 uses mark-bit on collisions - need extra 1 bit,
31 ID is strictly 3 bits larger than rb_id_serial_t */
32
33#if SIZEOF_VALUE == 8
34/* The table body is a single buffer laid out as:
35
36 [VALUE values[capa] | id_key_t keys[capa] | collision bitmap]
37
38 where the collision bitmap uses one mark bit per slot. Keeping the
39 keys out of the item struct avoids padding them to the alignment of
40 VALUE. The three regions are computed from buf and capa, so the
41 struct only needs to store the buffer pointer. */
42#define COLLISION_TABLE_SIZE(capa) roomof((size_t)(capa), CHAR_BIT)
43#define ID_TABLE_BUF_SIZE(capa) \
44 ((sizeof(VALUE) + sizeof(id_key_t)) * (size_t)(capa) + COLLISION_TABLE_SIZE(capa))
45
46static inline VALUE *
47id_table_items(struct rb_id_table *tbl)
48{
49 return (VALUE *)tbl->buf;
50}
51
52static inline id_key_t *
53id_table_keys(struct rb_id_table *tbl)
54{
55 return (id_key_t *)(id_table_items(tbl) + tbl->capa);
56}
57
58static inline uint8_t *
59id_table_collision_table(struct rb_id_table *tbl)
60{
61 return (uint8_t *)(id_table_keys(tbl) + tbl->capa);
62}
63
64#define ITEM_GET_KEY(tbl, i) (id_table_keys(tbl)[i])
65#define ITEM_KEY_ISSET(tbl, i) ((tbl)->buf && id_table_keys(tbl)[i])
66#define ITEM_COLLIDED(tbl, i) (id_table_collision_table(tbl)[(i) / CHAR_BIT] & ((uint8_t)1 << ((i) % CHAR_BIT)))
67#define ITEM_SET_COLLIDED(tbl, i) (id_table_collision_table(tbl)[(i) / CHAR_BIT] |= ((uint8_t)1 << ((i) % CHAR_BIT)))
68#define ITEM_VALUE(tbl, i) (id_table_items(tbl)[i])
69
70static inline void
71ITEM_SET_KEY(struct rb_id_table *tbl, int i, id_key_t key)
72{
73 id_table_keys(tbl)[i] = key;
74}
75#else
76typedef struct rb_id_item {
77 id_key_t key;
78 VALUE val;
79} item_t;
80
81#define ID_TABLE_BUF_SIZE(capa) (sizeof(item_t) * (size_t)(capa))
82#define id_table_items(tbl) ((item_t *)(tbl)->buf)
83
84#define ITEM_GET_KEY(tbl, i) (id_table_items(tbl)[i].key >> 1)
85#define ITEM_KEY_ISSET(tbl, i) (id_table_items(tbl)[i].key > 1)
86#define ITEM_COLLIDED(tbl, i) (id_table_items(tbl)[i].key & 1)
87#define ITEM_SET_COLLIDED(tbl, i) (id_table_items(tbl)[i].key |= 1)
88#define ITEM_VALUE(tbl, i) (id_table_items(tbl)[i].val)
89
90static inline void
91ITEM_SET_KEY(struct rb_id_table *tbl, int i, id_key_t key)
92{
93 id_table_items(tbl)[i].key = (key << 1) | ITEM_COLLIDED(tbl, i);
94}
95#endif
96
97static inline int
98round_capa(int capa)
99{
100 /* minsize is 4 */
101 capa >>= 2;
102 capa |= capa >> 1;
103 capa |= capa >> 2;
104 capa |= capa >> 4;
105 capa |= capa >> 8;
106 capa |= capa >> 16;
107 return (capa + 1) << 2;
108}
109
110static void
111id_table_alloc_buf(struct rb_id_table *tbl, int capa)
112{
113#if SIZEOF_VALUE == 8
114 tbl->buf = ruby_xcalloc(1, ID_TABLE_BUF_SIZE(capa));
115#else
116 tbl->buf = ZALLOC_N(item_t, capa);
117#endif
118}
119
120struct rb_id_table *
121rb_id_table_init(struct rb_id_table *tbl, size_t s_capa)
122{
123 int capa = (int)s_capa;
124 MEMZERO(tbl, struct rb_id_table, 1);
125 if (capa > 0) {
126 capa = round_capa(capa);
127 tbl->capa = (int)capa;
128 id_table_alloc_buf(tbl, capa);
129 }
130 return tbl;
131}
132
133struct rb_id_table *
134rb_id_table_create(size_t capa)
135{
136 struct rb_id_table *tbl = ALLOC(struct rb_id_table);
137 return rb_id_table_init(tbl, capa);
138}
139
140void
141rb_id_table_free_items(struct rb_id_table *tbl)
142{
143 xfree(tbl->buf);
144}
145
146void
147rb_id_table_free(struct rb_id_table *tbl)
148{
149 xfree(tbl->buf);
150 xfree(tbl);
151}
152
153void
154rb_id_table_clear(struct rb_id_table *tbl)
155{
156 tbl->num = 0;
157 tbl->used = 0;
158 if (tbl->buf) {
159 memset(tbl->buf, 0, ID_TABLE_BUF_SIZE(tbl->capa));
160 }
161}
162
163size_t
164rb_id_table_size(const struct rb_id_table *tbl)
165{
166 return (size_t)tbl->num;
167}
168
169size_t
170rb_id_table_memsize(const struct rb_id_table *tbl)
171{
172 return ID_TABLE_BUF_SIZE(tbl->capa) + sizeof(struct rb_id_table);
173}
174
175static int
176hash_table_index(struct rb_id_table* tbl, id_key_t key)
177{
178 if (tbl->capa > 0) {
179 int mask = tbl->capa - 1;
180 int ix = key & mask;
181 int d = 1;
182 while (key != ITEM_GET_KEY(tbl, ix)) {
183 if (!ITEM_COLLIDED(tbl, ix))
184 return -1;
185 ix = (ix + d) & mask;
186 d++;
187 }
188 return ix;
189 }
190 return -1;
191}
192
193static void
194hash_table_raw_insert(struct rb_id_table *tbl, id_key_t key, VALUE val)
195{
196 int mask = tbl->capa - 1;
197 int ix = key & mask;
198 int d = 1;
199 RUBY_ASSERT(key != 0);
200 while (ITEM_KEY_ISSET(tbl, ix)) {
201 ITEM_SET_COLLIDED(tbl, ix);
202 ix = (ix + d) & mask;
203 d++;
204 }
205 tbl->num++;
206 if (!ITEM_COLLIDED(tbl, ix)) {
207 tbl->used++;
208 }
209 ITEM_SET_KEY(tbl, ix, key);
210 ITEM_VALUE(tbl, ix) = val;
211}
212
213static int
214hash_delete_index(struct rb_id_table *tbl, int ix)
215{
216 if (ix >= 0) {
217 if (!ITEM_COLLIDED(tbl, ix)) {
218 tbl->used--;
219 }
220 tbl->num--;
221 ITEM_SET_KEY(tbl, ix, 0);
222 ITEM_VALUE(tbl, ix) = 0;
223 return TRUE;
224 }
225 else {
226 return FALSE;
227 }
228}
229
230static void
231hash_table_extend(struct rb_id_table* tbl)
232{
233 if (tbl->used + (tbl->used >> 1) >= tbl->capa) {
234 int new_cap = round_capa(tbl->num + (tbl->num >> 1));
235 int i;
236 void *old;
237 struct rb_id_table tmp_tbl = {0};
238 if (new_cap < tbl->capa) {
239 new_cap = round_capa(tbl->used + (tbl->used >> 1));
240 }
241 tmp_tbl.capa = new_cap;
242 id_table_alloc_buf(&tmp_tbl, new_cap);
243 for (i = 0; i < tbl->capa; i++) {
244 id_key_t key = ITEM_GET_KEY(tbl, i);
245 if (key != 0) {
246 hash_table_raw_insert(&tmp_tbl, key, ITEM_VALUE(tbl, i));
247 }
248 }
249 old = tbl->buf;
250 *tbl = tmp_tbl;
251 xfree(old);
252 }
253}
254
255#if ID_TABLE_DEBUG && 0
256static void
257hash_table_show(struct rb_id_table *tbl)
258{
259 const int capa = tbl->capa;
260 int i;
261
262 fprintf(stderr, "tbl: %p (capa: %d, num: %d, used: %d)\n", tbl, tbl->capa, tbl->num, tbl->used);
263 for (i=0; i<capa; i++) {
264 if (ITEM_KEY_ISSET(tbl, i)) {
265 const id_key_t key = ITEM_GET_KEY(tbl, i);
266 fprintf(stderr, " -> [%d] %s %d\n", i, rb_id2name(key2id(key)), (int)key);
267 }
268 }
269}
270#endif
271
272static int
273rb_id_table_lookup_key(struct rb_id_table *tbl, id_key_t key, VALUE *valp)
274{
275 int index = hash_table_index(tbl, key);
276
277 if (index >= 0) {
278 *valp = ITEM_VALUE(tbl, index);
279 return TRUE;
280 }
281 else {
282 return FALSE;
283 }
284}
285
286int
287rb_id_table_lookup(struct rb_id_table *tbl, ID id, VALUE *valp)
288{
289 return rb_id_table_lookup_key(tbl, id2key(id), valp);
290}
291
292static int
293rb_id_table_insert_key(struct rb_id_table *tbl, const id_key_t key, const VALUE val)
294{
295 const int index = hash_table_index(tbl, key);
296
297 if (index >= 0) {
298 ITEM_VALUE(tbl, index) = val;
299 }
300 else {
301 hash_table_extend(tbl);
302 hash_table_raw_insert(tbl, key, val);
303 }
304 return TRUE;
305}
306
307int
308rb_id_table_insert(struct rb_id_table *tbl, ID id, VALUE val)
309{
310 return rb_id_table_insert_key(tbl, id2key(id), val);
311}
312
313int
314rb_id_table_delete(struct rb_id_table *tbl, ID id)
315{
316 const id_key_t key = id2key(id);
317 int index = hash_table_index(tbl, key);
318 return hash_delete_index(tbl, index);
319}
320
321void
322rb_id_table_foreach(struct rb_id_table *tbl, rb_id_table_foreach_func_t *func, void *data)
323{
324 int i, capa = tbl->capa;
325
326 for (i=0; i<capa; i++) {
327 if (ITEM_KEY_ISSET(tbl, i)) {
328 const id_key_t key = ITEM_GET_KEY(tbl, i);
329 enum rb_id_table_iterator_result ret = (*func)(key2id(key), ITEM_VALUE(tbl, i), data);
330 RUBY_ASSERT(key != 0);
331
332 if (ret == ID_TABLE_DELETE)
333 hash_delete_index(tbl, i);
334 else if (ret == ID_TABLE_STOP)
335 return;
336 }
337 }
338}
339
340void
341rb_id_table_foreach_values(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, void *data)
342{
343 int i, capa = tbl->capa;
344
345 if (!tbl->buf) {
346 return;
347 }
348
349 for (i=0; i<capa; i++) {
350 if (ITEM_KEY_ISSET(tbl, i)) {
351 enum rb_id_table_iterator_result ret = (*func)(ITEM_VALUE(tbl, i), data);
352
353 if (ret == ID_TABLE_DELETE)
354 hash_delete_index(tbl, i);
355 else if (ret == ID_TABLE_STOP)
356 return;
357 }
358 }
359}
360
361void
362rb_id_table_foreach_values_with_replace(struct rb_id_table *tbl, rb_id_table_foreach_values_func_t *func, rb_id_table_update_value_callback_func_t *replace, void *data)
363{
364 int i, capa = tbl->capa;
365
366 for (i = 0; i < capa; i++) {
367 if (ITEM_KEY_ISSET(tbl, i)) {
368 enum rb_id_table_iterator_result ret = (*func)(ITEM_VALUE(tbl, i), data);
369
370 if (ret == ID_TABLE_REPLACE) {
371 VALUE val = ITEM_VALUE(tbl, i);
372 ret = (*replace)(&val, data, TRUE);
373 ITEM_VALUE(tbl, i) = val;
374 }
375
376 if (ret == ID_TABLE_STOP)
377 return;
378 }
379 }
380}
381
382static void
383managed_id_table_free(void *data)
384{
385 struct rb_id_table *tbl = (struct rb_id_table *)data;
386 rb_id_table_free_items(tbl);
387}
388
389static size_t
390managed_id_table_memsize(const void *data)
391{
392 const struct rb_id_table *tbl = (const struct rb_id_table *)data;
393 return rb_id_table_memsize(tbl) - sizeof(struct rb_id_table);
394}
395
396const rb_data_type_t rb_managed_id_table_type = {
397 .wrap_struct_name = "VM/managed_id_table",
398 .function = {
399 .dmark = NULL, // Nothing to mark
400 .dfree = managed_id_table_free,
401 .dsize = managed_id_table_memsize,
402 },
403 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
404};
405
406static inline struct rb_id_table *
407managed_id_table_ptr(VALUE obj)
408{
410 RUBY_ASSERT(rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), &rb_managed_id_table_type));
411
412 return RTYPEDDATA_GET_DATA(obj);
413}
414
415VALUE
416rb_managed_id_table_create(const rb_data_type_t *type, size_t capa)
417{
418 struct rb_id_table *tbl;
419 VALUE obj = TypedData_Make_Struct(0, struct rb_id_table, type, tbl);
421 rb_id_table_init(tbl, capa); // NOTE: this can cause GC, so dmark and dsize need to check tbl->buf
422 return obj;
423}
424
425VALUE
426rb_managed_id_table_new(size_t capa)
427{
428 return rb_managed_id_table_create(&rb_managed_id_table_type, capa);
429}
430
431static enum rb_id_table_iterator_result
432managed_id_table_dup_i(ID id, VALUE val, void *data)
433{
434 struct rb_id_table *new_tbl = (struct rb_id_table *)data;
435 rb_id_table_insert(new_tbl, id, val);
436 return ID_TABLE_CONTINUE;
437}
438
439VALUE
440rb_managed_id_table_dup(VALUE old_table)
441{
442 struct rb_id_table *new_tbl;
443 VALUE obj = TypedData_Make_Struct(0, struct rb_id_table, RTYPEDDATA_TYPE(old_table), new_tbl);
444 /* A managed id table hangs off VM-global state (e.g. a shape tree's edge
445 * table grows via this dup) and is reachable from every Ractor, so mark it
446 * shareable. */
448 struct rb_id_table *old_tbl = managed_id_table_ptr(old_table);
449 rb_id_table_init(new_tbl, old_tbl->num + 1);
450 rb_id_table_foreach(old_tbl, managed_id_table_dup_i, new_tbl);
451 /* The table body is embedded (RUBY_TYPED_EMBEDDABLE), so old_tbl is an
452 * interior pointer. Keep old_table live in case dup_i triggers a GC. */
453 RB_GC_GUARD(old_table);
454 return obj;
455}
456
457int
458rb_managed_id_table_lookup_serial(VALUE table, id_key_t key, VALUE *valp)
459{
460 return rb_id_table_lookup_key(managed_id_table_ptr(table), key, valp);
461}
462
463int
464rb_managed_id_table_lookup(VALUE table, ID id, VALUE *valp)
465{
466 return rb_managed_id_table_lookup_serial(table, id2key(id), valp);
467}
468
469int
470rb_managed_id_table_insert_serial(VALUE table, id_key_t key, VALUE val)
471{
472 int result = rb_id_table_insert_key(managed_id_table_ptr(table), key, val);
473 /* The table body is embedded (RUBY_TYPED_EMBEDDABLE), so the pointer above
474 * is interior. Keep table live in case the insert grows the buffer. */
475 RB_GC_GUARD(table);
476 return result;
477}
478
479int
480rb_managed_id_table_insert(VALUE table, ID id, VALUE val)
481{
482 return rb_managed_id_table_insert_serial(table, id2key(id), val);
483}
484
485size_t
486rb_managed_id_table_size(VALUE table)
487{
488 return rb_id_table_size(managed_id_table_ptr(table));
489}
490
491void
492rb_managed_id_table_foreach(VALUE table, rb_id_table_foreach_func_t *func, void *data)
493{
494 rb_id_table_foreach(managed_id_table_ptr(table), func, data);
495 /* The table body is embedded (RUBY_TYPED_EMBEDDABLE), so the pointer above
496 * is interior. Keep table live in case func triggers a GC. */
497 RB_GC_GUARD(table);
498}
499
500void
501rb_managed_id_table_foreach_values(VALUE table, rb_id_table_foreach_values_func_t *func, void *data)
502{
503 rb_id_table_foreach_values(managed_id_table_ptr(table), func, data);
504 RB_GC_GUARD(table);
505}
506
507int
508rb_managed_id_table_delete(VALUE table, ID id)
509{
510 return rb_id_table_delete(managed_id_table_ptr(table), id);
511}
512
513static enum rb_id_table_iterator_result
514marked_id_table_mark_i(VALUE val, void *data)
515{
516 rb_gc_mark_movable(val);
517 return ID_TABLE_CONTINUE;
518}
519
520static void
521marked_id_table_mark(void *ptr)
522{
523 struct rb_id_table *tbl = (struct rb_id_table *)ptr;
524 rb_id_table_foreach_values(tbl, marked_id_table_mark_i, NULL);
525}
526
527static enum rb_id_table_iterator_result
528marked_id_table_compact_check_i(VALUE value, void *data)
529{
530 if (rb_gc_location(value) != value) {
531 return ID_TABLE_REPLACE;
532 }
533 return ID_TABLE_CONTINUE;
534}
535
536static enum rb_id_table_iterator_result
537marked_id_table_compact_replace_i(VALUE *value, void *data, int existing)
538{
539 rb_gc_update_moved(value);
540 return ID_TABLE_CONTINUE;
541}
542
543static void
544marked_id_table_compact(void *ptr)
545{
546 struct rb_id_table *tbl = (struct rb_id_table *)ptr;
547 rb_id_table_foreach_values_with_replace(tbl, marked_id_table_compact_check_i, marked_id_table_compact_replace_i, NULL);
548}
549
550const rb_data_type_t rb_marked_id_table_type = {
551 .wrap_struct_name = "VM/marked_id_table",
552 .function = {
553 .dmark = marked_id_table_mark,
554 .dfree = managed_id_table_free,
555 .dsize = managed_id_table_memsize,
556 .dcompact = marked_id_table_compact,
557 },
558 .parent = &rb_managed_id_table_type,
559 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
560};
561
562VALUE
563rb_marked_id_table_new(size_t capa)
564{
565 return rb_managed_id_table_create(&rb_marked_id_table_type, capa);
566}
567
568int
569rb_marked_id_table_insert(VALUE table, ID id, VALUE val)
570{
571 int result = rb_managed_id_table_insert(table, id, val);
572 RB_OBJ_WRITTEN(table, Qundef, val);
573 return result;
574}
575
576static enum rb_id_table_iterator_result
577marked_id_table_dup_i(VALUE val, void *data)
578{
579 VALUE new_table = (VALUE)data;
580 RB_OBJ_WRITTEN(new_table, Qundef, val);
581 return ID_TABLE_CONTINUE;
582}
583
584VALUE
585rb_marked_id_table_dup(VALUE old_table)
586{
587 VALUE new_table = rb_managed_id_table_dup(old_table);
588 rb_managed_id_table_foreach_values(new_table, marked_id_table_dup_i, (void *)new_table);
589 return new_table;
590}
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:403
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:404
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:504
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
int capa
Designed capacity of the buffer.
Definition io.h:11
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
Definition ractor.h:290
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:363
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
VALUE type(ANYARGS)
ANYARGS-ed function type.
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:692
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:239
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:604
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376