Ruby: prism/util/pm_constant_pool.c Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

pm_constant_pool.c
1#include "prism/util/pm_constant_pool.h"
2
6void
7pm_constant_id_list_init(pm_constant_id_list_t *list) {
8 list->ids = NULL;
9 list->size = 0;
10 list->capacity = 0;
11}
12
16void
17pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity) {
18 if (capacity) {
19 list->ids = xcalloc(capacity, sizeof(pm_constant_id_t));
20 if (list->ids == NULL) abort();
21 } else {
22 list->ids = NULL;
23 }
24
25 list->size = 0;
26 list->capacity = capacity;
27}
28
33bool
34pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id) {
35 if (list->size >= list->capacity) {
36 list->capacity = list->capacity == 0 ? 8 : list->capacity * 2;
37 list->ids = (pm_constant_id_t *) xrealloc(list->ids, sizeof(pm_constant_id_t) * list->capacity);
38 if (list->ids == NULL) return false;
39 }
40
41 list->ids[list->size++] = id;
42 return true;
43}
44
48void
49pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id) {
50 assert(index < list->capacity);
51 assert(list->ids[index] == PM_CONSTANT_ID_UNSET);
52
53 list->ids[index] = id;
54 list->size++;
55}
56
60bool
61pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) {
62 for (size_t index = 0; index < list->size; index++) {
63 if (list->ids[index] == id) return true;
64 }
65 return false;
66}
67
71void
72pm_constant_id_list_free(pm_constant_id_list_t *list) {
73 if (list->ids != NULL) {
74 xfree(list->ids);
75 }
76}
77
82static inline uint32_t
83pm_constant_pool_hash(const uint8_t *start, size_t length) {
84 // This is a prime number used as the initial value for the hash function.
85 uint32_t value = 5381;
86
87 for (size_t index = 0; index < length; index++) {
88 value = ((value << 5) + value) + start[index];
89 }
90
91 return value;
92}
93
97static uint32_t
98next_power_of_two(uint32_t v) {
99 // Avoid underflow in subtraction on next line.
100 if (v == 0) {
101 // 1 is the nearest power of 2 to 0 (2^0)
102 return 1;
103 }
104 v--;
105 v |= v >> 1;
106 v |= v >> 2;
107 v |= v >> 4;
108 v |= v >> 8;
109 v |= v >> 16;
110 v++;
111 return v;
112}
113
114#ifndef NDEBUG
115static bool
116is_power_of_two(uint32_t size) {
117 return (size & (size - 1)) == 0;
118}
119#endif
120
124static inline bool
125pm_constant_pool_resize(pm_constant_pool_t *pool) {
126 assert(is_power_of_two(pool->capacity));
127
128 uint32_t next_capacity = pool->capacity * 2;
129 if (next_capacity < pool->capacity) return false;
130
131 const uint32_t mask = next_capacity - 1;
132 const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
133
134 void *next = xcalloc(next_capacity, element_size);
135 if (next == NULL) return false;
136
137 pm_constant_pool_bucket_t *next_buckets = next;
138 pm_constant_t *next_constants = (void *)(((char *) next) + next_capacity * sizeof(pm_constant_pool_bucket_t));
139
140 // For each bucket in the current constant pool, find the index in the
141 // next constant pool, and insert it.
142 for (uint32_t index = 0; index < pool->capacity; index++) {
143 pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
144
145 // If an id is set on this constant, then we know we have content here.
146 // In this case we need to insert it into the next constant pool.
147 if (bucket->id != PM_CONSTANT_ID_UNSET) {
148 uint32_t next_index = bucket->hash & mask;
149
150 // This implements linear scanning to find the next available slot
151 // in case this index is already taken. We don't need to bother
152 // comparing the values since we know that the hash is unique.
153 while (next_buckets[next_index].id != PM_CONSTANT_ID_UNSET) {
154 next_index = (next_index + 1) & mask;
155 }
156
157 // Here we copy over the entire bucket, which includes the id so
158 // that they are consistent between resizes.
159 next_buckets[next_index] = *bucket;
160 }
161 }
162
163 // The constants are stable with respect to hash table resizes.
164 memcpy(next_constants, pool->constants, pool->size * sizeof(pm_constant_t));
165
166 // pool->constants and pool->buckets are allocated out of the same chunk
167 // of memory, with the buckets coming first.
168 xfree(pool->buckets);
169 pool->constants = next_constants;
170 pool->buckets = next_buckets;
171 pool->capacity = next_capacity;
172 return true;
173}
174
178bool
179pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) {
180 const uint32_t maximum = (~((uint32_t) 0));
181 if (capacity >= ((maximum / 2) + 1)) return false;
182
183 capacity = next_power_of_two(capacity);
184 const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
185 void *memory = xcalloc(capacity, element_size);
186 if (memory == NULL) return false;
187
188 pool->buckets = memory;
189 pool->constants = (void *)(((char *)memory) + capacity * sizeof(pm_constant_pool_bucket_t));
190 pool->size = 0;
191 pool->capacity = capacity;
192 return true;
193}
194
198 pm_constant_t *
199pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
200 assert(constant_id != PM_CONSTANT_ID_UNSET && constant_id <= pool->size);
201 return &pool->constants[constant_id - 1];
202}
203
208 pm_constant_id_t
209pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
210 assert(is_power_of_two(pool->capacity));
211 const uint32_t mask = pool->capacity - 1;
212
213 uint32_t hash = pm_constant_pool_hash(start, length);
214 uint32_t index = hash & mask;
215 pm_constant_pool_bucket_t *bucket;
216
217 while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
218 pm_constant_t *constant = &pool->constants[bucket->id - 1];
219 if ((constant->length == length) && memcmp(constant->start, start, length) == 0) {
220 return bucket->id;
221 }
222
223 index = (index + 1) & mask;
224 }
225
226 return PM_CONSTANT_ID_UNSET;
227}
228
232static inline pm_constant_id_t
233pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) {
234 if (pool->size >= (pool->capacity / 4 * 3)) {
235 if (!pm_constant_pool_resize(pool)) return PM_CONSTANT_ID_UNSET;
236 }
237
238 assert(is_power_of_two(pool->capacity));
239 const uint32_t mask = pool->capacity - 1;
240
241 uint32_t hash = pm_constant_pool_hash(start, length);
242 uint32_t index = hash & mask;
243 pm_constant_pool_bucket_t *bucket;
244
245 while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
246 // If there is a collision, then we need to check if the content is the
247 // same as the content we are trying to insert. If it is, then we can
248 // return the id of the existing constant.
249 pm_constant_t *constant = &pool->constants[bucket->id - 1];
250
251 if ((constant->length == length) && memcmp(constant->start, start, length) == 0) {
252 // Since we have found a match, we need to check if this is
253 // attempting to insert a shared or an owned constant. We want to
254 // prefer shared constants since they don't require allocations.
255 if (type == PM_CONSTANT_POOL_BUCKET_OWNED) {
256 // If we're attempting to insert an owned constant and we have
257 // an existing constant, then either way we don't want the given
258 // memory. Either it's duplicated with the existing constant or
259 // it's not necessary because we have a shared version.
260 xfree((void *) start);
261 } else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
262 // If we're attempting to insert a shared constant and the
263 // existing constant is owned, then we can free the owned
264 // constant and replace it with the shared constant.
265 xfree((void *) constant->start);
266 constant->start = start;
267 bucket->type = (unsigned int) (type & 0x3);
268 }
269
270 return bucket->id;
271 }
272
273 index = (index + 1) & mask;
274 }
275
276 // IDs are allocated starting at 1, since the value 0 denotes a non-existent
277 // constant.
278 uint32_t id = ++pool->size;
279 assert(pool->size < ((uint32_t) (1 << 30)));
280
281 *bucket = (pm_constant_pool_bucket_t) {
282 .id = (unsigned int) (id & 0x3fffffff),
283 .type = (unsigned int) (type & 0x3),
284 .hash = hash
285 };
286
287 pool->constants[id - 1] = (pm_constant_t) {
288 .start = start,
289 .length = length,
290 };
291
292 return id;
293}
294
299 pm_constant_id_t
300pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
301 return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT);
302}
303
309 pm_constant_id_t
310pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length) {
311 return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
312}
313
319 pm_constant_id_t
320pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
321 return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT);
322}
323
327void
328pm_constant_pool_free(pm_constant_pool_t *pool) {
329 // For each constant in the current constant pool, free the contents if the
330 // contents are owned.
331 for (uint32_t index = 0; index < pool->capacity; index++) {
332 pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
333
334 // If an id is set on this constant, then we know we have content here.
335 if (bucket->id != PM_CONSTANT_ID_UNSET && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
336 pm_constant_t *constant = &pool->constants[bucket->id - 1];
337 xfree((void *) constant->start);
338 }
339 }
340
341 xfree(pool->buckets);
342}
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
xrealloc
#define xrealloc
Old name of ruby_xrealloc.
Definition xmalloc.h:56
xcalloc
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
pm_constant_pool.h
A data structure that stores a set of strings.
PM_CONSTANT_POOL_BUCKET_DEFAULT
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT
By default, each constant is a slice of the source.
Definition pm_constant_pool.h:104
PM_CONSTANT_ID_UNSET
#define PM_CONSTANT_ID_UNSET
When we allocate constants into the pool, we reserve 0 to mean that the slot is not yet filled.
Definition pm_constant_pool.h:26
pm_constant_pool_bucket_type_t
unsigned int pm_constant_pool_bucket_type_t
The type of bucket in the constant pool hash map.
Definition pm_constant_pool.h:101
pm_constant_id_t
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
Definition pm_constant_pool.h:31
PM_CONSTANT_POOL_BUCKET_OWNED
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED
An owned constant is one for which memory has been allocated.
Definition pm_constant_pool.h:107
PM_CONSTANT_POOL_BUCKET_CONSTANT
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT
A constant constant is known at compile time.
Definition pm_constant_pool.h:110
pm_constant_id_list_t
A list of constant IDs.
Definition pm_constant_pool.h:36
pm_constant_id_list_t::size
size_t size
The number of constant ids in the list.
Definition pm_constant_pool.h:38
pm_constant_id_list_t::capacity
size_t capacity
The number of constant ids that have been allocated in the list.
Definition pm_constant_pool.h:41
pm_constant_id_list_t::ids
pm_constant_id_t * ids
The constant ids in the list.
Definition pm_constant_pool.h:44
pm_constant_pool_bucket_t
A bucket in the hash map.
Definition pm_constant_pool.h:113
pm_constant_pool_bucket_t::hash
uint32_t hash
The hash of the bucket.
Definition pm_constant_pool.h:121
pm_constant_pool_bucket_t::id
unsigned int id
The incremental ID used for indexing back into the pool.
Definition pm_constant_pool.h:115
pm_constant_pool_bucket_t::type
pm_constant_pool_bucket_type_t type
The type of the bucket, which determines how to free it.
Definition pm_constant_pool.h:118
pm_constant_pool_t
The overall constant pool, which stores constants found while parsing.
Definition pm_constant_pool.h:134
pm_constant_pool_t::capacity
uint32_t capacity
The number of buckets that have been allocated in the hash map.
Definition pm_constant_pool.h:145
pm_constant_pool_t::buckets
pm_constant_pool_bucket_t * buckets
The buckets in the hash map.
Definition pm_constant_pool.h:136
pm_constant_pool_t::size
uint32_t size
The number of buckets in the hash map.
Definition pm_constant_pool.h:142
pm_constant_pool_t::constants
pm_constant_t * constants
The constants that are stored in the buckets.
Definition pm_constant_pool.h:139
pm_constant_t
A constant in the pool which effectively stores a string.
Definition pm_constant_pool.h:125
pm_constant_t::length
size_t length
The length of the string.
Definition pm_constant_pool.h:130
pm_constant_t::start
const uint8_t * start
A pointer to the start of the string.
Definition pm_constant_pool.h:127