From 460363bff151cc60fcc83601848c96c928038abb Mon Sep 17 00:00:00 2001 From: Rerumu <25379555+Rerumu@users.noreply.github.com> Date: Fri, 23 Jun 2023 16:02:06 -0400 Subject: [PATCH] Reduce `Statement` memory footprint --- wasm-ast/src/factory.rs | 14 ++++++-------- wasm-ast/src/node.rs | 16 ++++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/wasm-ast/src/factory.rs b/wasm-ast/src/factory.rs index 6b3be54..15f6e51 100644 --- a/wasm-ast/src/factory.rs +++ b/wasm-ast/src/factory.rs @@ -57,7 +57,7 @@ impl From for LabelType { struct StatList { stack: Stack, code: Vec, - last: Option, + last: Option>, block_data: BlockData, has_reference: bool, @@ -199,7 +199,7 @@ impl StatList { fn set_terminator(&mut self, term: Terminator) { self.leak_all(); - self.last = Some(term); + self.last = Some(term.into()); } } @@ -310,15 +310,13 @@ impl<'a> Factory<'a> { BlockData::Forward { .. } | BlockData::Backward { .. } => Statement::Block(now.into()), BlockData::If { .. } => Statement::If(If { condition: self.target.stack.pop().into(), - on_true: now.into(), + on_true: Box::new(now.into()), on_false: None, }), BlockData::Else { .. } => { - if let Statement::If(v) = self.target.code.last_mut().unwrap() { - v.on_false = Some(now.into()); - } else { - unreachable!() - } + let Statement::If(last) = self.target.code.last_mut().unwrap() else { unreachable!() }; + + last.on_false = Some(Box::new(now.into())); return; } diff --git a/wasm-ast/src/node.rs b/wasm-ast/src/node.rs index 7c2bc8e..7e17e79 100644 --- a/wasm-ast/src/node.rs +++ b/wasm-ast/src/node.rs @@ -887,7 +887,7 @@ pub enum Terminator { pub struct Block { pub(crate) label_type: Option, pub(crate) code: Vec, - pub(crate) last: Option, + pub(crate) last: Option>, } impl Block { @@ -902,8 +902,8 @@ impl Block { } #[must_use] - pub const fn last(&self) -> Option<&Terminator> { - self.last.as_ref() + pub fn last(&self) -> Option<&Terminator> { + self.last.as_deref() } } @@ -926,8 +926,8 @@ impl BrIf { pub struct If { pub(crate) condition: Box, - pub(crate) on_true: Block, - pub(crate) on_false: Option, + pub(crate) on_true: Box, + pub(crate) on_false: Option>, } impl If { @@ -937,13 +937,13 @@ impl If { } #[must_use] - pub const fn on_true(&self) -> &Block { + pub fn on_true(&self) -> &Block { &self.on_true } #[must_use] - pub const fn on_false(&self) -> Option<&Block> { - self.on_false.as_ref() + pub fn on_false(&self) -> Option<&Block> { + self.on_false.as_deref() } }