zeroize 1.9.1 -> 1.9.2 performance regression for short Strings
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 30/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- linux, rust
- 領域
- performance
調査の方向性
The regression is in the String Zeroize impl and zeroize_byte_ptr in zeroize's source, where the unconditional write_bytes call on spare capacity lives. Start by reproducing the benchmark in the issue for 1.9.1 versus 1.9.2 and checking the len == 0 early return. Done means a fix that keeps zeroing guarantees, with the benchmark numbers recovered and the existing tests passing.
索引モデルが issue の本文から書いたものです。
説明
Summary
In a same-machine A/B where only zeroize changed (1.9.1 → 1.9.2), an application that drops many short zeroize-on-drop Strings lost 4–10% throughput on AMD Zen 4 and Zen 5. A workload in the same application that drops none of these strings did not change.
In my build, 1.9.2's <String as Zeroize>::zeroize wipes the spare capacity with an unconditional call to libc memset, including when the spare capacity is 0. In 1.9.1 the same function was a leaf with inline byte stores. In the application, 76% of these memset calls had length 0, and all were under 64 bytes.
The regression appears in the randomized multi-string repro below (about +22 ns per zeroize() call), but not in a single-string hot-loop benchmark.
Environment
- AMD EPYC Zen 4 server CPU, Linux 6.12, x86-64
- glibc 2.34;
memsetresolves to__memset_avx512_unaligned_erms - rustc 1.97.1,
-C opt-level=3, default target CPU - zeroize 1.9.1 and 1.9.2 from crates.io, default features
I observed the application-level slowdown on Zen 5 as well, but ran the repro only on Zen 4. I have not tested Intel or aarch64.
Codegen difference
<alloc::string::String as zeroize::Zeroize>::zeroize in my build (PIE binary):
1.9.1, a leaf function with no calls:
mov (%rdi),%rsi ; capacity
mov 0x8(%rdi),%rax ; ptr
mov 0x10(%rdi),%rcx ; len
... ; inline byte-store loop over spare capacity (skipped when 0)
... ; inline byte-store loop over len
movq $0x0,0x10(%rdi)
ret
1.9.2:
push %r15
push %r14
push %r12
push %rbx
push %rax
...
lea (%r15,%r12,1),%r14 ; ptr + len
sub %r12,%rdx ; capacity - len, possibly 0
mov %r14,%rdi
xor %esi,%esi
call *memset@GOT ; unconditional
... ; inline byte-store loop over len (unchanged)
For lengths under 64 bytes, glibc's AVX-512 memset does a single masked store (vmovdqu8 %zmm16,(%rax){%k1}), which gets about 80% of the perf samples inside memset. Most of the added time shows up in the caller rather than inside memset. Data-TLB miss counts were similar for both versions (96.1M vs 96.4M). I haven't isolated the cause beyond that.
Repro
Cargo.toml:
[package]
name = "zeroize-short-string-bench"
version = "0.1.0"
edition = "2024"
[dependencies]
zeroize = "=1.9.2" # switch to "=1.9.1" to compare
src/main.rs:
// Zeroizes many short, live Strings in random order and reports the cost of
// `zeroize()` over `clear()`.
use std::hint::black_box;
use std::time::Instant;
use zeroize::Zeroize;
fn xorshift(s: &mut u64) -> u64 {
*s ^= *s << 13;
*s ^= *s >> 7;
*s ^= *s << 17;
*s
}
fn main() {
const N: usize = 1 << 20;
const RUNS: usize = 15;
let src = "x".repeat(64);
let mut st = 0x9E37_79B9_7F4A_7C15u64;
// Lengths 1..=48; spare capacity 0 for 76% of strings, 1..=15 for 21%,
// 16..=63 for 3%.
let shapes: Vec<(usize, usize)> = (0..N)
.map(|_| {
let len = 1 + (xorshift(&mut st) % 48) as usize;
let r = xorshift(&mut st) % 100;
let spare = match r {
0..76 => 0,
76..97 => 1 + (xorshift(&mut st) % 15) as usize,
_ => 16 + (xorshift(&mut st) % 48) as usize,
};
(len, len + spare)
})
.collect();
let mut strings: Vec<String> = shapes.iter().map(|&(_, c)| String::with_capacity(c)).collect();
let mut order: Vec<usize> = (0..N).collect();
for i in (1..N).rev() {
order.swap(i, (xorshift(&mut st) % (i as u64 + 1)) as usize);
}
let mut pass = |zero: bool| {
let t = Instant::now();
for &i in &order {
let s = &mut strings[i];
s.push_str(black_box(&src[..shapes[i].0]));
if zero { s.zeroize() } else { s.clear() }
black_box(&*s);
}
t.elapsed().as_nanos() as f64 / N as f64
};
let (mut clear, mut zero) = (Vec::new(), Vec::new());
for _ in 0..RUNS {
clear.push(pass(false));
zero.push(pass(true));
}
clear.sort_by(f64::total_cmp);
zero.sort_by(f64::total_cmp);
let (c, z) = (clear[RUNS / 2], zero[RUNS / 2]);
println!("clear: {c:.1} ns/op zeroize: {z:.1} ns/op zeroize overhead: {:.1} ns/op", z - c);
}
Run with taskset -c <core> cargo run --release.
The spare-capacity split in the comment is the requested mix (the capacity passed to String::with_capacity). It approximates what I measured in the application.
Results
Each run of the program prints the median zeroize time minus the median clear time, over 15 passes of each. I ran each build five times, alternating builds on the same core. The table shows the median and range of those five results.
| Build | zeroize overhead (ns/op) | Range over 5 runs |
|---|---|---|
| 1.9.1 | 58.7 | 57.8–59.2 |
| 1.9.2 | 80.7 | 79.3–85.0 |
1.9.2 + len == 0 early return |
66.2 | 64.5–66.9 |
clear() cost the same in all three builds (54.2–55.5 ns/op).
A hot loop that repeatedly allocates, zeroizes and drops one short String shows no regression; 1.9.2 is 1–2 ns faster there at 8–32 bytes. Larger buffers are much faster in 1.9.2, so this report is only about short lengths.
In the application, the 1.9.2 build made 2.48M more memset calls per run (5.27M vs 2.78M) and took about 60 ms longer. That averages to roughly 24 ns per added call. This is an amortized figure, not a per-call measurement.
Possible fix
Skipping zero-length calls in zeroize_byte_ptr produced the third row of the table:
pub(crate) unsafe fn zeroize_byte_ptr(dst: *mut u8, len: usize) {
+ if len == 0 {
+ return;
+ }
if HAS_ASM {
Short non-zero lengths may account for the remaining gap; the benchmarks in #1604 also show the new path slower at 16–65 bytes. Would an inline path for small lengths, before falling back to write_bytes + optimization_barrier, be worth testing? It would be close to newpavlov's suggestion in #1604 of a plain write loop plus optimization_barrier on asm! targets.
Related: #1504 (1.8.2 → 1.9.0 regression, fixed by #1535), #1602–#1607.
- 主要言語
- Rust
- スター
- 695
- フォーク
- 185
- 平均マージ
- 10時間 40分
- マージ済み PR(30日)
- 24
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
RustCrypto/utils のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
RustCrypto/utils#1555 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
RustCrypto/utils#1546 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
cpufeatures: aarch64 Linux reports aes, sha3 and sm4 when only one of their two HWCAPs is set対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
RustCrypto/utils#1627 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
RustCrypto/utils#1621 · コメント 17 件 ·
メンテナーはふだん 1 日以内に返信
-
cpubits: promoting wasm32 to 64-bit makes k256 0.14 up to 3.3x slower in Wasm (p256 0.14 is faster)オープン
難易度 5/5 1週間以上 初心者へのやさしさ 15/100
RustCrypto/utils#1620 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
RustCrypto/utils の issue をすべて見る
似ている issue
-
The new tests repeat each other and one only checks a two-line helper再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンinfra
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
storytold/wordcraft#9405 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
in-progress
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
area: ai-assistants documentation help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
BetaBots-LLC/latticegate#17 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 94/100
storytold/craft-libs#5594 ·
メンテナーはふだん 1 日以内に返信
-
available documentation good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Nixitov/Riptide-Native#932 ·
メンテナーはふだん 1 日以内に返信