Skip to main content

eventql_parser/
arena.rs

1use crate::typing::{ArgsRef, Record, Type, TypeRef};
2use crate::{Attrs, ExprKey, ExprPtr, ExprRef, Field, RecRef, StrRef, Value, VecRef};
3use rustc_hash::{FxBuildHasher, FxHashMap};
4use serde::Serialize;
5use std::collections::hash_map::Entry;
6use std::hash::BuildHasher;
7use std::ops::Range;
8use unicase::Ascii;
9
10/// An arena-based allocator for interning strings.
11///
12/// Deduplicates strings by hash and returns lightweight [`StrRef`] handles for O(1) lookups.
13#[derive(Default, Serialize)]
14pub(crate) struct StringArena {
15    #[serde(skip_serializing)]
16    hasher: FxBuildHasher,
17
18    cache: FxHashMap<u64, StrRef>,
19    slots: Vec<String>,
20}
21
22impl StringArena {
23    /// Interns a string and returns its [`StrRef`]. Returns the existing reference if already interned.
24    pub fn alloc(&mut self, value: &str) -> StrRef {
25        match self.cache.entry(self.hasher.hash_one(value)) {
26            Entry::Occupied(entry) => *entry.get(),
27            Entry::Vacant(entry) => {
28                let key = StrRef(self.slots.len());
29                entry.insert(key);
30                self.slots.push(value.to_owned());
31
32                key
33            }
34        }
35    }
36
37    /// If that value has already been interned, it returns the string pointer in the arena.
38    pub fn str_ref(&self, value: &str) -> Option<StrRef> {
39        self.cache.get(&self.hasher.hash_one(value)).copied()
40    }
41
42    /// Interns a string using case-insensitive hashing.
43    ///
44    /// Two strings that differ only in ASCII case will resolve to the same [`StrRef`].
45    /// The original casing of the first insertion is preserved.
46    pub fn alloc_no_case(&mut self, value: &str) -> StrRef {
47        match self.cache.entry(self.hasher.hash_one(Ascii::new(value))) {
48            Entry::Occupied(entry) => *entry.get(),
49            Entry::Vacant(entry) => {
50                let key = StrRef(self.slots.len());
51                entry.insert(key);
52                self.slots.push(value.to_owned());
53
54                key
55            }
56        }
57    }
58
59    /// Retrieves the string associated with the given [`StrRef`].
60    pub fn get(&self, key: StrRef) -> &str {
61        &self.slots[key.0]
62    }
63}
64
65/// An expression node stored in the [`ExprArena`].
66///
67/// Combines the expression's metadata ([`Attrs`]) with its actual content ([`Value`]).
68#[derive(Debug, Clone, Copy, Serialize)]
69pub struct Expr {
70    /// Metadata including source position.
71    pub attrs: Attrs,
72    /// The kind and content of this expression.
73    pub value: Value,
74}
75
76/// An arena-based allocator for EventQL expressions.
77///
78/// The `ExprArena` provides a memory-efficient way to store and manage AST nodes
79/// by using a flat vector and returning lightweight [`ExprRef`] handles.
80#[derive(Default, Serialize)]
81pub(crate) struct ExprArena {
82    #[serde(skip_serializing)]
83    hasher: FxBuildHasher,
84    exprs: Vec<Expr>,
85    vecs: Vec<Vec<ExprRef>>,
86    recs: Vec<Vec<Field>>,
87}
88
89impl ExprArena {
90    /// Allocates a new expression in the arena.
91    ///
92    /// This method takes an expression's attributes and value, hashes the value
93    /// to create a stable [`ExprKey`], and stores it in the arena. It returns
94    /// an [`ExprRef`] which can be used to retrieve the expression later.
95    pub fn alloc(&mut self, attrs: Attrs, value: Value) -> ExprRef {
96        let key = ExprKey(self.hasher.hash_one(value));
97
98        let ptr = ExprPtr(self.exprs.len());
99        self.exprs.push(Expr { attrs, value });
100
101        ExprRef { key, ptr }
102    }
103
104    /// Retrieves a node from the arena using an [`ExprRef`].
105    ///
106    /// # Panics
107    ///
108    /// Panics if the [`ExprRef`] contains an invalid pointer that is out of bounds
109    /// of the arena's internal storage.
110    pub fn get(&self, node_ref: ExprRef) -> Expr {
111        self.exprs[node_ref.ptr.0]
112    }
113
114    /// Allocates a vector of expression references and returns a [`VecRef`] handle.
115    pub fn alloc_vec(&mut self, values: Vec<ExprRef>) -> VecRef {
116        let key = VecRef(self.vecs.len());
117        self.vecs.push(values);
118
119        key
120    }
121
122    /// Allocates a vector of record fields and returns a [`RecRef`] handle.
123    pub fn alloc_rec(&mut self, values: Vec<Field>) -> RecRef {
124        let key = RecRef(self.recs.len());
125        self.recs.push(values);
126
127        key
128    }
129
130    /// Returns the slice of expression references for the given [`VecRef`].
131    pub fn vec(&self, ptr: VecRef) -> &[ExprRef] {
132        &self.vecs[ptr.0]
133    }
134
135    /// Returns the expression reference at index `idx` within the given [`VecRef`].
136    pub fn vec_get(&self, ptr: VecRef, idx: usize) -> ExprRef {
137        self.vecs[ptr.0][idx]
138    }
139
140    /// Returns an iterator over valid indices for the given [`VecRef`].
141    pub fn vec_idxes(&self, ptr: VecRef) -> Range<usize> {
142        0..self.vec(ptr).len()
143    }
144
145    /// Returns the vector of fields for the given [`RecRef`].
146    pub fn rec(&self, ptr: RecRef) -> &[Field] {
147        self.recs[ptr.0].as_slice()
148    }
149
150    /// Returns the field at index `idx` within the given [`RecRef`].
151    pub fn rec_get(&self, ptr: RecRef, idx: usize) -> Field {
152        self.recs[ptr.0][idx]
153    }
154
155    /// Returns an iterator over valid indices for the given [`RecRef`].
156    pub fn rec_idxes(&self, ptr: RecRef) -> Range<usize> {
157        0..self.rec(ptr).len()
158    }
159}
160
161/// An arena-based allocator for type information.
162///
163/// Stores and deduplicates types, record definitions, and function argument lists.
164/// Supports freezing to mark a baseline and freeing types allocated after the baseline.
165#[derive(Default, Serialize)]
166pub(crate) struct TypeArena {
167    #[serde(skip_serializing)]
168    args_hasher: FxBuildHasher,
169
170    type_offset: usize,
171    rec_offset: usize,
172
173    dedup_types: FxHashMap<Type, TypeRef>,
174    dedup_args: FxHashMap<u64, ArgsRef>,
175    types: Vec<Type>,
176    pub(crate) records: Vec<FxHashMap<StrRef, Type>>,
177    pub(crate) args: Vec<Vec<Type>>,
178}
179
180impl TypeArena {
181    /// Marks the current allocation state as the baseline.
182    ///
183    /// Subsequent calls to [`free_space`](TypeArena::free_space) will deallocate
184    /// only types and records allocated after this point.
185    pub fn freeze(&mut self) {
186        self.rec_offset = self.records.len();
187        self.type_offset = self.types.len();
188    }
189
190    /// Frees types and records allocated after the last [`freeze`](TypeArena::freeze) call.
191    pub fn free_space(&mut self) {
192        for tpe in self.types.drain(self.type_offset..) {
193            self.dedup_types.remove(&tpe);
194        }
195
196        for _ in self.records.drain(self.rec_offset..) {}
197    }
198
199    /// Registers a type and returns a deduplicated [`TypeRef`]. Returns the existing reference if already registered.
200    pub fn register_type(&mut self, tpe: Type) -> TypeRef {
201        match self.dedup_types.entry(tpe) {
202            Entry::Occupied(entry) => *entry.get(),
203            Entry::Vacant(entry) => {
204                let key = TypeRef(self.types.len());
205                self.types.push(tpe);
206                entry.insert(key);
207
208                key
209            }
210        }
211    }
212
213    /// Allocates a fresh copy of a type. For records, this clones the record definition.
214    pub fn alloc_type(&mut self, tpe: Type) -> Type {
215        if let Type::Record(rec) = tpe {
216            let key = Record(self.records.len());
217            // TODO: technically, a deep-clone is needed here, where properties that point to
218            // records should also be allocated as well.
219            self.records.push(self.records[rec.0].clone());
220
221            return Type::Record(key);
222        }
223
224        tpe
225    }
226
227    /// Creates an array type containing elements of the given type.
228    pub fn alloc_array_of(&mut self, tpe: Type) -> Type {
229        Type::Array(self.register_type(tpe))
230    }
231
232    /// Allocates a new record type from a map of field names to types.
233    pub fn alloc_record(&mut self, record: FxHashMap<StrRef, Type>) -> Record {
234        let key = Record(self.records.len());
235        self.records.push(record);
236        key
237    }
238
239    /// Allocates a deduplicated list of function argument types and returns an [`ArgsRef`].
240    pub fn alloc_args(&mut self, args: &[Type]) -> ArgsRef {
241        let hash = self.args_hasher.hash_one(args);
242
243        match self.dedup_args.entry(hash) {
244            Entry::Occupied(entry) => *entry.get(),
245            Entry::Vacant(entry) => {
246                let key = ArgsRef(self.args.len());
247                entry.insert(key);
248                self.args.push(args.to_vec());
249
250                key
251            }
252        }
253    }
254
255    /// Retrieves the type for the given [`TypeRef`].
256    pub fn get_type(&self, key: TypeRef) -> Type {
257        self.types[key.0]
258    }
259
260    /// Returns the field map for the given record.
261    pub fn get_record(&self, key: Record) -> &FxHashMap<StrRef, Type> {
262        &self.records[key.0]
263    }
264
265    /// Returns the argument type slice for the given [`ArgsRef`].
266    pub fn get_args(&self, key: ArgsRef) -> &[Type] {
267        self.args[key.0].as_slice()
268    }
269
270    /// Returns an iterator over valid indices for the given [`ArgsRef`].
271    pub fn args_idxes(&self, key: ArgsRef) -> impl Iterator<Item = usize> + use<> {
272        0..self.get_args(key).len()
273    }
274
275    /// Returns the argument type at index `idx` for the given [`ArgsRef`].
276    pub fn args_get(&self, key: ArgsRef, idx: usize) -> Type {
277        self.get_args(key)[idx]
278    }
279
280    /// Returns the type of a field in the given record, or `None` if the field doesn't exist.
281    pub fn record_get(&self, record: Record, field: StrRef) -> Option<Type> {
282        self.records[record.0].get(&field).copied()
283    }
284
285    /// Checks whether two records have the exact same set of field names.
286    pub fn records_have_same_keys(&self, rec_a: Record, rec_b: Record) -> bool {
287        let rec_a = self.get_record(rec_a);
288        let rec_b = self.get_record(rec_b);
289
290        if rec_a.is_empty() && rec_b.is_empty() {
291            return true;
292        }
293
294        if rec_a.len() != rec_b.len() {
295            return false;
296        }
297
298        for bk in rec_b.keys() {
299            if !rec_a.contains_key(bk) {
300                return false;
301            }
302        }
303
304        true
305    }
306
307    /// Creates an empty record type.
308    pub fn instantiate_record(&mut self) -> Record {
309        self.alloc_record(FxHashMap::default())
310    }
311
312    /// Sets the type of a field in the given record, inserting or updating as needed.
313    pub fn record_set(&mut self, record: Record, field: StrRef, value: Type) {
314        self.records[record.0].insert(field, value);
315    }
316
317    /// Returns the number of fields in the given record.
318    pub fn record_len(&self, record: Record) -> usize {
319        self.records[record.0].len()
320    }
321}
322
323/// Top-level arena that holds all memory pools for expressions, strings, and types.
324#[derive(Default, Serialize)]
325pub struct Arena {
326    pub(crate) exprs: ExprArena,
327    pub(crate) strings: StringArena,
328    pub(crate) types: TypeArena,
329}
330
331impl Arena {
332    /// Freezes the type arena to mark the current state as baseline.
333    pub fn freeze(&mut self) {
334        self.types.freeze();
335    }
336
337    /// Frees types allocated after the last freeze, reclaiming memory for reuse.
338    pub fn free_space(&mut self) {
339        self.types.free_space();
340    }
341
342    /// Retrieves the interned string associated with the given [`StrRef`].
343    pub fn get_str(&self, key: StrRef) -> &str {
344        self.strings.get(key)
345    }
346
347    /// If that value has already been interned, it returns the string pointer in the arena.
348    pub fn str_ref(&self, key: &str) -> Option<StrRef> {
349        self.strings.str_ref(key)
350    }
351
352    /// Retrieves the expression node associated with the given [`ExprRef`].
353    pub fn get_expr(&self, key: ExprRef) -> Expr {
354        self.exprs.get(key)
355    }
356
357    /// Retrieves the type associated with the given [`TypeRef`].
358    pub fn get_type(&self, key: TypeRef) -> Type {
359        self.types.get_type(key)
360    }
361
362    /// Returns the slice of expression references for the given [`VecRef`].
363    pub fn get_vec(&self, key: VecRef) -> &[ExprRef] {
364        self.exprs.vec(key)
365    }
366
367    /// Returns the slice of record fields for the given [`RecRef`].
368    pub fn get_rec(&self, key: RecRef) -> &[Field] {
369        self.exprs.rec(key)
370    }
371
372    /// Returns the map of type record fields for the given type [`Record`]
373    pub fn get_type_rec(&self, key: Record) -> &FxHashMap<StrRef, Type> {
374        self.types.get_record(key)
375    }
376
377    /// Returns the function argument types for the given [`ArgsRef`].
378    pub fn get_args(&self, key: ArgsRef) -> &[Type] {
379        self.types.get_args(key)
380    }
381}