Line | Count | Source |
1 | | #include "prism/internal/constant_pool.h" |
2 | | |
3 | | #include "prism/compiler/align.h" |
4 | | #include "prism/compiler/inline.h" |
5 | | #include "prism/internal/arena.h" |
6 | | #include "prism/internal/assert.h" |
7 | | |
8 | | #include <stdbool.h> |
9 | | |
10 | | /** |
11 | | * Initialize a list of constant ids. |
12 | | */ |
13 | | void |
14 | | pm_constant_id_list_init(pm_constant_id_list_t *list) { |
15 | | list->ids = NULL; |
16 | | list->size = 0; |
17 | | list->capacity = 0; |
18 | | } |
19 | | |
20 | | /** |
21 | | * Initialize a list of constant ids with a given capacity. |
22 | | */ |
23 | | void |
24 | 58.0k | pm_constant_id_list_init_capacity(pm_arena_t *arena, pm_constant_id_list_t *list, size_t capacity) { |
25 | 58.0k | if (capacity) { Branch (25:9): [True: 21.7k, False: 36.2k]
|
26 | 21.7k | list->ids = (pm_constant_id_t *) pm_arena_zalloc(arena, capacity * sizeof(pm_constant_id_t), PRISM_ALIGNOF(pm_constant_id_t)); |
27 | 36.2k | } else { |
28 | 36.2k | list->ids = NULL; |
29 | 36.2k | } |
30 | | |
31 | 58.0k | list->size = 0; |
32 | 58.0k | list->capacity = capacity; |
33 | 58.0k | } |
34 | | |
35 | | /** |
36 | | * Append a constant id to a list of constant ids. |
37 | | */ |
38 | | void |
39 | | pm_constant_id_list_append(pm_arena_t *arena, pm_constant_id_list_t *list, pm_constant_id_t id) { |
40 | | if (list->size >= list->capacity) { |
41 | | size_t new_capacity = list->capacity == 0 ? 8 : list->capacity * 2; |
42 | | pm_constant_id_t *new_ids = (pm_constant_id_t *) pm_arena_alloc(arena, sizeof(pm_constant_id_t) * new_capacity, PRISM_ALIGNOF(pm_constant_id_t)); |
43 | | |
44 | | if (list->size > 0) { |
45 | | memcpy(new_ids, list->ids, list->size * sizeof(pm_constant_id_t)); |
46 | | } |
47 | | |
48 | | list->ids = new_ids; |
49 | | list->capacity = new_capacity; |
50 | | } |
51 | | |
52 | | list->ids[list->size++] = id; |
53 | | } |
54 | | |
55 | | /** |
56 | | * Insert a constant id into a list of constant ids at the specified index. |
57 | | */ |
58 | | void |
59 | 52.1k | pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id) { |
60 | 52.1k | PRISM_ASSERT(index < list->capacity); Branch (60:5): [True: 0, False: 52.1k]
Branch (60:5): [True: 52.1k, False: 0]
|
61 | 52.1k | PRISM_ASSERT(list->ids[index] == PM_CONSTANT_ID_UNSET); Branch (61:5): [True: 0, False: 52.1k]
Branch (61:5): [True: 52.1k, False: 0]
|
62 | | |
63 | 52.1k | list->ids[index] = id; |
64 | 52.1k | list->size++; |
65 | 52.1k | } |
66 | | |
67 | | /** |
68 | | * The slot that holds the given id, or the empty slot it would occupy. The |
69 | | * table is never full, so this terminates. |
70 | | */ |
71 | | static PRISM_INLINE size_t |
72 | 18.7k | pm_constant_id_set_slot(const pm_constant_id_set_t *set, pm_constant_id_t id) { |
73 | 18.7k | PRISM_ASSERT(set->capacity != 0); Branch (73:5): [True: 0, False: 18.7k]
Branch (73:5): [True: 18.7k, False: 0]
|
74 | | |
75 | 18.7k | size_t mask = set->capacity - 1; |
76 | 18.7k | size_t index = ((size_t) pm_constant_id_hash(id)) & mask; |
77 | | |
78 | 25.0k | while (set->ids[index] != PM_CONSTANT_ID_UNSET && set->ids[index] != id6.57k ) { Branch (78:12): [True: 6.57k, False: 18.4k]
Branch (78:55): [True: 6.28k, False: 283]
|
79 | 6.28k | index = (index + 1) & mask; |
80 | 6.28k | } |
81 | | |
82 | 18.7k | return index; |
83 | 18.7k | } |
84 | | |
85 | | /** |
86 | | * Add a constant id to the set, reporting whether it was not already there. |
87 | | */ |
88 | | bool |
89 | 12.7k | pm_constant_id_set_insert(pm_arena_t *arena, pm_constant_id_set_t *set, pm_constant_id_t id) { |
90 | | /* PM_CONSTANT_ID_UNSET marks an empty slot, so it cannot also be a member. */ |
91 | 12.7k | PRISM_ASSERT(id != PM_CONSTANT_ID_UNSET); Branch (91:5): [True: 0, False: 12.7k]
Branch (91:5): [True: 12.7k, False: 0]
|
92 | | |
93 | | /* |
94 | | * Grow at the same load factor the locals table and the constant pool use. |
95 | | * An empty set has no slots at all, so this also builds the first table. |
96 | | */ |
97 | 12.7k | if (set->size >= (set->capacity / 4 * 3)) { Branch (97:9): [True: 6.67k, False: 6.11k]
|
98 | 6.67k | size_t capacity = set->capacity == 0 ? 85.90k : set->capacity * 2768 ; Branch (98:27): [True: 5.90k, False: 768]
|
99 | 6.67k | pm_constant_id_set_t grown = { |
100 | 6.67k | .size = set->size, |
101 | 6.67k | .capacity = capacity, |
102 | 6.67k | .ids = (pm_constant_id_t *) pm_arena_zalloc(arena, capacity * sizeof(pm_constant_id_t), PRISM_ALIGNOF(pm_constant_id_t)) |
103 | 6.67k | }; |
104 | | |
105 | 14.6k | for (size_t index = 0; index < set->capacity; index++7.93k ) { Branch (105:32): [True: 7.93k, False: 6.67k]
|
106 | 7.93k | pm_constant_id_t moved = set->ids[index]; |
107 | 7.93k | if (moved != PM_CONSTANT_ID_UNSET) grown.ids[pm_constant_id_set_slot(&grown, moved)] = moved5.95k ; Branch (107:17): [True: 5.95k, False: 1.98k]
|
108 | 7.93k | } |
109 | | |
110 | 6.67k | *set = grown; |
111 | 6.67k | } |
112 | | |
113 | 12.7k | size_t index = pm_constant_id_set_slot(set, id); |
114 | 12.7k | if (set->ids[index] == id) return 283 false283 ; Branch (114:9): [True: 283, False: 12.5k]
|
115 | | |
116 | 12.5k | set->ids[index] = id; |
117 | 12.5k | set->size++; |
118 | 12.5k | return true; |
119 | 12.7k | } |
120 | | |
121 | | /** |
122 | | * A multiply-xorshift hash that processes input a word at a time. This is |
123 | | * significantly faster than the byte-at-a-time djb2 hash for the short strings |
124 | | * typical in Ruby source (~15 bytes average). Each word is mixed into the hash |
125 | | * by XOR followed by multiplication by a large odd constant, which spreads |
126 | | * entropy across all bits. A final xorshift fold produces the 32-bit result. |
127 | | */ |
128 | | static PRISM_INLINE uint32_t |
129 | 385k | pm_constant_pool_hash(const uint8_t *start, size_t length) { |
130 | | // This constant is borrowed from wyhash. It is a 64-bit odd integer with |
131 | | // roughly equal 0/1 bits, chosen for good avalanche behavior when used in |
132 | | // multiply-xorshift sequences. |
133 | 385k | static const uint64_t secret = 0x517cc1b727220a95ULL; |
134 | 385k | uint64_t hash = (uint64_t) length; |
135 | | |
136 | 385k | if (length <= 8) { Branch (136:9): [True: 378k, False: 6.99k]
|
137 | | // Short strings: read first and last 4 bytes (overlapping for len < 8). |
138 | | // This covers the majority of Ruby identifiers with a single multiply. |
139 | 378k | if (length >= 4) { Branch (139:13): [True: 42.5k, False: 336k]
|
140 | 42.5k | uint32_t a, b; |
141 | 42.5k | memcpy(&a, start, 4); |
142 | 42.5k | memcpy(&b, start + length - 4, 4); |
143 | 42.5k | hash ^= (uint64_t) a | ((uint64_t) b << 32); |
144 | 336k | } else if (length > 0) { Branch (144:20): [True: 331k, False: 4.05k]
|
145 | 331k | hash ^= (uint64_t) start[0] | ((uint64_t) start[length >> 1] << 8) | ((uint64_t) start[length - 1] << 16); |
146 | 331k | } |
147 | 378k | hash *= secret; |
148 | 378k | } else if (6.99k length <= 166.99k ) { Branch (148:16): [True: 5.25k, False: 1.74k]
|
149 | | // Medium strings: read first and last 8 bytes (overlapping). |
150 | | // Two multiplies instead of the three the loop-based approach needs. |
151 | 5.25k | uint64_t word; |
152 | 5.25k | memcpy(&word, start, 8); |
153 | 5.25k | hash ^= word; |
154 | 5.25k | hash *= secret; |
155 | 5.25k | memcpy(&word, start + length - 8, 8); |
156 | 5.25k | hash ^= word; |
157 | 5.25k | hash *= secret; |
158 | 5.25k | } else { |
159 | 1.74k | const uint8_t *ptr = start; |
160 | 1.74k | size_t remaining = length; |
161 | | |
162 | 7.05k | while (remaining >= 8) { Branch (162:16): [True: 5.31k, False: 1.74k]
|
163 | 5.31k | uint64_t word; |
164 | 5.31k | memcpy(&word, ptr, 8); |
165 | 5.31k | hash ^= word; |
166 | 5.31k | hash *= secret; |
167 | 5.31k | ptr += 8; |
168 | 5.31k | remaining -= 8; |
169 | 5.31k | } |
170 | | |
171 | 1.74k | if (remaining > 0) { Branch (171:13): [True: 1.55k, False: 183]
|
172 | | // Read the last 8 bytes (overlapping with already-processed data). |
173 | 1.55k | uint64_t word; |
174 | 1.55k | memcpy(&word, start + length - 8, 8); |
175 | 1.55k | hash ^= word; |
176 | 1.55k | hash *= secret; |
177 | 1.55k | } |
178 | 1.74k | } |
179 | | |
180 | 385k | hash ^= hash >> 32; |
181 | 385k | return (uint32_t) hash; |
182 | 385k | } |
183 | | |
184 | | /** |
185 | | * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 |
186 | | */ |
187 | | static uint32_t |
188 | 14.6k | next_power_of_two(uint32_t v) { |
189 | | // Avoid underflow in subtraction on next line. |
190 | 14.6k | if (v == 0) { Branch (190:9): [True: 0, False: 14.6k]
|
191 | | // 1 is the nearest power of 2 to 0 (2^0) |
192 | 0 | return 1; |
193 | 0 | } |
194 | 14.6k | v--; |
195 | 14.6k | v |= v >> 1; |
196 | 14.6k | v |= v >> 2; |
197 | 14.6k | v |= v >> 4; |
198 | 14.6k | v |= v >> 8; |
199 | 14.6k | v |= v >> 16; |
200 | 14.6k | v++; |
201 | 14.6k | return v; |
202 | 14.6k | } |
203 | | |
204 | | /** |
205 | | * Resize a constant pool to a given capacity. |
206 | | */ |
207 | | static PRISM_INLINE void |
208 | 26.1k | pm_constant_pool_resize(pm_arena_t *arena, pm_constant_pool_t *pool) { |
209 | 26.1k | uint32_t next_capacity = pool->capacity * 2; |
210 | 26.1k | const uint32_t mask = next_capacity - 1; |
211 | | |
212 | 26.1k | pm_constant_pool_bucket_t *next_buckets = (pm_constant_pool_bucket_t *) pm_arena_zalloc(arena, next_capacity * sizeof(pm_constant_pool_bucket_t), PRISM_ALIGNOF(pm_constant_pool_bucket_t)); |
213 | 26.1k | pm_constant_t *next_constants = (pm_constant_t *) pm_arena_alloc(arena, next_capacity * sizeof(pm_constant_t), PRISM_ALIGNOF(pm_constant_t)); |
214 | | |
215 | | // For each bucket in the current constant pool, find the index in the |
216 | | // next constant pool, and insert it. |
217 | 280k | for (uint32_t index = 0; index < pool->capacity; index++254k ) { Branch (217:30): [True: 254k, False: 26.1k]
|
218 | 254k | pm_constant_pool_bucket_t *bucket = &pool->buckets[index]; |
219 | | |
220 | | // If an id is set on this constant, then we know we have content here. |
221 | | // In this case we need to insert it into the next constant pool. |
222 | 254k | if (bucket->id != PM_CONSTANT_ID_UNSET) { Branch (222:13): [True: 190k, False: 63.6k]
|
223 | 190k | uint32_t next_index = bucket->hash & mask; |
224 | | |
225 | | // This implements linear scanning to find the next available slot |
226 | | // in case this index is already taken. We don't need to bother |
227 | | // comparing the values since we know that the hash is unique. |
228 | 243k | while (next_buckets[next_index].id != PM_CONSTANT_ID_UNSET) { Branch (228:20): [True: 52.3k, False: 190k]
|
229 | 52.3k | next_index = (next_index + 1) & mask; |
230 | 52.3k | } |
231 | | |
232 | | // Here we copy over the entire bucket, which includes the id so |
233 | | // that they are consistent between resizes. |
234 | 190k | next_buckets[next_index] = *bucket; |
235 | 190k | } |
236 | 254k | } |
237 | | |
238 | | // The constants are stable with respect to hash table resizes. |
239 | 26.1k | memcpy(next_constants, pool->constants, pool->size * sizeof(pm_constant_t)); |
240 | | |
241 | 26.1k | pool->constants = next_constants; |
242 | 26.1k | pool->buckets = next_buckets; |
243 | 26.1k | pool->capacity = next_capacity; |
244 | 26.1k | } |
245 | | |
246 | | /** |
247 | | * Initialize a new constant pool with a given capacity. |
248 | | */ |
249 | | void |
250 | 14.6k | pm_constant_pool_init(pm_arena_t *arena, pm_constant_pool_t *pool, uint32_t capacity) { |
251 | 14.6k | capacity = next_power_of_two(capacity); |
252 | | |
253 | 14.6k | pool->buckets = (pm_constant_pool_bucket_t *) pm_arena_zalloc(arena, capacity * sizeof(pm_constant_pool_bucket_t), PRISM_ALIGNOF(pm_constant_pool_bucket_t)); |
254 | 14.6k | pool->constants = (pm_constant_t *) pm_arena_alloc(arena, capacity * sizeof(pm_constant_t), PRISM_ALIGNOF(pm_constant_t)); |
255 | 14.6k | pool->size = 0; |
256 | 14.6k | pool->capacity = capacity; |
257 | 14.6k | } |
258 | | |
259 | | /** |
260 | | * Return a pointer to the constant indicated by the given constant id. |
261 | | */ |
262 | | pm_constant_t * |
263 | 768k | pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) { |
264 | 768k | PRISM_ASSERT(constant_id != PM_CONSTANT_ID_UNSET && constant_id <= pool->size); Branch (264:5): [True: 0, False: 768k]
Branch (264:5): [True: 0, False: 0]
Branch (264:5): [True: 768k, False: 0]
Branch (264:5): [True: 768k, False: 0]
|
265 | 768k | return &pool->constants[constant_id - 1]; |
266 | 768k | } |
267 | | |
268 | | /** |
269 | | * Find a constant in a constant pool. Returns the id of the constant, or 0 if |
270 | | * the constant is not found. |
271 | | */ |
272 | | pm_constant_id_t |
273 | | pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length) { |
274 | | const uint32_t mask = pool->capacity - 1; |
275 | | |
276 | | uint32_t hash = pm_constant_pool_hash(start, length); |
277 | | uint32_t index = hash & mask; |
278 | | pm_constant_pool_bucket_t *bucket; |
279 | | |
280 | | while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) { |
281 | | // Compare the stored hash before touching the contents so that probe |
282 | | // collisions are rejected without a memcmp call. |
283 | | if ((bucket->hash == hash) && (bucket->length == length) && memcmp(bucket->start, start, length) == 0) { |
284 | | return bucket->id; |
285 | | } |
286 | | |
287 | | index = (index + 1) & mask; |
288 | | } |
289 | | |
290 | | return PM_CONSTANT_ID_UNSET; |
291 | | } |
292 | | |
293 | | /** |
294 | | * Insert a constant into a constant pool and return its index in the pool. |
295 | | */ |
296 | | static PRISM_INLINE pm_constant_id_t |
297 | 385k | pm_constant_pool_insert(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) { |
298 | 385k | if (pool->size >= (pool->capacity / 4 * 3)) { Branch (298:9): [True: 26.1k, False: 359k]
|
299 | 26.1k | pm_constant_pool_resize(arena, pool); |
300 | 26.1k | } |
301 | | |
302 | 385k | const uint32_t mask = pool->capacity - 1; |
303 | | |
304 | 385k | uint32_t hash = pm_constant_pool_hash(start, length); |
305 | 385k | uint32_t index = hash & mask; |
306 | 385k | pm_constant_pool_bucket_t *bucket; |
307 | | |
308 | 656k | while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) { Branch (308:12): [True: 487k, False: 168k]
|
309 | | // If there is a collision, then we need to check if the content is the |
310 | | // same as the content we are trying to insert. If it is, then we can |
311 | | // return the id of the existing constant. Compare the stored hash |
312 | | // first so that probe collisions are rejected without a memcmp call. |
313 | 487k | if ((bucket->hash == hash) && (bucket->length == length)217k && memcmp(bucket->start, start, length) == 0216k ) { Branch (313:13): [True: 217k, False: 270k]
Branch (313:39): [True: 216k, False: 883]
Branch (313:69): [True: 216k, False: 0]
|
314 | | // Since we have found a match, we need to check if this is |
315 | | // attempting to insert a shared or an owned constant. We want to |
316 | | // prefer shared constants since they don't require allocations. |
317 | 216k | if (type != PM_CONSTANT_POOL_BUCKET_OWNED && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED215k ) { Branch (317:17): [True: 215k, False: 1.47k]
Branch (317:58): [True: 1.55k, False: 213k]
|
318 | | // If we're attempting to insert a shared constant and the |
319 | | // existing constant is owned, then we can replace it with the |
320 | | // shared constant to prefer non-owned references. |
321 | 1.55k | bucket->start = start; |
322 | 1.55k | bucket->type = (unsigned int) (type & 0x3); |
323 | 1.55k | pool->constants[bucket->id - 1].start = start; |
324 | 1.55k | } |
325 | | |
326 | 216k | return bucket->id; |
327 | 216k | } |
328 | | |
329 | 271k | index = (index + 1) & mask; |
330 | 271k | } |
331 | | |
332 | | // IDs are allocated starting at 1, since the value 0 denotes a non-existent |
333 | | // constant. |
334 | 168k | uint32_t id = ++pool->size; |
335 | 168k | PRISM_ASSERT(pool->size < ((uint32_t) (1 << 30))); Branch (335:5): [True: 0, False: 168k]
Branch (335:5): [True: 168k, False: 0]
|
336 | | |
337 | 168k | *bucket = (pm_constant_pool_bucket_t) { |
338 | 168k | .id = (unsigned int) (id & 0x3fffffff), |
339 | 168k | .type = (unsigned int) (type & 0x3), |
340 | 168k | .hash = hash, |
341 | 168k | .start = start, |
342 | 168k | .length = length |
343 | 168k | }; |
344 | | |
345 | 168k | pool->constants[id - 1] = (pm_constant_t) { |
346 | 168k | .start = start, |
347 | 168k | .length = length, |
348 | 168k | }; |
349 | | |
350 | 168k | return id; |
351 | 168k | } |
352 | | |
353 | | /** |
354 | | * Insert a constant into a constant pool. Returns the id of the constant, or |
355 | | * PM_CONSTANT_ID_UNSET if any potential calls to resize fail. |
356 | | */ |
357 | | pm_constant_id_t |
358 | 344k | pm_constant_pool_insert_shared(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length) { |
359 | 344k | return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT); |
360 | 344k | } |
361 | | |
362 | | /** |
363 | | * Insert a constant into a constant pool from memory that is now owned by the |
364 | | * constant pool. Returns the id of the constant, or PM_CONSTANT_ID_UNSET if any |
365 | | * potential calls to resize fail. |
366 | | */ |
367 | | pm_constant_id_t |
368 | 13.6k | pm_constant_pool_insert_owned(pm_arena_t *arena, pm_constant_pool_t *pool, uint8_t *start, size_t length) { |
369 | 13.6k | return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED); |
370 | 13.6k | } |
371 | | |
372 | | /** |
373 | | * Insert a constant into a constant pool from memory that is constant. Returns |
374 | | * the id of the constant, or PM_CONSTANT_ID_UNSET if any potential calls to |
375 | | * resize fail. |
376 | | */ |
377 | | pm_constant_id_t |
378 | 27.7k | pm_constant_pool_insert_constant(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length) { |
379 | 27.7k | return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT); |
380 | 27.7k | } |
381 | | |
382 | | /** |
383 | | * Return a raw pointer to the start of a constant. |
384 | | */ |
385 | | const uint8_t * |
386 | | pm_constant_start(const pm_constant_t *constant) { |
387 | | return constant->start; |
388 | | } |
389 | | |
390 | | /** |
391 | | * Return the length of a constant. |
392 | | */ |
393 | | size_t pm_constant_length(const pm_constant_t *constant) { |
394 | | return constant->length; |
395 | | } |