Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ impl Decoder {
let instruction_cache_key = {
// according to RISC-V instruction encoding, the lowest bit in PC will always be zero
let pc = pc >> 1;
// Here we try to balance between local code and remote code. At times,
// we can find the code jumping to a remote function(e.g., memcpy or
// alloc), then resumes execution at a local location. Previous cache
// key only optimizes for local operations, while this new cache key
// balances the code between a 8192-byte local region, and certain remote
// code region. Notice the value 12 and 8 here are chosen by empirical
// evidence.
((pc & 0xFF) | (pc >> 12 << 8)) as usize % INSTRUCTION_CACHE_SIZE
// This indexing strategy optimizes instruction cache utilization by improving the distribution of addresses.
// - `pc >> 5`: Incorporates higher bits to ensure a more even spread across cache indices.
// - `pc << 1`: Spreads lower-bit information into higher positions, enhancing variability.
// - `^` (XOR): Further randomizes index distribution, reducing cache conflicts and improving hit rates.
//
// This approach helps balance cache efficiency between local execution and remote function calls,
// reducing hotspots and improving overall performance.
((pc >> 5) ^ (pc << 1)) as usize % INSTRUCTION_CACHE_SIZE
};
let cached_instruction = self.instructions_cache[instruction_cache_key];
if cached_instruction.0 == pc {
Expand Down
4 changes: 2 additions & 2 deletions src/machine/asm/execute_x64.S
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ ckb_vm_x64_execute:
andq CKB_VM_ASM_INVOKE_DATA_OFFSET_FIXED_TRACE_MASK(INVOKE_DATA), %rax
imul $CKB_VM_ASM_FIXED_TRACE_STRUCT_SIZE, %eax
movq CKB_VM_ASM_INVOKE_DATA_OFFSET_FIXED_TRACES(INVOKE_DATA), %rdx
prefetcht2 0(%rdx, %rax)
prefetchnta 0(%rdx, %rax)
lea CKB_VM_ASM_TRACE_OFFSET_THREADS(TRACE), INST_PC
mov INST_PC, INST_ARGS
add $8, INST_ARGS
Expand All @@ -468,7 +468,7 @@ ckb_vm_x64_execute:
/* Load current instruction as the full trace address */
movq -16(INST_ARGS), TRACE
/* Prefetch trace info for the consecutive block */
prefetcht2 CKB_VM_ASM_TRACE_OFFSET_THREADS(TRACE)
prefetchnta CKB_VM_ASM_TRACE_OFFSET_THREADS(TRACE)
mov CKB_VM_ASM_TRACE_OFFSET_LENGTH(TRACE), %edx
movq CKB_VM_ASM_ASM_CORE_MACHINE_OFFSET_CYCLES(MACHINE), %rax
addq CKB_VM_ASM_TRACE_OFFSET_CYCLES(TRACE), %rax
Expand Down