From 6b4f38d0303055f6593c558e3592f63b1d87918a Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Tue, 8 Jun 2021 15:07:51 +0200 Subject: [PATCH] [ElfLoader] added cache for already decoded instructions --- riscemu/priv/ElfLoader.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/riscemu/priv/ElfLoader.py b/riscemu/priv/ElfLoader.py index f908f4d..e6084fa 100644 --- a/riscemu/priv/ElfLoader.py +++ b/riscemu/priv/ElfLoader.py @@ -124,13 +124,26 @@ class ElfInstruction: class ElfLoadedMemorySection(LoadedMemorySection): + ins_cache: List[Optional[ElfInstruction]] + """ + A fast cache for accessing pre-decoded instructions + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.__setattr__('ins_cache', [None] * (self.size // 4)) + def read_instruction(self, offset): + if self.ins_cache[offset//4] is not None: + return self.ins_cache[offset//4] if not self.flags.executable: print(FMT_PARSE + "Reading instruction from non-executable memory!" + FMT_NONE) raise InstructionAccessFault(offset + self.base) if offset % 4 != 0: raise InstructionAddressMisalignedTrap(offset + self.base) - return ElfInstruction(*decode(self.content[offset:offset + 4])) + ins = ElfInstruction(*decode(self.content[offset:offset + 4])) + self.ins_cache[offset // 4] = ins + return ins @property def end(self):