`Cow<[T]>` layout forces unnecessary branching
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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
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 reading prerequisite issue #45431 and running the linked Godbolt example. Use the stated target layouts for non-ZST and ZST types, and branchless access to the contained pointer, as the completion criteria.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100