From c0b356b025ef93eb3c1378c203858c3a7486f398 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 4 May 2026 19:25:22 +0200 Subject: chore: simplify parser & reduce allocations Signed-off-by: Henry --- crates/parser/src/macros.rs | 87 ++++----------------------------------------- 1 file changed, 6 insertions(+), 81 deletions(-) (limited to 'crates/parser/src/macros.rs') diff --git a/crates/parser/src/macros.rs b/crates/parser/src/macros.rs index dded6d8..b21c1e8 100644 --- a/crates/parser/src/macros.rs +++ b/crates/parser/src/macros.rs @@ -1,94 +1,19 @@ pub(crate) mod visit { macro_rules! validate_then_visit { - ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => { - $(validate_then_visit!(@@$proposal $op $({ $($arg: $argty),* })? => $visit ($($ann)*));)* - }; - - // These special-case arms exist so we only clone wasmparser's non-Copy payloads - (@@mvp BrTable { $arg:ident: $argty:ty } => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self, $arg: $argty) -> Self::Output { - self.0.$visit($arg.clone()); - let validation = self.0.validator.visitor(self.0.position).$visit($arg); - if let Err(e) = validation { - cold_path(); - self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); - } - } - }; - - (@@reference_types TypedSelectMulti { $arg:ident: $argty:ty } => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self, $arg: $argty) -> Self::Output { - self.0.$visit($arg.clone()); - let validation = self.0.validator.visitor(self.0.position).$visit($arg); - if let Err(e) = validation { - cold_path(); - self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); - } - } - }; - - (@@exceptions TryTable { $arg:ident: $argty:ty } => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self, $arg: $argty) -> Self::Output { - self.0.$visit($arg.clone()); - let validation = self.0.validator.visitor(self.0.position).$visit($arg); - if let Err(e) = validation { - cold_path(); - self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); - } - } - }; - - (@@stack_switching Resume { cont_type_index: $cont:ty, resume_table: $table:ty } => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self, cont_type_index: $cont, resume_table: $table) -> Self::Output { - self.0.$visit(cont_type_index, resume_table.clone()); - let validation = self.0.validator.visitor(self.0.position).$visit(cont_type_index, resume_table); - if let Err(e) = validation { - cold_path(); - self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); - } - } - }; - - (@@stack_switching ResumeThrow { cont_type_index: $cont:ty, tag_index: $tag:ty, resume_table: $table:ty } => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self, cont_type_index: $cont, tag_index: $tag, resume_table: $table) -> Self::Output { - self.0.$visit(cont_type_index, tag_index, resume_table.clone()); - let validation = self.0.validator.visitor(self.0.position).$visit(cont_type_index, tag_index, resume_table); - if let Err(e) = validation { - cold_path(); - self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); - } - } - }; - - (@@stack_switching ResumeThrowRef { cont_type_index: $cont:ty, resume_table: $table:ty } => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self, cont_type_index: $cont, resume_table: $table) -> Self::Output { - self.0.$visit(cont_type_index, resume_table.clone()); - let validation = self.0.validator.visitor(self.0.position).$visit(cont_type_index, resume_table); - if let Err(e) = validation { - cold_path(); - self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); - } - } - }; - - (@@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*)) => { + ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {$( fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output { - self.0.$visit($($($arg),*)?); + self.0.$visit($($($arg.clone()),*)?); let validation = self.0.validator.visitor(self.0.position).$visit($($($arg),*)?); if let Err(e) = validation { cold_path(); self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); } } - }; + )*}; } macro_rules! validate_then_visit_simd { - ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => { - $(validate_then_visit_simd!(@@$proposal $op $({ $($arg: $argty),* })? => $visit ($($ann)*));)* - }; - - (@@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*)) => { + ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {$( fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output { self.0.$visit($($($arg),*)?); let validation = self.0.validator.simd_visitor(self.0.position).$visit($($($arg),*)?); @@ -97,7 +22,7 @@ pub(crate) mod visit { self.0.record_error(crate::ParseError::ParseError { message: e.to_string(), offset: self.0.position }); } } - }; + )*}; } macro_rules! define_operand { @@ -166,7 +91,7 @@ pub(crate) mod visit { (@@tail_call $($rest:tt)* ) => {}; (@@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*)) => { - fn $visit(&mut self $($(,_: $argty)*)?) { + fn $visit(&mut self $($(,_: $argty)*)?) -> Self::Output { self.unsupported(stringify!($visit)) } }; -- cgit v1.3.1