Skip to content

Commit ea1fae9

Browse files
committed
Switch oplog to VecDeque
We are constantly removing items from the beginning of it and pushing things onto the end, which causes a lot of reallocations for a `Vec`, but not for a `VecDeque`. Fixes #79.
1 parent 7bb1327 commit ea1fae9

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

left-right/src/write.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Absorb;
22
use crate::read::ReadHandle;
33

4+
use std::collections::VecDeque;
45
use std::ptr::NonNull;
56
use std::sync::atomic;
67
use std::sync::{Arc, MutexGuard};
@@ -24,7 +25,7 @@ where
2425
{
2526
epochs: crate::Epochs,
2627
w_handle: NonNull<T>,
27-
oplog: Vec<O>,
28+
oplog: VecDeque<O>,
2829
swap_index: usize,
2930
r_handle: ReadHandle<T>,
3031
last_epochs: Vec<usize>,
@@ -116,7 +117,7 @@ where
116117
epochs,
117118
// safety: Box<T> is not null and covariant.
118119
w_handle: unsafe { NonNull::new_unchecked(Box::into_raw(Box::new(w_handle))) },
119-
oplog: Vec::new(),
120+
oplog: VecDeque::new(),
120121
swap_index: 0,
121122
r_handle,
122123
last_epochs: Vec::new(),
@@ -264,14 +265,16 @@ where
264265
/// Returns true if there are operations in the operational log that have not yet been exposed
265266
/// to readers.
266267
pub fn has_pending_operations(&self) -> bool {
268+
// NOTE: we don't use self.oplog.is_empty() here because it's not really that important if
269+
// there are operations that have not yet been applied to the _write_ handle.
267270
self.swap_index < self.oplog.len()
268271
}
269272

270273
/// Append the given operation to the operational log.
271274
///
272275
/// Its effects will not be exposed to readers until you call [`publish`](Self::publish).
273276
pub fn append(&mut self, op: O) -> &mut Self {
274-
self.oplog.push(op);
277+
self.oplog.push_back(op);
275278
self
276279
}
277280

@@ -389,7 +392,7 @@ mod tests {
389392
// pin the epoch
390393
let _count = r.enter();
391394
// refresh would hang here
392-
assert!(w.oplog[w.swap_index..].is_empty());
395+
assert_eq!(w.oplog.iter().skip(w.swap_index).count(), 0);
393396
assert!(!w.has_pending_operations());
394397
}
395398

0 commit comments

Comments
 (0)