Skip to content

Commit

Permalink
feat(decoder): move cache to heap (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jul 11, 2023
1 parent 3c14050 commit 4de0c87
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ pub struct Decoder {
factories: Vec<InstructionFactory>,
mop: bool,
version: u32,
// use a cache of instructions to avoid decoding the same instruction twice, pc is the key and the instruction is the value
instructions_cache: [(u64, u64); INSTRUCTION_CACHE_SIZE],
// Use a cache of instructions to avoid decoding the same instruction
// twice, pc is the key and the instruction is the value.
//
// Use Vector so that the data is on the heap. Otherwise, if there is
// a vm call chain, it will quickly consume Rust's 2M stack space.
instructions_cache: Vec<(u64, u64)>,
}

impl Decoder {
Expand All @@ -26,7 +30,7 @@ impl Decoder {
factories: vec![],
mop,
version,
instructions_cache: [(RISCV_MAX_MEMORY as u64, 0); INSTRUCTION_CACHE_SIZE],
instructions_cache: vec![(RISCV_MAX_MEMORY as u64, 0); INSTRUCTION_CACHE_SIZE],
}
}

Expand Down Expand Up @@ -857,7 +861,7 @@ impl Decoder {
}

pub fn reset_instructions_cache(&mut self) {
self.instructions_cache = [(RISCV_MAX_MEMORY as u64, 0); INSTRUCTION_CACHE_SIZE];
self.instructions_cache = vec![(RISCV_MAX_MEMORY as u64, 0); INSTRUCTION_CACHE_SIZE];
}
}

Expand Down

0 comments on commit 4de0c87

Please sign in to comment.