inout: `xor_in2out` compiles to a byte-at-a-time loop at `opt-level = "s"`, and is ~40% slower than word-wise XOR even at `opt-level = 3`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- rust
- 領域
- performance
調査の方向性
Start with inout/src/inout.rs and inspect InOut::xor_in2out, then use the linked inout-xor-repro crate to reproduce the byte-at-a-time loop at opt-level = "s". Compare the generated behavior and throughput for the suggested word-wise XOR approaches across the supplied profiles; done means the XOR avoids the reported slowdown while producing identical output.
索引モデルが issue の本文から書いたものです。
説明
Crate: inout 0.2.6 (via cipher 0.5.2, ctr 0.10.1, aes 0.9.4)
Summary
StreamCipher::apply_keystream on ctr::Ctr128BE<aes::Aes256> spends most of its time XORing the keystream into the data in InOut::xor_in2out, not in AES. In a binary built at opt-level = "s" (a common choice for size-sensitive targets such as routers), the XOR is one byte at a time and the cipher runs at a third of the speed of the same keystream XORed by hand. At opt-level = 3 the gap narrows but remains about 40%.
Reproduction
A self-contained crate with a pinned Cargo.lock: https://github.com/ours-core/inout-xor-repro. It compares apply_keystream with write_keystream into a 2 KiB stack buffer followed by a 16-byte-at-a-time XOR (u128::from_ne_bytes), over 1 GiB in 16 KiB pieces. Both produce the same output; the program asserts it.
git clone https://github.com/ours-core/inout-xor-repro && cd inout-xor-repro
cargo run --locked --profile release # fat LTO, codegen-units = 1, opt-level = "s"
cargo run --locked --profile release-o3 # the same at opt-level = 3
cargo run --locked --profile release-hot # "s", with only the cipher crates and the caller at 3
Results
AMD EPYC 9554 (Zen 4, VAES), one core, rustc 1.100.0 (pinned in rust-toolchain.toml; 1.99.1 gives the same picture), best of 5 runs:
| profile | apply_keystream |
XOR by words | ratio |
|---|---|---|---|
opt-level = "s", fat LTO |
1.19 GB/s | 3.96 GB/s | 3.3× |
opt-level = 3, fat LTO |
5.79 GB/s | 8.29 GB/s | 1.4× |
The release-hot profile raises only aes, ctr, cipher, hybrid-array and the caller to
opt-level = 3 (not inout), and brings apply_keystream back to 6.01 GB/s (by words: 8.28 GB/s). We see the byte-wise loop in a larger fat-LTO binary configured the same way, but we have not yet reduced that case to a minimal example.
Where the time goes (opt-level = "s")
perf samples, the two hottest instructions (~53% of all samples together) are the inner loop of xor_in2out (inout/src/inout.rs), inlined into the CTR backend:
mov (%rcx,%rdi,1),%r8b
xor (%rax,%rdi,1),%r8b
mov %r8b,(%r15,%rdi,1)
inc %rdi
cmp $0x10,%rdi
jne ...
AES itself (vaesenc on zmm) accounts for ~21%.
The function copies the input into a temporary Array, XORs it byte by byte, and writes the temporary back:
let input = ptr::read(self.in_ptr);
let mut temp = Array::<u8, N>::default();
for i in 0..N::USIZE {
temp[i] = input[i] ^ data[i];
}
ptr::write(self.out_ptr, temp);
At opt-level = "s" LLVM does not vectorize this loop, so each 16-byte block costs 16 load/xor/store iterations plus the copies. At opt-level = 3 it is vectorized, but the round trip through temp still costs about 40% against XORing words in place.
Suggestion
XOR in machine words rather than relying on the auto-vectorizer, e.g. over chunks_exact(8) or chunks_exact(16) with u64/u128::from_ne_bytes, or over the block as a whole for the common N = 16. The by-words variant in the example stays fast at opt-level = "s" (3.96 GB/s) because the 16-byte u128 XOR becomes a single xorps regardless of the vectorizer.
We would be glad to send a PR if this direction is acceptable.
- 主要言語
- 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 日以内に返信
-
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 日以内に返信
-
難易度 2/5 1時間未満 初心者へのやさしさ 15/100
RustCrypto/utils#1617 ·
メンテナーはふだん 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 日以内に返信