-
Couldn't load subscription status.
- Fork 13.9k
Description
I tried this code (Godbolt):
pub fn test<'a>(cow: &'a std::borrow::Cow<'a, [u8]>) -> &'a [u8] {
&*cow
}I expected to see this happen: The generated assembly is branchless. The pointer and length are at the same offset in both variants of the Cow.
Instead, this happened: There is a branch in the generated assembly. The pointer to the slice is at a different offset in the Owned and Borrowed variants of the Cow.
The ideal layout for Cow<[T]> (and Cow<str>) would look like this:
If `T` is not a ZST:
┌──────────┬──────────┬────────────────────────────────────────────────────┐
│ pointer │ length │ capacity if `Owned`, or `usize::MAX` if `Borrowed` │
└──────────┴──────────┴────────────────────────────────────────────────────┘
If `T` is a ZST:
┌──────────┬──────────┬────────────────────────────────────────┐
│ pointer │ length │ boolean flag for `Owned` vs `Borrowed` │
└──────────┴──────────┴────────────────────────────────────────┘
The present layout looks like this:
`Owned` variant:
┌──────────┬──────────┬──────────┐
│ pointer │ capacity │ length │
└──────────┴──────────┴──────────┘
`Borrowed` variant:
┌──────────┬──────────┬──────────┐
│ 0x0 │ pointer │ length │
└──────────┴──────────┴──────────┘
Because of this non-optimal layout, every access to the contained pointer requires a branch on the enum variant.
(Fixing #45431 is a prerequisite to fixing this)
Meta
rustc --version --verbose:
rustc 1.75.0-nightly (189d6c71f 2023-11-06)
binary: rustc
commit-hash: 189d6c71f3bb6c52113b5639a80839791974fd22
commit-date: 2023-11-06
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.4
@rustbot label A-layout I-heavy I-slow T-compiler T-libs