Skip to content

Commit 5c16242

Browse files
committed
implement direct-threading encoding in translator
1 parent b7731c8 commit 5c16242

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

crates/wasmi/src/engine/translator/func/encoder.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{Reset, ReusableAllocations};
22
use crate::{
33
core::FuelCostsProvider,
44
engine::{
5+
executor::op_code_to_handler,
56
translator::{
67
comparator::UpdateBranchOffset,
78
func::{
@@ -749,12 +750,24 @@ impl FuelCostsSelector for FuelUsed {
749750

750751
/// Encodes an [`ir::OpCode`] to a generic [`ir::Encoder`].
751752
fn encode_op_code<E: ir::Encoder>(encoder: &mut E, code: ir::OpCode) -> Result<E::Pos, E::Error> {
752-
// Note: this implements encoding for indirect threading.
753-
//
754-
// For direct threading we need to know ahead of time about the
755-
// function pointers of all operator execution handlers which
756-
// are defined in the Wasmi executor and available to the translator.
757-
u16::from(code).encode(encoder)
753+
match cfg!(feature = "compact") {
754+
true => {
755+
// Note: encoding for indirect-threading
756+
//
757+
// The op-codes are not resolved during translation time and must
758+
// be resolved during execution time. This decreases memory footprint
759+
// of the encoded IR at the cost of execution performance.
760+
u16::from(code).encode(encoder)
761+
}
762+
false => {
763+
// Note: encoding for direct-threading
764+
//
765+
// The op-codes are resolved during translation time (now) to their
766+
// underlying function pointers. This increases memory footprint
767+
// of the encoded IR but improves execution performance.
768+
(op_code_to_handler(code) as usize).encode(encoder)
769+
}
770+
}
758771
}
759772

760773
/// Creates an initialized [`BranchOffset`] from `src` to `dst`.

0 commit comments

Comments
 (0)