Skip to content

Commit

Permalink
Set num_locals more efficiently (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejrgilbert authored Oct 1, 2024
1 parent 3d71fa2 commit 61d5c79
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/ir/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ impl<'a> Module<'a> {
}
Payload::CodeSectionEntry(body) => {
let locals_reader = body.get_locals_reader()?;
let num_locals = locals_reader.get_count();
let locals = locals_reader.into_iter().collect::<Result<Vec<_>, _>>()?;
let locals: Vec<(u32, DataType)> = locals
.iter()
.map(|(idx, val_type)| (*idx, DataType::from(*val_type)))
.collect();
// TODO: can I just iter locals once?
let num_locals = locals.iter().fold(0, |acc, x| acc + x.0) as usize;

let instructions = body
.get_operators_reader()?
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions src/ir/module/module_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ impl<'a> LocalFunction<'a> {
pub(crate) fn add_local(
ty: DataType,
num_params: usize,
num_locals: &mut usize,
num_locals: &mut u32,
locals: &mut Vec<(u32, DataType)>,
) -> LocalID {
let index = num_params + *num_locals;
let index = num_params + *num_locals as usize;

let len = locals.len();
*num_locals += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ pub struct Body<'a> {
/// defined here then local indices 0 and 1 will refer to the parameters and
/// index 2 will refer to the local here.
pub locals: Vec<(u32, DataType)>,
pub num_locals: usize,
pub num_locals: u32,
// accessing operators by .0 is not very clear
pub instructions: Vec<Instruction<'a>>,
pub num_instructions: usize,
Expand Down

0 comments on commit 61d5c79

Please sign in to comment.