Skip to content

Commit

Permalink
feat!: apply new side table
Browse files Browse the repository at this point in the history
with a bug on restoring parser
  • Loading branch information
zhouwfang committed Jan 15, 2025
1 parent 9a0df02 commit 38ad38d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
23 changes: 7 additions & 16 deletions crates/interpreter/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ impl<'m> Module<'m> {
}

pub(crate) fn func_type(&self, x: FuncIdx) -> FuncType<'m> {
let mut parser = self.section(SectionId::Function).unwrap();
for i in 0 .. parser.parse_vec().into_ok() {
let y = parser.parse_typeidx().into_ok();
if i == x as usize {
return self.types[y as usize];
}
}
unreachable!()
self.types[self.side_table[x as usize].type_idx]
}

pub(crate) fn table_type(&self, x: TableIdx) -> TableType {
Expand Down Expand Up @@ -183,17 +176,15 @@ impl<'m> Module<'m> {
unreachable!()
}

// TODO(dev/fast-interp): Improve the performance of such accessor functions from O(n) to O(1).
pub(crate) fn func(&self, x: FuncIdx) -> (Parser<'m>, &'m [BranchTableEntry]) {
let mut parser = self.section(SectionId::Code).unwrap();
for i in 0 .. parser.parse_vec().into_ok() {
let size = parser.parse_u32().into_ok() as usize;
let parser = parser.split_at(size).into_ok();
if i == x as usize {
return (parser, &self.side_table[i].branch_table);
}
let saved = parser.save();
let entry = &self.side_table[x as usize];
let range = &entry.parser_range;
unsafe {
parser.restore(&saved[range.start .. range.end]);
}
unreachable!()
(parser, &entry.branch_table)
}

pub(crate) fn data(&self, x: DataIdx) -> Parser<'m> {
Expand Down
2 changes: 0 additions & 2 deletions crates/interpreter/src/side_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ impl<'m> Metadata<'m> {

#[derive(Default, Debug)]
pub struct MetadataEntry {
#[allow(dead_code)]
pub type_idx: usize,
#[allow(dead_code)]
pub parser_range: Range<usize>,
pub branch_table: Vec<BranchTableEntry>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl<'m> Context<'m> {
}
let mut side_tables = vec![];
if let Some(mut parser) = self.check_section(parser, SectionId::Code)? {
check(self.funcs.len() == imported_funcs + parser.parse_vec()?)?;
let saved = parser.save();
check(self.funcs.len() == imported_funcs + parser.parse_vec()?)?;
for x in imported_funcs .. self.funcs.len() {
let size = parser.parse_u32()? as usize;
let mut parser = parser.split_at(size)?;
Expand Down

0 comments on commit 38ad38d

Please sign in to comment.