@samitouri / QOS-React-2 / commits / c16124f255

[rust] Helpers for arena allocating box/vec

Joe Savona committed Jul 13, 2023 at 11:26 UTC c16124f255163482a787c13d228f8c76e4a7c836
4 files changed +53 -51
compiler/forget/crates/build-hir/src/build.rs
+12 -15
@@ -46,7 +46,7 @@ pub fn build<'a>(
46 }
47 }
48
49 - let mut params = Vec::with_capacity_in(fun.params.len(), &env.allocator);
49 + let mut params = env.vec_with_capacity(fun.params.len());
50 for param in fun.params {
51 match param {
52 Pattern::Identifier(param) => {
@@ -78,18 +78,15 @@ pub fn build<'a>(
78 );
79
80 let body = builder.build()?;
81 - Ok(Box::new_in(
82 - Function {
83 - id: fun
84 - .id
85 - .map(|id| String::from_str_in(&id.name, &env.allocator)),
86 - body,
87 - params,
88 - is_async: fun.is_async,
89 - is_generator: fun.is_generator,
90 - },
91 - &env.allocator,
92 - ))
81 + Ok(env.box_new(Function {
82 + id: fun
83 + .id
84 + .map(|id| String::from_str_in(&id.name, &env.allocator)),
85 + body,
86 + params,
87 + is_async: fun.is_async,
88 + is_generator: fun.is_generator,
89 + }))
90 }
91
92 fn lower_block_statement<'a>(
@@ -367,7 +364,7 @@ fn lower_expression<'a>(
364 value: lower_primitive(env, builder, *expr),
365 }),
366 Expression::ArrayExpression(expr) => {
370 - let mut elements = Vec::with_capacity_in(expr.elements.len(), &env.allocator);
367 + let mut elements = env.vec_with_capacity(expr.elements.len());
368 for expr in expr.elements {
369 let element = match expr {
370 Some(estree::ExpressionOrSpread::SpreadElement(expr)) => {
@@ -430,7 +427,7 @@ fn lower_expression<'a>(
427 let fun = build(env, function)?;
428 InstructionValue::Function(hir::FunctionExpression {
429 // TODO: collect dependencies!
433 - dependencies: Vec::new_in(&env.allocator),
430 + dependencies: env.vec_new(),
431 lowered_function: fun,
432 })
433 }
compiler/forget/crates/build-hir/src/builder.rs
+25 -31
@@ -94,16 +94,16 @@ impl<'a> Builder<'a> {
94 let current = WipBlock {
95 id: entry,
96 kind: BlockKind::Block,
97 - instructions: Vec::new_in(&environment.allocator),
97 + instructions: environment.vec_new(),
98 };
99 Self {
100 environment,
101 completed: Default::default(),
102 - instructions: Vec::new_in(&environment.allocator),
102 + instructions: environment.vec_new(),
103 entry,
104 wip: current,
105 id_gen: InstructionIdGenerator::new(),
106 - scopes: Vec::new_in(&environment.allocator),
106 + scopes: environment.vec_new(),
107 }
108 }
109
@@ -141,7 +141,7 @@ impl<'a> Builder<'a> {
141 let next_wip = WipBlock {
142 id: self.environment.next_block_id(),
143 kind: next_kind,
144 - instructions: Vec::new_in(&self.environment.allocator),
144 + instructions: self.environment.vec_new(),
145 };
146 self.terminate_with_fallthrough(terminal, next_wip)
147 }
@@ -154,20 +154,17 @@ impl<'a> Builder<'a> {
154 let prev_wip = std::mem::replace(&mut self.wip, fallthrough);
155 self.completed.insert(
156 prev_wip.id,
157 - Box::new_in(
158 - BasicBlock {
159 - id: prev_wip.id,
160 - kind: prev_wip.kind,
161 - instructions: prev_wip.instructions,
162 - terminal: Terminal {
163 - id: self.id_gen.next(),
164 - value: terminal,
165 - },
166 - predecessors: Default::default(),
167 - phis: Vec::new_in(&self.environment.allocator),
157 + self.environment.box_new(BasicBlock {
158 + id: prev_wip.id,
159 + kind: prev_wip.kind,
160 + instructions: prev_wip.instructions,
161 + terminal: Terminal {
162 + id: self.id_gen.next(),
163 + value: terminal,
164 },
169 - &self.environment.allocator,
170 - ),
165 + predecessors: Default::default(),
166 + phis: self.environment.vec_new(),
167 + }),
168 );
169 }
170
@@ -175,7 +172,7 @@ impl<'a> Builder<'a> {
172 WipBlock {
173 id: self.environment.next_block_id(),
174 kind,
178 - instructions: Vec::new_in(&self.environment.allocator),
175 + instructions: self.environment.vec_new(),
176 }
177 }
178
@@ -210,20 +207,17 @@ impl<'a> Builder<'a> {
207 let completed = std::mem::replace(&mut self.wip, current);
208 self.completed.insert(
209 completed.id,
213 - Box::new_in(
214 - BasicBlock {
215 - id: completed.id,
216 - kind: completed.kind,
217 - instructions: completed.instructions,
218 - terminal: Terminal {
219 - id: self.id_gen.next(),
220 - value: terminal,
221 - },
222 - predecessors: Default::default(),
223 - phis: Vec::new_in(&self.environment.allocator),
210 + self.environment.box_new(BasicBlock {
211 + id: completed.id,
212 + kind: completed.kind,
213 + instructions: completed.instructions,
214 + terminal: Terminal {
215 + id: self.id_gen.next(),
216 + value: terminal,
217 },
225 - &self.environment.allocator,
226 - ),
218 + predecessors: Default::default(),
219 + phis: self.environment.vec_new(),
220 + }),
221 );
222 result
223 }
compiler/forget/crates/hir-ssa/src/enter.rs
+3 -4
@@ -68,8 +68,8 @@ impl<'a> BlockState<'a> {
68 fn new(env: &Environment<'a>) -> Self {
69 Self {
70 defs: Default::default(),
71 - incomplete_phis: Vec::new_in(&env.allocator),
72 - phis: Vec::new_in(&env.allocator),
71 + incomplete_phis: env.vec_new(),
72 + phis: env.vec_new(),
73 }
74 }
75 }
@@ -211,8 +211,7 @@ impl<'a, 'e, 'f> Builder<'a, 'e, 'f> {
211
212 fn fix_incomplete_phis(&mut self, block_id: BlockId) -> () {
213 let state = self.states.get_mut(&block_id).unwrap();
214 - let incomplete_phis =
215 - std::mem::replace(&mut state.incomplete_phis, Vec::new_in(&self.env.allocator));
214 + let incomplete_phis = std::mem::replace(&mut state.incomplete_phis, self.env.vec_new());
215 for phi in incomplete_phis {
216 self.add_phi(block_id, &phi.old_id, phi.new_id);
217 }
compiler/forget/crates/hir/src/environment.rs
+13 -1
@@ -4,7 +4,7 @@ use std::{
4 rc::Rc,
5 };
6
7 -use bumpalo::Bump;
7 +use bumpalo::{boxed::Box, collections::Vec, Bump};
8 use estree::BindingId;
9
10 use crate::{
@@ -57,6 +57,18 @@ impl<'a> Environment<'a> {
57 self.allocator.alloc(value)
58 }
59
60 + pub fn box_new<T>(&self, value: T) -> Box<'a, T> {
61 + Box::new_in(value, &self.allocator)
62 + }
63 +
64 + pub fn vec_new<T>(&self) -> Vec<'a, T> {
65 + Vec::new_in(&self.allocator)
66 + }
67 +
68 + pub fn vec_with_capacity<T>(&self, capacity: usize) -> Vec<'a, T> {
69 + Vec::with_capacity_in(capacity, &self.allocator)
70 + }
71 +
72 /// Get the next available block id
73 pub fn next_block_id(&self) -> BlockId {
74 let id = self.next_block_id.get();