Cow<[T]>
layout forces unnecessary branching
#117763
Labels
A-layout
Area: Memory layout of types
C-bug
Category: This is a bug.
I-heavy
Issue: Problems and improvements with respect to binary size of generated code.
I-slow
Issue: Problems and improvements with respect to performance of generated code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
I tried this code (Godbolt):
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
andBorrowed
variants of theCow
.The ideal layout for
Cow<[T]>
(andCow<str>
) would look like this:The present layout looks like this:
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
:@rustbot label A-layout I-heavy I-slow T-compiler T-libs
The text was updated successfully, but these errors were encountered: