dprint_core/formatting/
arena.rs1use std::sync::Arc;
2use std::sync::Mutex;
3
4use super::Condition;
5use super::ConditionResolver;
6use super::ConditionResolverContext;
7use super::PrintNodeCell;
8use super::SavePoint;
9use super::StringContainer;
10use super::WriteItem;
11use super::collections::GraphNode;
12use super::collections::LookAheadSavePointsNode;
13use super::collections::NodeStackNode;
14use super::thread_state;
15use super::thread_state::Counts;
16
17pub struct Arena {
48 bump: bumpalo::Bump,
49 outer_counts: Counts,
52 pool: Option<ArenaPool>,
54}
55
56impl Default for Arena {
57 fn default() -> Self {
58 Self::new()
59 }
60}
61
62impl Drop for Arena {
63 fn drop(&mut self) {
64 if let Some(pool) = self.pool.take() {
65 let mut bump = std::mem::take(&mut self.bump);
66 bump.reset();
67 pool.lock_bumps().push(bump);
68 }
69 thread_state::set_counts(std::mem::take(&mut self.outer_counts));
70 }
71}
72
73impl Arena {
74 pub fn new() -> Self {
79 Self::new_internal(bumpalo::Bump::new(), None)
80 }
81
82 fn new_internal(bump: bumpalo::Bump, pool: Option<ArenaPool>) -> Self {
83 Self {
84 bump,
85 outer_counts: thread_state::take_counts(),
86 pool,
87 }
88 }
89
90 pub fn bump(&self) -> &bumpalo::Bump {
95 &self.bump
96 }
97
98 pub fn alloc_resolver<'a>(&'a self, resolver: impl Fn(&mut ConditionResolverContext) -> Option<bool> + Copy + 'a) -> ConditionResolver<'a> {
114 self.bump.alloc(resolver)
115 }
116
117 pub(super) fn alloc_condition<'a>(&'a self, condition: Condition<'a>) -> &'a Condition<'a> {
118 self.bump.alloc(condition)
119 }
120
121 pub(super) fn alloc_str<'a>(&'a self, text: &str) -> &'a StringContainer<'a> {
123 let text = self.bump.alloc_str(text);
124 self.alloc_string_container(text)
125 }
126
127 pub(super) fn alloc_string_container<'a>(&'a self, text: &'a str) -> &'a StringContainer<'a> {
128 self.bump.alloc(StringContainer::new(text))
129 }
130
131 pub(super) fn alloc_print_node_cell<'a>(&'a self, cell: PrintNodeCell<'a>) -> &'a PrintNodeCell<'a> {
132 self.bump.alloc(cell)
133 }
134
135 pub(super) fn alloc_write_item_graph_node<'a>(&'a self, node: GraphNode<'a, WriteItem<'a>>) -> &'a GraphNode<'a, WriteItem<'a>> {
136 self.bump.alloc(node)
137 }
138
139 pub(super) fn alloc_node_stack_node<'a>(&'a self, node: NodeStackNode<'a>) -> &'a NodeStackNode<'a> {
140 self.bump.alloc(node)
141 }
142
143 pub(super) fn alloc_save_point<'a>(&'a self, save_point: SavePoint<'a>) -> &'a SavePoint<'a> {
144 self.bump.alloc(save_point)
145 }
146
147 pub(super) fn alloc_look_ahead_save_points_node<'a>(&'a self, node: LookAheadSavePointsNode<'a>) -> &'a LookAheadSavePointsNode<'a> {
148 self.bump.alloc(node)
149 }
150}
151
152#[derive(Clone, Default)]
170pub struct ArenaPool {
171 bumps: Arc<Mutex<Vec<bumpalo::Bump>>>,
172}
173
174impl ArenaPool {
175 pub fn new() -> Self {
176 Self::default()
177 }
178
179 pub fn create_arena(&self) -> Arena {
182 let bump = self.lock_bumps().pop().unwrap_or_default();
183 Arena::new_internal(bump, Some(self.clone()))
184 }
185
186 pub fn clear(&self) {
190 self.lock_bumps().clear();
191 }
192
193 fn lock_bumps(&self) -> std::sync::MutexGuard<'_, Vec<bumpalo::Bump>> {
194 self.bumps.lock().unwrap_or_else(|err| err.into_inner())
196 }
197}
198
199#[cfg(test)]
200mod test {
201 use super::*;
202
203 #[test]
204 fn should_reuse_the_memory_of_arenas_from_the_pool() {
205 let pool = ArenaPool::new();
206 {
207 let arena = pool.create_arena();
208 arena.bump().alloc_slice_fill_copy(10_000, 0u8);
209 }
210 assert_eq!(pool.lock_bumps().len(), 1);
211 {
212 let arena = pool.create_arena();
213 assert_eq!(pool.lock_bumps().len(), 0);
214 assert!(arena.bump().allocated_bytes() >= 10_000);
215 }
216 assert_eq!(pool.lock_bumps().len(), 1);
217 }
218
219 #[test]
220 fn should_keep_the_memory_of_arenas_alive_at_the_same_time() {
221 let pool = ArenaPool::new();
222 {
223 let _outer = pool.create_arena();
224 let _inner = pool.create_arena();
225 }
226 assert_eq!(pool.lock_bumps().len(), 2);
227 }
228
229 #[test]
230 fn should_free_the_memory_when_cleared() {
231 let pool = ArenaPool::new();
232 drop(pool.create_arena());
233 assert_eq!(pool.lock_bumps().len(), 1);
234 pool.clear();
235 assert_eq!(pool.lock_bumps().len(), 0);
236 }
237
238 #[test]
239 fn should_not_keep_the_memory_of_arenas_without_a_pool() {
240 let pool = ArenaPool::new();
241 drop(Arena::new());
242 assert_eq!(pool.lock_bumps().len(), 0);
243 }
244
245 #[test]
246 fn should_be_able_to_share_the_pool_between_threads() {
247 fn assert_send_sync<T: Send + Sync>() {}
248 assert_send_sync::<ArenaPool>();
249 }
250}