Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Restored `HistoryBuf::write` performance after the generic storage refactor.
- Added `swap_remove()` to `IndexMap` and `IndexSet`.
- Deprecated `.remove()` in `IndexMap` and `IndexSet` in favour of `.swap_remove()`.
- Fixed `IndexMap::truncate` leading to an inconsistent state.
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ stable_deref_trait = { version = "1", default-features = false }

[dev-dependencies]
critical-section = { version = "1.1", features = ["std"] }
divan = "0.1.21"
static_assertions = "1.1.0"

[[bench]]
name = "history_buf"
harness = false

[package.metadata.docs.rs]
features = [
"bytes",
Expand Down
35 changes: 35 additions & 0 deletions benches/history_buf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::hint::black_box;

use divan::counter::BytesCount;
use heapless::HistoryBuf;

const NEEDLE: &[u8] = b"needle451";
const WINDOW_SIZE: usize = NEEDLE.len();

fn write_search_window(data: Vec<u8>) {
let mut search_window: HistoryBuf<u8, WINDOW_SIZE> = HistoryBuf::new();

for byte in data {
search_window.write(byte);
}
}

#[divan::bench]
fn history_buf_write(bencher: divan::Bencher) {
let total_bytes = 8 * 1024 * 1024;
let bytes_before_needle = total_bytes - NEEDLE.len();

bencher
.counter(BytesCount::new(total_bytes))
.with_inputs(|| {
let mut data = Vec::with_capacity(total_bytes);
data.resize(bytes_before_needle, 0);
data.extend(NEEDLE);
data
})
.bench_values(|data| write_search_window(black_box(data)));
}

fn main() {
divan::main();
}
25 changes: 22 additions & 3 deletions src/history_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,28 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
/// Writes an element to the buffer, overwriting the oldest value.
pub fn write(&mut self, t: T) {
let _tmp;
let write_at = self.write_at;
let data = self.data.borrow_mut();
let capacity = data.len();

assert!(capacity != 0, "cannot write to a zero-capacity HistoryBuf");
debug_assert!(write_at < capacity);

// SAFETY: write_at starts at zero and is reset before it reaches
// capacity. The zero-capacity case is rejected above, so write_at is
// a valid index into data.
let slot = unsafe { data.get_unchecked_mut(write_at) };

if self.filled {
// Copy the old so that it is dropped at the end
// We don't drop it now so that a panic in its destructor doesn't
// lead to an invalid state
_tmp = unsafe { ptr::read(self.data.borrow_mut()[self.write_at].as_mut_ptr()) };
_tmp = unsafe { ptr::read(slot.as_ptr()) };
}
self.data.borrow_mut()[self.write_at] = MaybeUninit::new(t);
*slot = MaybeUninit::new(t);

self.write_at += 1;
if self.write_at == self.capacity() {
if self.write_at == capacity {
self.write_at = 0;
self.filled = true;
}
Expand Down Expand Up @@ -710,6 +722,13 @@ mod tests {
assert_eq!(x.as_slice(), [10, 11, 12, 6]);
}

#[test]
#[should_panic(expected = "cannot write to a zero-capacity HistoryBuf")]
fn write_zero_capacity_panics() {
let mut x: HistoryBuf<u8, 0> = HistoryBuf::new_with(0);
x.write(1);
}

#[test]
fn clear() {
let mut x: HistoryBuf<u8, 4> = HistoryBuf::new_with(1);
Expand Down
Loading