-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-layoutArea: Memory layout of typesArea: Memory layout of typesC-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-heavyIssue: Problems and improvements with respect to binary size of generated code.Issue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
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
Metadata
Metadata
Assignees
Labels
A-layoutArea: Memory layout of typesArea: Memory layout of typesC-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-heavyIssue: Problems and improvements with respect to binary size of generated code.Issue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.