From 9ae740fb49a0c0debfa0c2e42d2ced525a185dd1 Mon Sep 17 00:00:00 2001 From: Quaternions <krakow20@gmail.com> Date: Wed, 15 Jan 2025 01:01:34 -0800 Subject: [PATCH] InstructionCache --- lib/common/src/instruction.rs | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lib/common/src/instruction.rs b/lib/common/src/instruction.rs index d1d6e36..8643033 100644 --- a/lib/common/src/instruction.rs +++ b/lib/common/src/instruction.rs @@ -33,6 +33,42 @@ impl<I,T,X> InstructionFeedback<I,T> for X X:InstructionEmitter<I,TimeInner=T>+InstructionConsumer<I,TimeInner=T>, {} +pub struct InstructionCache<S,I,T>{ + instruction_machine:S, + cached_instruction:Option<TimedInstruction<I,T>>, + time_limit:Time<T>, +} +impl<S,I,T> InstructionCache<S,I,T> + where + Time<T>:Copy+Ord, + Option<TimedInstruction<I,T>>:Clone, + S:InstructionEmitter<I,TimeInner=T>+InstructionConsumer<I,TimeInner=T> +{ + pub fn new( + instruction_machine:S, + )->Self{ + Self{ + instruction_machine, + cached_instruction:None, + time_limit:Time::MIN, + } + } + pub fn next_instruction_cached(&mut self,time_limit:Time<T>)->Option<TimedInstruction<I,T>>{ + if time_limit<self.time_limit{ + return self.cached_instruction.clone(); + } + let next_instruction=self.instruction_machine.next_instruction(time_limit); + self.cached_instruction=next_instruction.clone(); + self.time_limit=time_limit; + next_instruction + } + pub fn process_instruction(&mut self,instruction:TimedInstruction<I,T>){ + // invalidate cache + self.time_limit=Time::MIN; + self.instruction_machine.process_instruction(instruction); + } +} + //PROPER PRIVATE FIELDS!!! pub struct InstructionCollector<I,T>{ time:Time<T>,