1use super::*;
2
3impl Arena {
4 pub fn new() -> Self {
5 Arena {
6 young_entries: Vec::with_capacity(256),
7 yard_entries: Vec::with_capacity(64),
8 handoff_entries: Vec::with_capacity(64),
9 stable_entries: Vec::with_capacity(64),
10 scratch_young: Vec::new(),
11 scratch_yard: Vec::new(),
12 scratch_handoff: Vec::new(),
13 scratch_stable: Vec::new(),
14 peak_usage: ArenaUsage::default(),
15 alloc_space: AllocSpace::Young,
16 type_names: Vec::new(),
17 type_field_names: Vec::new(),
18 type_variant_names: Vec::new(),
19 type_variant_ctor_ids: Vec::new(),
20 ctor_to_type_variant: Vec::new(),
21 symbol_entries: Vec::new(),
22 }
23 }
24
25 #[inline]
26 pub fn push(&mut self, entry: ArenaEntry) -> u32 {
27 match &entry {
28 ArenaEntry::Fn(_) | ArenaEntry::Builtin(_) | ArenaEntry::Namespace { .. } => {}
29 _ => {
30 return match self.alloc_space {
31 AllocSpace::Young => {
32 let idx = self.young_entries.len() as u32;
33 self.young_entries.push(entry);
34 self.note_peak_usage();
35 Self::encode_index(HeapSpace::Young, idx)
36 }
37 AllocSpace::Yard => {
38 let idx = self.yard_entries.len() as u32;
39 self.yard_entries.push(entry);
40 self.note_peak_usage();
41 Self::encode_index(HeapSpace::Yard, idx)
42 }
43 AllocSpace::Handoff => {
44 let idx = self.handoff_entries.len() as u32;
45 self.handoff_entries.push(entry);
46 self.note_peak_usage();
47 Self::encode_index(HeapSpace::Handoff, idx)
48 }
49 };
50 }
51 }
52 match entry {
53 ArenaEntry::Fn(f) => self.push_symbol(ArenaSymbol::Fn(f)),
54 ArenaEntry::Builtin(name) => self.push_symbol(ArenaSymbol::Builtin(name)),
55 ArenaEntry::Namespace { name, members } => {
56 self.push_symbol(ArenaSymbol::Namespace { name, members })
57 }
58 _ => unreachable!("non-symbol entry already returned above"),
59 }
60 }
61
62 #[inline]
63 pub fn push_symbol(&mut self, symbol: ArenaSymbol) -> u32 {
64 let idx = self.symbol_entries.len() as u32;
65 self.symbol_entries.push(symbol);
66 idx
67 }
68
69 #[inline]
70 pub fn get(&self, index: u32) -> &ArenaEntry {
71 let (space, raw_index) = Self::decode_index(index);
72 match space {
73 HeapSpace::Young => &self.young_entries[raw_index as usize],
74 HeapSpace::Yard => &self.yard_entries[raw_index as usize],
75 HeapSpace::Handoff => &self.handoff_entries[raw_index as usize],
76 HeapSpace::Stable => &self.stable_entries[raw_index as usize],
77 }
78 }
79
80 #[inline]
81 pub(super) fn encode_index(space: HeapSpace, index: u32) -> u32 {
82 ((space as u32) << HEAP_SPACE_SHIFT) | index
83 }
84
85 #[inline]
86 pub(super) fn encode_yard_index(index: u32) -> u32 {
87 Self::encode_index(HeapSpace::Yard, index)
88 }
89
90 #[inline]
91 pub(super) fn encode_stable_index(index: u32) -> u32 {
92 Self::encode_index(HeapSpace::Stable, index)
93 }
94
95 #[inline]
96 pub(super) fn encode_handoff_index(index: u32) -> u32 {
97 Self::encode_index(HeapSpace::Handoff, index)
98 }
99
100 #[inline]
101 pub(super) fn decode_index(index: u32) -> (HeapSpace, u32) {
102 let space = match (index & HEAP_SPACE_MASK_U32) >> HEAP_SPACE_SHIFT {
103 0 => HeapSpace::Young,
104 1 => HeapSpace::Yard,
105 2 => HeapSpace::Handoff,
106 3 => HeapSpace::Stable,
107 _ => unreachable!("invalid heap space bits"),
108 };
109 (space, index & HEAP_INDEX_MASK_U32)
110 }
111
112 #[inline]
113 pub fn is_stable_index(index: u32) -> bool {
114 matches!(Self::decode_index(index).0, HeapSpace::Stable)
115 }
116
117 #[inline]
118 pub fn is_yard_index_in_region(&self, index: u32, mark: u32) -> bool {
119 let (space, raw_index) = Self::decode_index(index);
120 matches!(space, HeapSpace::Yard)
121 && raw_index >= mark
122 && raw_index < self.yard_entries.len() as u32
123 }
124
125 #[inline]
126 pub fn is_handoff_index_in_region(&self, index: u32, mark: u32) -> bool {
127 let (space, raw_index) = Self::decode_index(index);
128 matches!(space, HeapSpace::Handoff)
129 && raw_index >= mark
130 && raw_index < self.handoff_entries.len() as u32
131 }
132
133 #[inline]
134 pub fn is_young_index_in_region(&self, index: u32, mark: u32) -> bool {
135 let (space, raw_index) = Self::decode_index(index);
136 matches!(space, HeapSpace::Young)
137 && raw_index >= mark
138 && raw_index < self.young_entries.len() as u32
139 }
140
141 #[inline]
142 pub fn young_len(&self) -> usize {
143 self.young_entries.len()
144 }
145
146 #[inline]
147 pub fn yard_len(&self) -> usize {
148 self.yard_entries.len()
149 }
150
151 #[inline]
152 pub fn handoff_len(&self) -> usize {
153 self.handoff_entries.len()
154 }
155
156 #[inline]
157 pub fn stable_len(&self) -> usize {
158 self.stable_entries.len()
159 }
160
161 #[inline]
162 pub fn usage(&self) -> ArenaUsage {
163 ArenaUsage {
164 young: self.young_entries.len(),
165 yard: self.yard_entries.len(),
166 handoff: self.handoff_entries.len(),
167 stable: self.stable_entries.len(),
168 }
169 }
170
171 #[inline]
172 pub fn peak_usage(&self) -> ArenaUsage {
173 self.peak_usage
174 }
175
176 #[inline]
177 pub(super) fn note_peak_usage(&mut self) {
178 let usage = self.usage();
179 self.peak_usage.young = self.peak_usage.young.max(usage.young);
180 self.peak_usage.yard = self.peak_usage.yard.max(usage.yard);
181 self.peak_usage.handoff = self.peak_usage.handoff.max(usage.handoff);
182 self.peak_usage.stable = self.peak_usage.stable.max(usage.stable);
183 }
184
185 #[inline]
186 pub(super) fn take_u32_scratch(slot: &mut Vec<u32>, len: usize) -> Vec<u32> {
187 let mut scratch = std::mem::take(slot);
188 scratch.clear();
189 scratch.resize(len, u32::MAX);
190 scratch
191 }
192
193 #[inline]
194 pub(super) fn recycle_u32_scratch(slot: &mut Vec<u32>, mut scratch: Vec<u32>) {
195 scratch.clear();
196 *slot = scratch;
197 }
198
199 #[inline]
200 pub fn is_frame_local_index(
201 &self,
202 index: u32,
203 arena_mark: u32,
204 yard_mark: u32,
205 handoff_mark: u32,
206 ) -> bool {
207 self.is_young_index_in_region(index, arena_mark)
208 || self.is_yard_index_in_region(index, yard_mark)
209 || self.is_handoff_index_in_region(index, handoff_mark)
210 }
211
212 pub fn with_alloc_space<T>(&mut self, space: AllocSpace, f: impl FnOnce(&mut Arena) -> T) -> T {
213 let prev = self.alloc_space;
214 self.alloc_space = space;
215 let out = f(self);
216 self.alloc_space = prev;
217 out
218 }
219
220 pub fn push_i64(&mut self, val: i64) -> u32 {
223 self.push(ArenaEntry::Int(val))
224 }
225 pub fn push_string(&mut self, s: &str) -> u32 {
226 self.push(ArenaEntry::String(Rc::from(s)))
227 }
228 pub fn push_boxed(&mut self, val: NanValue) -> u32 {
229 self.push(ArenaEntry::Boxed(val))
230 }
231 pub fn push_record(&mut self, type_id: u32, fields: Vec<NanValue>) -> u32 {
232 self.push(ArenaEntry::Record { type_id, fields })
233 }
234 pub fn push_variant(&mut self, type_id: u32, variant_id: u16, fields: Vec<NanValue>) -> u32 {
235 self.push(ArenaEntry::Variant {
236 type_id,
237 variant_id,
238 fields,
239 })
240 }
241 pub fn push_list(&mut self, items: Vec<NanValue>) -> u32 {
242 self.push(ArenaEntry::List(ArenaList::Flat {
243 items: Rc::new(items),
244 start: 0,
245 }))
246 }
247 pub fn push_map(&mut self, map: PersistentMap) -> u32 {
248 self.push(ArenaEntry::Map(map))
249 }
250 pub fn push_tuple(&mut self, items: Vec<NanValue>) -> u32 {
251 self.push(ArenaEntry::Tuple(items))
252 }
253 pub fn push_fn(&mut self, f: Rc<FunctionValue>) -> u32 {
254 self.push_symbol(ArenaSymbol::Fn(f))
255 }
256 pub fn push_builtin(&mut self, name: &str) -> u32 {
257 self.push_symbol(ArenaSymbol::Builtin(Rc::from(name)))
258 }
259 pub fn push_nullary_variant_symbol(&mut self, ctor_id: u32) -> u32 {
260 self.push_symbol(ArenaSymbol::NullaryVariant { ctor_id })
261 }
262
263 pub fn get_i64(&self, index: u32) -> i64 {
266 match self.get(index) {
267 ArenaEntry::Int(i) => *i,
268 _ => panic!("Arena: expected Int at {}", index),
269 }
270 }
271 pub fn get_string(&self, index: u32) -> &str {
272 match self.get(index) {
273 ArenaEntry::String(s) => s,
274 other => panic!("Arena: expected String at {} but found {:?}", index, other),
275 }
276 }
277 pub fn get_string_value(&self, value: NanValue) -> NanString<'_> {
278 if let Some(s) = value.small_string() {
279 s
280 } else {
281 NanString::Borrowed(self.get_string(value.arena_index()))
282 }
283 }
284 pub fn get_boxed(&self, index: u32) -> NanValue {
285 match self.get(index) {
286 ArenaEntry::Boxed(v) => *v,
287 _ => panic!("Arena: expected Boxed at {}", index),
288 }
289 }
290 pub fn get_record(&self, index: u32) -> (u32, &[NanValue]) {
291 match self.get(index) {
292 ArenaEntry::Record { type_id, fields } => (*type_id, fields),
293 _ => panic!("Arena: expected Record at {}", index),
294 }
295 }
296 pub fn get_variant(&self, index: u32) -> (u32, u16, &[NanValue]) {
297 match self.get(index) {
298 ArenaEntry::Variant {
299 type_id,
300 variant_id,
301 fields,
302 } => (*type_id, *variant_id, fields),
303 other => panic!("Arena: expected Variant at {} but found {:?}", index, other),
304 }
305 }
306 pub fn get_list(&self, index: u32) -> &ArenaList {
307 match self.get(index) {
308 ArenaEntry::List(items) => items,
309 _ => panic!("Arena: expected List at {}", index),
310 }
311 }
312 pub fn get_tuple(&self, index: u32) -> &[NanValue] {
313 match self.get(index) {
314 ArenaEntry::Tuple(items) => items,
315 _ => panic!("Arena: expected Tuple at {}", index),
316 }
317 }
318 pub fn get_map(&self, index: u32) -> &PersistentMap {
319 match self.get(index) {
320 ArenaEntry::Map(map) => map,
321 _ => panic!("Arena: expected Map at {}", index),
322 }
323 }
324 pub fn map_ref_value(&self, map: NanValue) -> &PersistentMap {
325 static EMPTY_MAP: std::sync::OnceLock<PersistentMap> = std::sync::OnceLock::new();
326 if map.is_empty_map_immediate() {
327 return EMPTY_MAP.get_or_init(PersistentMap::new);
328 }
329 self.get_map(map.arena_index())
330 }
331 pub fn clone_map_value(&self, map: NanValue) -> PersistentMap {
332 if map.is_empty_map_immediate() {
333 PersistentMap::new()
334 } else {
335 self.get_map(map.arena_index()).clone()
336 }
337 }
338 pub fn get_fn(&self, index: u32) -> &FunctionValue {
339 match &self.symbol_entries[index as usize] {
340 ArenaSymbol::Fn(f) => f,
341 _ => panic!("Arena: expected Fn symbol at {}", index),
342 }
343 }
344 pub fn get_fn_rc(&self, index: u32) -> &Rc<FunctionValue> {
345 match &self.symbol_entries[index as usize] {
346 ArenaSymbol::Fn(f) => f,
347 _ => panic!("Arena: expected Fn symbol at {}", index),
348 }
349 }
350 pub fn get_builtin(&self, index: u32) -> &str {
351 match &self.symbol_entries[index as usize] {
352 ArenaSymbol::Builtin(s) => s,
353 _ => panic!("Arena: expected Builtin symbol at {}", index),
354 }
355 }
356 pub fn get_namespace(&self, index: u32) -> (&str, &[(Rc<str>, NanValue)]) {
357 match &self.symbol_entries[index as usize] {
358 ArenaSymbol::Namespace { name, members } => (name, members),
359 _ => panic!("Arena: expected Namespace symbol at {}", index),
360 }
361 }
362 pub fn get_nullary_variant_ctor(&self, index: u32) -> u32 {
363 match &self.symbol_entries[index as usize] {
364 ArenaSymbol::NullaryVariant { ctor_id } => *ctor_id,
365 _ => panic!("Arena: expected NullaryVariant symbol at {}", index),
366 }
367 }
368
369 pub fn register_record_type(&mut self, name: &str, field_names: Vec<String>) -> u32 {
372 let id = self.type_names.len() as u32;
373 self.type_names.push(name.to_string());
374 self.type_field_names.push(field_names);
375 self.type_variant_names.push(Vec::new());
376 self.type_variant_ctor_ids.push(Vec::new());
377 id
378 }
379
380 pub fn register_sum_type(&mut self, name: &str, variant_names: Vec<String>) -> u32 {
381 let id = self.type_names.len() as u32;
382 self.type_names.push(name.to_string());
383 self.type_field_names.push(Vec::new());
384 let ctor_ids: Vec<u32> = (0..variant_names.len())
385 .map(|variant_idx| {
386 let ctor_id = self.ctor_to_type_variant.len() as u32;
387 self.ctor_to_type_variant.push((id, variant_idx as u16));
388 ctor_id
389 })
390 .collect();
391 self.type_variant_names.push(variant_names);
392 self.type_variant_ctor_ids.push(ctor_ids);
393 id
394 }
395
396 pub fn register_variant_name(&mut self, type_id: u32, variant_name: String) -> u16 {
397 let variants = &mut self.type_variant_names[type_id as usize];
398 let variant_id = variants.len() as u16;
399 variants.push(variant_name);
400
401 let ctor_id = self.ctor_to_type_variant.len() as u32;
402 self.ctor_to_type_variant.push((type_id, variant_id));
403 self.type_variant_ctor_ids[type_id as usize].push(ctor_id);
404
405 variant_id
406 }
407
408 pub fn get_type_name(&self, type_id: u32) -> &str {
409 &self.type_names[type_id as usize]
410 }
411 pub fn type_count(&self) -> u32 {
412 self.type_names.len() as u32
413 }
414 pub fn get_field_names(&self, type_id: u32) -> &[String] {
415 &self.type_field_names[type_id as usize]
416 }
417 pub fn get_variant_name(&self, type_id: u32, variant_id: u16) -> &str {
418 &self.type_variant_names[type_id as usize][variant_id as usize]
419 }
420 pub fn find_type_id(&self, name: &str) -> Option<u32> {
421 self.type_names
422 .iter()
423 .position(|n| n == name)
424 .map(|i| i as u32)
425 }
426 pub fn find_variant_id(&self, type_id: u32, variant_name: &str) -> Option<u16> {
427 self.type_variant_names
428 .get(type_id as usize)?
429 .iter()
430 .position(|n| n == variant_name)
431 .map(|i| i as u16)
432 }
433
434 pub fn find_ctor_id(&self, type_id: u32, variant_id: u16) -> Option<u32> {
435 self.type_variant_ctor_ids
436 .get(type_id as usize)?
437 .get(variant_id as usize)
438 .copied()
439 }
440
441 pub fn get_ctor_parts(&self, ctor_id: u32) -> (u32, u16) {
442 self.ctor_to_type_variant
443 .get(ctor_id as usize)
444 .copied()
445 .unwrap_or_else(|| panic!("Arena: expected ctor id {} to be registered", ctor_id))
446 }
447
448 pub fn len(&self) -> usize {
449 self.young_entries.len()
450 + self.yard_entries.len()
451 + self.handoff_entries.len()
452 + self.stable_entries.len()
453 }
454 pub fn is_empty(&self) -> bool {
455 self.young_entries.is_empty()
456 && self.yard_entries.is_empty()
457 && self.handoff_entries.is_empty()
458 && self.stable_entries.is_empty()
459 }
460}
461
462impl Default for Arena {
463 fn default() -> Self {
464 Self::new()
465 }
466}