velox_engine/arena/
scratch.rs1use core::cell::Cell;
4use core::fmt;
5use core::mem::{align_of, size_of};
6
7use super::StrRef;
8
9pub const SCRATCH_CAPACITY: usize = 4992;
11
12pub struct ScratchArena<const CAP: usize = SCRATCH_CAPACITY> {
14 inner: bumpalo::Bump,
15 used: Cell<usize>,
16 peak_used: Cell<usize>,
17 truncation_count: Cell<u32>,
18}
19
20impl<const CAP: usize> Default for ScratchArena<CAP> {
21 fn default() -> Self {
22 Self::new()
23 }
24}
25
26impl<const CAP: usize> ScratchArena<CAP> {
27 #[inline]
29 pub fn new() -> Self {
30 Self {
31 inner: bumpalo::Bump::with_capacity(CAP),
32 used: Cell::new(0),
33 peak_used: Cell::new(0),
34 truncation_count: Cell::new(0),
35 }
36 }
37
38 #[inline(always)]
39 fn set_used_std(&self, next: usize) {
40 self.used.set(next);
41 if next > self.peak_used.get() {
42 self.peak_used.set(next);
43 }
44 }
45
46 #[inline]
48 pub fn try_alloc<'a, T>(&'a self, val: T) -> Option<&'a mut T> {
49 let align = align_of::<T>().max(1);
50 let size = size_of::<T>();
51 let cur = (self.used.get() + align - 1) & !(align - 1);
52 self.set_used_std(cur + size);
53 Some(self.inner.alloc(val))
54 }
55
56 #[inline(always)]
58 pub fn alloc<'a, T>(&'a self, val: T) -> &'a mut T {
59 self.try_alloc(val).expect("ScratchArena overflow")
60 }
61
62 #[inline]
64 pub fn try_alloc_temp<'a, T>(&'a self, val: T) -> Option<&'a mut T> {
65 self.try_alloc(val)
66 }
67
68 #[inline(always)]
70 pub fn alloc_temp<'a, T>(&'a self, val: T) -> &'a mut T {
71 self.try_alloc_temp(val)
72 .expect("ScratchArena temp overflow")
73 }
74
75 #[inline(always)]
80 pub(crate) unsafe fn reset_temp(&self) {
81 }
82
83 #[inline(always)]
85 pub(crate) fn save_temp_mark(&self) -> usize {
86 CAP
87 }
88
89 #[inline(always)]
91 pub(crate) fn restore_temp_mark(&mut self, mark: usize) {
92 let _ = mark;
93 }
94
95 #[inline(always)]
97 pub(crate) unsafe fn rollback_speculative_marks(&self, mark: usize, temp_mark: usize) {
98 let _ = temp_mark;
99 if mark <= self.used.get() {
100 self.used.set(mark);
101 }
102 }
103
104 #[inline(always)]
106 pub(crate) fn contains_temp_ptr(&self, ptr: *const u8) -> bool {
107 if ptr.is_null() {
108 return false;
109 }
110 self.used.get() > 0
111 }
112
113 #[inline(always)]
115 pub fn alloc_bytes<'a>(&'a self, len: usize) -> &'a mut [u8] {
116 if len == 0 {
117 return &mut [];
118 }
119 self.set_used_std(self.used.get() + len);
120 self.inner.alloc_slice_fill_copy(len, 0u8)
121 }
122
123 pub fn alloc_iter<'a, T, I>(&'a self, iter: I) -> &'a mut [T]
125 where
126 I: IntoIterator<Item = T>,
127 {
128 let mut iter = iter.into_iter();
129 let (lower, upper) = iter.size_hint();
130
131 let size = size_of::<T>();
132 if Some(lower) == upper {
133 let count = lower;
134 if count == 0 || size == 0 {
135 return &mut [];
136 }
137 self.set_used_std(self.used.get() + count.saturating_mul(size));
138 return self.inner.alloc_slice_fill_with(count, |_| {
139 iter.next().expect("Exact size_hint mismatch")
140 });
141 }
142 let mut vec = bumpalo::collections::Vec::with_capacity_in(lower, &self.inner);
143 for item in iter {
144 vec.push(item);
145 }
146 self.set_used_std(self.used.get() + vec.len().saturating_mul(size));
147 vec.into_bump_slice_mut()
148 }
149
150 #[inline]
152 pub fn alloc_str<'a>(&'a self, s: &str) -> StrRef<'a> {
153 if s.is_empty() {
154 return StrRef::Static("");
155 }
156 let copied = self.inner.alloc_str(s);
157 self.set_used_std(self.used.get() + copied.len());
158 StrRef::Scratch(copied)
159 }
160
161 pub fn alloc_fmt<'a>(&'a self, args: fmt::Arguments<'_>) -> StrRef<'a> {
163 if let Some(static_str) = args.as_str() {
164 return StrRef::Static(static_str);
165 }
166 let mut s = bumpalo::collections::String::new_in(&self.inner);
167 let _ = fmt::write(&mut s, args);
168 self.set_used_std(self.used.get() + s.len());
169 StrRef::Scratch(s.into_bump_str())
170 }
171
172 #[inline(always)]
174 pub fn rewind(&mut self) {
175 self.used.set(0);
176 self.peak_used.set(0);
177 self.truncation_count.set(0);
178 self.inner.reset();
179 }
180
181 #[inline(always)]
183 pub fn save_mark(&self) -> usize {
184 self.used_bytes()
185 }
186
187 #[inline(always)]
189 pub fn restore_mark(&mut self, mark: usize) {
190 if mark <= self.used.get() {
191 self.used.set(mark);
192 }
193 }
194
195 #[inline(always)]
197 pub fn used_bytes(&self) -> usize {
198 self.used.get()
199 }
200
201 #[inline(always)]
203 pub fn peak_used_bytes(&self) -> usize {
204 self.peak_used.get()
205 }
206
207 #[inline(always)]
209 pub fn truncation_count(&self) -> u32 {
210 self.truncation_count.get()
211 }
212
213 #[inline(always)]
215 pub(crate) fn take_truncation_count(&self) -> u32 {
216 self.truncation_count.replace(0)
217 }
218
219 #[inline(always)]
221 pub(crate) fn reset_peak_to_current(&self) {
222 self.peak_used.set(self.used.get());
223 }
224
225 #[inline(always)]
227 pub(crate) fn real_allocated_bytes(&self) -> usize {
228 self.inner.allocated_bytes()
229 }
230
231 #[inline(always)]
233 pub fn can_alloc_slice<T>(&self, count: usize) -> bool {
234 let _ = count;
235 true
236 }
237
238 #[inline(always)]
240 pub fn can_alloc_temp_slice<T>(&self, count: usize) -> bool {
241 let _ = count;
242 true
243 }
244
245 pub fn alloc_temp_iter_exact<'a, T, I>(&'a self, count: usize, iter: I) -> &'a mut [T]
247 where
248 I: IntoIterator<Item = T>,
249 {
250 if count == 0 || size_of::<T>() == 0 {
251 return &mut [];
252 }
253 self.alloc_iter(iter.into_iter().take(count))
254 }
255}