Skip to content

Commit b1fc5a7

Browse files
authored
Merge pull request #22 from ing-systems/cache-offsets-on-iter
Add offsets caching during iteration.
2 parents 37c8042 + b3b0831 commit b1fc5a7

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/lib.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,21 +510,46 @@ impl QueueFile {
510510
Ok(self.inner.file.sync_all()?)
511511
}
512512

513+
fn cache_last_offset_if_needed(&mut self, affected_items: usize) {
514+
if self.elem_cnt == 0 {
515+
return;
516+
}
517+
518+
let need_to_cache = self.offset_cache_kind.map_or(false, |kind| {
519+
let last_index = self.elem_cnt - 1;
520+
521+
match kind {
522+
OffsetCacheKind::Linear { offset } => {
523+
let last_cached_index = self.cached_offsets.back().map_or(0, |(idx, _)| *idx);
524+
last_index - last_cached_index >= offset
525+
}
526+
OffsetCacheKind::Quadratic => {
527+
let x = (last_index as f64).sqrt() as usize;
528+
x > 1 && (self.elem_cnt - affected_items..=last_index).contains(&(x * x))
529+
}
530+
}
531+
});
532+
533+
if need_to_cache {
534+
self.cache_last_offset();
535+
}
536+
}
537+
513538
fn cache_last_offset(&mut self) {
514539
debug_assert!(self.elem_cnt != 0);
515540

516-
let i = self.elem_cnt - 1;
541+
let index = self.elem_cnt - 1;
517542

518-
if let Some((index, elem)) = self.cached_offsets.back() {
519-
if *index == i {
520-
debug_assert_eq!(elem.pos, self.last.pos);
521-
debug_assert_eq!(elem.len, self.last.len);
543+
if let Some((last_cached_index, last_cached_elem)) = self.cached_offsets.back() {
544+
if *last_cached_index == index {
545+
debug_assert_eq!(last_cached_elem.pos, self.last.pos);
546+
debug_assert_eq!(last_cached_elem.len, self.last.len);
522547

523548
return;
524549
}
525550
}
526551

527-
self.cached_offsets.push_back((i, self.last));
552+
self.cached_offsets.push_back((index, self.last));
528553
}
529554

530555
#[inline]
@@ -588,23 +613,7 @@ impl QueueFile {
588613
self.write_header(self.file_len(), self.elem_cnt + count, self.first.pos, self.last.pos)?;
589614
self.elem_cnt += count;
590615

591-
if let Some(kind) = self.offset_cache_kind {
592-
let last_index = self.elem_cnt - 1;
593-
let need_to_cache = match kind {
594-
OffsetCacheKind::Linear { offset } => {
595-
let last_cached_index = self.cached_offsets.back().map_or(0, |(idx, _)| *idx);
596-
last_index - last_cached_index >= offset
597-
}
598-
OffsetCacheKind::Quadratic => {
599-
let x = (last_index as f64).sqrt() as usize;
600-
x > 1 && (self.elem_cnt - count..=last_index).contains(&(x * x))
601-
}
602-
};
603-
604-
if need_to_cache {
605-
self.cache_last_offset();
606-
}
607-
}
616+
self.cache_last_offset_if_needed(count);
608617

609618
Ok(())
610619
}
@@ -1290,6 +1299,8 @@ impl Iter<'_> {
12901299
.wrap_pos(current.pos + Element::HEADER_LENGTH as u64 + current.len as u64);
12911300
self.next_elem_index += 1;
12921301

1302+
self.queue_file.cache_last_offset_if_needed(1);
1303+
12931304
Some(&self.buffer[..current.len])
12941305
}
12951306
}

0 commit comments

Comments
 (0)