Skip to content

Restore HistoryBuf write performance - #687

Open
wyf-777 wants to merge 2 commits into
rust-embedded:mainfrom
wyf-777:fix/historybuf-write-performance
Open

Restore HistoryBuf write performance#687
wyf-777 wants to merge 2 commits into
rust-embedded:mainfrom
wyf-777:fix/historybuf-write-performance

Conversation

@wyf-777

@wyf-777 wyf-777 commented Aug 28, 2026

Copy link
Copy Markdown

Summary

  • Fixes Performance regression for HistoryBuf(fer)::write() with heapless 0.9.1 #598.
  • Restore HistoryBuf::write() optimization after the generic storage/view refactor introduced repeated backing-slice indexing that LLVM could not eliminate.
  • Borrow the backing slice once, preserve the existing zero-capacity panic, and use the private write-position invariant to access the current slot without repeated bounds checks.
  • Add a Divan benchmark based on the issue reproducer so the regression remains measurable.

Safety

write_at starts at zero and is reset before reaching capacity. The zero-capacity case is rejected before the unchecked access, and debug builds additionally assert write_at < capacity. The HistoryBuf test suite passes under Miri, including wraparound, drop-panic, and zero-capacity paths.

Performance

Windows x86_64, Rust 1.97.1, 8 MiB issue workload (cargo bench --bench history_buf):

  • Before: 7.693 ms median, 1.09 GB/s median
  • After: 223.1 us median, 37.58 GB/s median

Testing

  • cargo test --features "alloc,mpmc_large,portable-atomic-critical-section,serde,ufmt,bytes,zeroize,embedded-io-v0.7" (294 unit, 8 concurrency, and 204 doc tests passed)
  • cargo check --features defmt
  • cargo test --release history_buf::tests (15 passed)
  • MIRIFLAGS=-Zmiri-ignore-leaks cargo +nightly-2025-10-11 miri test history_buf::tests (15 passed)
  • cargo +nightly-2025-10-11 fmt --all -- --check
  • CI-equivalent Clippy for i686-unknown-linux-musl with all configured features
  • Default and alloc checks for i686-unknown-linux-musl, riscv32imc-unknown-none-elf, armv7r-none-eabi, thumbv6m-none-eabi, thumbv7m-none-eabi, thumbv8m.base-none-eabi, and thumbv8m.main-none-eabi
  • git diff --check

The Windows host cannot link executable tests with the embedded defmt feature because its runtime symbols are intentionally supplied by an embedded target; the feature itself passed cargo check, and the repository CI exercises the supported Linux configuration.

AI assistance

OpenAI Codex (GPT-5) assisted with issue analysis, implementation, benchmark construction, test execution, and pull request wording. The commit includes the corresponding Assisted-by trailer.

Reuse the backing slice and access the write slot without repeated bounds checks. The private write-position invariant keeps the unchecked access in bounds, while zero-capacity buffers continue to panic.

Add a Divan benchmark for the regression reported in issue rust-embedded#598.

Assisted-by: OpenAI Codex:GPT-5
Document the restored write performance in the Unreleased section.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance regression for HistoryBuf(fer)::write() with heapless 0.9.1

1 participant