@@ -107,11 +107,10 @@ pub struct MemoryCtx<const PAGE_BITS: usize> {
107107 pub boundary_idx : usize ,
108108 pub merkle_tree_index : Option < usize > ,
109109 pub adapter_offset : usize ,
110- pub continuations_enabled : bool ,
110+ continuations_enabled : bool ,
111111 chunk : u32 ,
112112 chunk_bits : u32 ,
113113 pub page_indices : BitSet ,
114- pub page_access_count : usize ,
115114 pub addr_space_access_count : RVec < usize > ,
116115 pub page_indices_since_checkpoint : Box < [ u32 ] > ,
117116 pub page_indices_since_checkpoint_len : usize ,
@@ -140,7 +139,6 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
140139 memory_dimensions,
141140 continuations_enabled : config. continuation_enabled ,
142141 page_indices : BitSet :: new ( bitset_size) ,
143- page_access_count : 0 ,
144142 addr_space_access_count : vec ! [ 0 ; addr_space_size] . into ( ) ,
145143 page_indices_since_checkpoint : vec ! [ 0 ; page_indices_since_checkpoint_cap]
146144 . into_boxed_slice ( ) ,
@@ -257,7 +255,10 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
257255 /// Initialize state for a new segment
258256 #[ inline( always) ]
259257 pub ( crate ) fn initialize_segment ( & mut self , trace_heights : & mut [ u32 ] ) {
260- // Reset trace heights for memory chips
258+ // Clear page indices for the new segment
259+ self . page_indices . clear ( ) ;
260+
261+ // Reset trace heights for memory chips as 0
261262 // SAFETY: boundary_idx is a compile time constant within bounds
262263 unsafe {
263264 * trace_heights. get_unchecked_mut ( self . boundary_idx ) = 0 ;
@@ -277,15 +278,11 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
277278 // Apply height updates for all pages accessed since last checkpoint, and
278279 // initialize page_indices for the new segment.
279280 let mut addr_space_access_count = vec ! [ 0 ; self . addr_space_access_count. len( ) ] ;
280- let mut page_access_count = 0 ;
281- self . page_indices . clear ( ) ;
282-
283281 let pages_len = self . page_indices_since_checkpoint_len ;
284282 for i in 0 ..pages_len {
285283 // SAFETY: i is within 0..pages_len and pages_len is the slice length.
286284 let page_id = unsafe { * self . page_indices_since_checkpoint . get_unchecked ( i) } as usize ;
287285 if self . page_indices . insert ( page_id) {
288- page_access_count += 1 ;
289286 let ( addr_space, _) = self
290287 . memory_dimensions
291288 . index_to_label ( ( page_id as u64 ) << PAGE_BITS ) ;
@@ -298,9 +295,7 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
298295 }
299296 }
300297 }
301-
302- self . apply_height_updates ( trace_heights, page_access_count, & addr_space_access_count) ;
303-
298+ self . apply_height_updates ( trace_heights, & addr_space_access_count) ;
304299 self . page_indices_since_checkpoint_len = 0 ;
305300 }
306301
@@ -312,12 +307,9 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
312307
313308 /// Apply height updates given page counts
314309 #[ inline( always) ]
315- fn apply_height_updates (
316- & self ,
317- trace_heights : & mut [ u32 ] ,
318- page_access_count : usize ,
319- addr_space_access_count : & [ usize ] ,
320- ) {
310+ fn apply_height_updates ( & self , trace_heights : & mut [ u32 ] , addr_space_access_count : & [ usize ] ) {
311+ let page_access_count = addr_space_access_count. iter ( ) . sum ( ) ;
312+
321313 // On page fault, assume we add all leaves in a page
322314 let leaves = ( page_access_count << PAGE_BITS ) as u32 ;
323315 // SAFETY: boundary_idx is a compile time constant within bounds
@@ -346,7 +338,9 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
346338 }
347339 }
348340
349- for ( address_space, & x) in addr_space_access_count. iter ( ) . enumerate ( ) {
341+ for address_space in 0 ..addr_space_access_count. len ( ) {
342+ // SAFETY: address_space is from 0 to len(), guaranteed to be in bounds
343+ let x = unsafe { * addr_space_access_count. get_unchecked ( address_space) } ;
350344 if x > 0 {
351345 // Initial **and** final handling of touched pages requires send (resp. receive) in
352346 // chunk-sized units for the merkle chip
@@ -365,13 +359,7 @@ impl<const PAGE_BITS: usize> MemoryCtx<PAGE_BITS> {
365359 /// Resolve all lazy updates of each memory access for memory adapters/poseidon2/merkle chip.
366360 #[ inline( always) ]
367361 pub ( crate ) fn lazy_update_boundary_heights ( & mut self , trace_heights : & mut [ u32 ] ) {
368- let page_access_count = self . addr_space_access_count . iter ( ) . sum ( ) ;
369- self . apply_height_updates (
370- trace_heights,
371- page_access_count,
372- & self . addr_space_access_count ,
373- ) ;
374- self . page_access_count = 0 ;
362+ self . apply_height_updates ( trace_heights, & self . addr_space_access_count ) ;
375363 // SAFETY: Resetting array elements to 0 is always safe
376364 unsafe {
377365 std:: ptr:: write_bytes (
0 commit comments