Zeroize performance on u8 arrays
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 674
- Forks
- 170
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 10
Description
I inspected the generated assembly code and benchmarked zeroize for [u8; 32] on x86_64 and found it quite inefficient, storing one byte at a time:
On my Ryzen CPU, it takes ~7.8324 ns, or ~1cpb. Binary code size is also quite large.
Using inline assembly (just stabilized in 1.59) and SSE2, zeroing a [u8; 32] takes just 3 instructions and ~492.87 ps (~16 bytes per cycle):
let mut buf: [u8; 32];
core::arch::asm!(
"xorps {zero}, {zero}",
"movups {zero}, ({ptr})",
"movups {zero}, 16({ptr})",
zero = out(xmm_reg) _,
ptr = in(reg) &mut buf,
options(att_syntax, nostack, preserves_flags),
);
So it might be something worth optimizing/documenting.
If you do not want to use inline assembly, maybe you should encourage using larger types or SIMD types, e.g., [u64; 4] or [__m128; 2] instead of [u8; 32]. Using write_volatile on *mut __m128 generates equally compact and efficient code as the assembly code above.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Rust Playground benchmark linked in the issue and inspect the generated assembly for zeroize on [u8; 32]. Compare the current output with the inline-assembly and larger-type approaches described in the report. Done requires an agreed optimization or documentation change, with its performance and code-size impact verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100