@@ -16,8 +16,9 @@ use crate::{
1616 tables:: { MemFinalRecord , MemInitRecord , ProgramTableCircuit , ProgramTableConfig } ,
1717} ;
1818use ceno_emul:: {
19- Addr , ByteAddr , CENO_PLATFORM , Cycle , EmuContext , InsnKind , IterAddresses , Platform , Program ,
20- StepRecord , Tracer , VMState , WORD_SIZE , Word , WordAddr , host_utils:: read_all_messages,
19+ Addr , ByteAddr , CENO_PLATFORM , Cycle , EmuContext , InsnKind , IterAddresses , NextAccessPair ,
20+ NextCycleAccess , Platform , Program , StepRecord , Tracer , VMState , WORD_SIZE , Word , WordAddr ,
21+ host_utils:: read_all_messages,
2122} ;
2223use clap:: ValueEnum ;
2324use either:: Either ;
@@ -147,7 +148,7 @@ pub struct ShardContext<'a> {
147148 shards : Shards ,
148149 max_cycle : Cycle ,
149150 // TODO optimize this map as it's super huge
150- addr_future_accesses : Cow < ' a , HashMap < ( WordAddr , Cycle ) , Cycle > > ,
151+ addr_future_accesses : Cow < ' a , NextCycleAccess < NextAccessPair > > ,
151152 read_thread_based_record_storage :
152153 Either < Vec < BTreeMap < WordAddr , RAMRecord > > , & ' a mut BTreeMap < WordAddr , RAMRecord > > ,
153154 write_thread_based_record_storage :
@@ -161,7 +162,7 @@ impl<'a> Default for ShardContext<'a> {
161162 Self {
162163 shards : Shards :: default ( ) ,
163164 max_cycle : Cycle :: default ( ) ,
164- addr_future_accesses : Cow :: Owned ( HashMap :: new ( ) ) ,
165+ addr_future_accesses : Cow :: Owned ( Default :: default ( ) ) ,
165166 read_thread_based_record_storage : Either :: Left (
166167 ( 0 ..max_threads)
167168 . into_par_iter ( )
@@ -183,7 +184,7 @@ impl<'a> ShardContext<'a> {
183184 pub fn new (
184185 shards : Shards ,
185186 executed_instructions : usize ,
186- addr_future_accesses : HashMap < ( WordAddr , Cycle ) , Cycle > ,
187+ addr_future_accesses : NextCycleAccess < NextAccessPair > ,
187188 ) -> Self {
188189 // current strategy: at least each shard deal with one instruction
189190 let max_num_shards = shards. max_num_shards . min ( executed_instructions) ;
@@ -329,8 +330,21 @@ impl<'a> ShardContext<'a> {
329330 }
330331
331332 // check write to external mem bus
332- if let Some ( future_touch_cycle) = self . addr_future_accesses . get ( & ( addr, cycle) )
333- && * future_touch_cycle >= self . cur_shard_cycle_range . end as Cycle
333+ if let Some ( future_touch_cycle) =
334+ self . addr_future_accesses
335+ . get ( cycle as usize )
336+ . and_then ( |res| {
337+ if res. len ( ) == 1 {
338+ Some ( res[ 0 ] . 1 )
339+ } else if res. len ( ) > 1 {
340+ res. iter ( )
341+ . find ( |( m_addr, _) | * m_addr == addr)
342+ . map ( |( _, cycle) | * cycle)
343+ } else {
344+ None
345+ }
346+ } )
347+ && future_touch_cycle >= self . cur_shard_cycle_range . end as Cycle
334348 && self . is_current_shard_cycle ( cycle)
335349 {
336350 let ram_record = self
0 commit comments