Optimize table.init instruction and instantiation (#2847)
* Optimize `table.init` instruction and instantiation This commit optimizes table initialization as part of instance instantiation and also applies the same optimization to the `table.init` instruction. One part of this commit is to remove some preexisting duplication between instance instantiation and the `table.init` instruction itself, after this the actual implementation of `table.init` is optimized to effectively have fewer bounds checks in fewer places and have a much tighter loop for instantiation. A big fallout from this change is that memory/table initializer offsets are now stored as `u32` instead of `usize` to remove a few casts in a few places. This ended up requiring moving some overflow checks that happened in parsing to later in code itself because otherwise the wrong spec test errors are emitted during testing. I've tried to trace where these can possibly overflow but I think that I managed to get everything. In a local synthetic test where an empty module with a single 80,000 element initializer this improves total instantiation time by 4x (562us => 141us) * Review comments
This commit is contained in:
@@ -377,7 +377,7 @@ pub fn parse_element_section<'data>(
|
||||
} => {
|
||||
let mut init_expr_reader = init_expr.get_binary_reader();
|
||||
let (base, offset) = match init_expr_reader.read_operator()? {
|
||||
Operator::I32Const { value } => (None, value as u32 as usize),
|
||||
Operator::I32Const { value } => (None, value as u32),
|
||||
Operator::GlobalGet { global_index } => {
|
||||
(Some(GlobalIndex::from_u32(global_index)), 0)
|
||||
}
|
||||
@@ -388,12 +388,6 @@ pub fn parse_element_section<'data>(
|
||||
));
|
||||
}
|
||||
};
|
||||
// Check for offset + len overflow
|
||||
if offset.checked_add(segments.len()).is_none() {
|
||||
return Err(wasm_unsupported!(
|
||||
"element segment offset and length overflows"
|
||||
));
|
||||
}
|
||||
environ.declare_table_elements(
|
||||
TableIndex::from_u32(table_index),
|
||||
base,
|
||||
@@ -429,7 +423,7 @@ pub fn parse_data_section<'data>(
|
||||
} => {
|
||||
let mut init_expr_reader = init_expr.get_binary_reader();
|
||||
let (base, offset) = match init_expr_reader.read_operator()? {
|
||||
Operator::I32Const { value } => (None, value as u32 as usize),
|
||||
Operator::I32Const { value } => (None, value as u32),
|
||||
Operator::GlobalGet { global_index } => {
|
||||
(Some(GlobalIndex::from_u32(global_index)), 0)
|
||||
}
|
||||
@@ -440,12 +434,6 @@ pub fn parse_data_section<'data>(
|
||||
))
|
||||
}
|
||||
};
|
||||
// Check for offset + len overflow
|
||||
if offset.checked_add(data.len()).is_none() {
|
||||
return Err(wasm_unsupported!(
|
||||
"data segment offset and length overflows"
|
||||
));
|
||||
}
|
||||
environ.declare_data_initialization(
|
||||
MemoryIndex::from_u32(memory_index),
|
||||
base,
|
||||
|
||||
Reference in New Issue
Block a user