incremental_query/
storage.rs

1use bumpalo::Bump;
2
3use super::query_parameter::QueryParameter;
4
5/// Storage is what stores the inputs and outputs to queries.
6///
7/// Inside, it has a bump allocator which will grow like a memory leak...
8/// That's why there's [`Context::gc`](crate::Context::gc).
9///
10/// Note: the main thing that grows storage is new generations as that
11/// invalidates a lot of the cache. [serializing](crate::Context::serialize)
12/// and [deserializing](crate::Context::deserialize) do not store elements and
13/// that would be garbage collected otherwise (so a serialize --> deserialize
14/// cycle is effectively equivalent to a garbage collect). Thus, you might not
15/// need to care about this storage growing too much if you serialize the
16/// [`Context`](crate::Context) regularly.
17pub struct Storage {
18    data: Bump,
19}
20
21impl Storage {
22    pub fn new() -> Self {
23        Self { data: Bump::new() }
24    }
25
26    // hidden because of use in macro
27    #[doc(hidden)]
28    pub fn alloc<T: QueryParameter>(&self, v: T) -> &mut T {
29        self.data.alloc(v)
30    }
31
32    /// Number of allocated bytes in the storage
33    ///
34    /// This is much more precise than [`Context::size`](crate::Context::size)
35    pub fn size(&self) -> usize {
36        self.data.allocated_bytes()
37    }
38}
39
40impl Default for Storage {
41    fn default() -> Self {
42        Self::new()
43    }
44}