Codegen significantly worse when using u128 rather than two u64
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
fn read_bits(data: &mut u128, n_bits: u32) -> u32 {
let ret = *data & ((1 << n_bits) - 1);
*data >>= n_bits;
ret as u32
}
This replaces the original code which was emulating the same operation using two u64:
struct Bitstream {
low: u64,
high: u64,
}
impl Bitstream {
fn read_bits(&mut self, num_bits: u32) -> u32 {
let mask = (1 << num_bits) - 1;
let bits = self.low & mask;
self.low >>= num_bits;
self.low |= (self.high & mask) << (u64::BITS as u64 - num_bits as u64);
self.high >>= num_bits;
bits as u32
}
}
I expected to see this happen: no conditional code should be generated, resulting in the same performances as the u64 code.
Instead, this happened: the codegen is significantly worse. For a BC7 decoding crate the blocks are decoded 20 ns slower when using u128 instead of the existing high and low u64 (163.48 ns per block → 145.83 ns per block, -10.749% including randomness overhead). The same happens with an ASTC decoding crate. This is a followup of https://github.com/rust-lang/rust/issues/122252.
Meta
rustc --version --verbose:
rustc 1.79.0-nightly (9d5cdf75a 2024-04-07)
Contributor guide
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 by reproducing the comparison with the provided Rust snippets and the linked BC7 and ASTC decoding crates, then inspect the generated code for the u128 and two-u64 versions. Compare the current nightly behavior with the expected branch-free operation and determine whether the regression is in rustc codegen. Done means the u128 version no longer produces the reported performance gap without regressing the existing cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100