-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix in-place collect not reallocating when necessary #118460
Conversation
@@ -168,7 +168,7 @@ const fn in_place_collectible<DEST, SRC>( | |||
step_merge: Option<NonZeroUsize>, | |||
step_expand: Option<NonZeroUsize>, | |||
) -> bool { | |||
if DEST::IS_ZST || mem::align_of::<SRC>() < mem::align_of::<DEST>() { | |||
if const { SRC::IS_ZST || DEST::IS_ZST || mem::align_of::<SRC>() < mem::align_of::<DEST>() } { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not part of the fix. the condition was already enforced before, I'm just making it more explicit for readability.
This comment has been minimized.
This comment has been minimized.
I can confirm that with this patch applied, the test suite passes in Miri. |
4268a02
to
35a2d1c
Compare
The current implementation in this PR looks like it is trying to enumerate the cases where reallocation is not permitted, then if none of those cases are hit we fall through to permitting allocation reuse. I would much prefer to see this implemented with the inverse strategy; try to enumerate the cases where allocation reuse is permitted, then fall through to requiring reallocation. Alignment is trivial though, so I don't mind handling that differently. So I'd prefer to see something where either we early-return That being said, I slapped together this alternative implementation and it seems to pass the test suite: const fn needs_realloc<SRC, DEST>(src_cap: usize, dst_cap: usize) -> bool {
// Alignment must match exactly. Beyond this check we do not need to reason about it.
if const { mem::align_of::<SRC>() != mem::align_of::<DEST>() } {
return true;
}
// Exact allocation size matches can be reused
let src_bytes = src_cap * mem::size_of::<SRC>();
let dest_bytes = dst_cap * mem::size_of::<DEST>();
if src_bytes == dest_bytes {
return false;
}
// Everything else needs a realloc
true
} This implementation seems much simpler to me and I'd prefer to see it (or something similar) used if it is equivalent. Is it? If it isn't, I think we're missing a test. |
The difference is that I'm doing more |
new trophy case entry Cc rust-lang/rust#118460
new trophy case entry Cc rust-lang#118460
35a2d1c
to
9d0f8ee
Compare
I rewrote the |
// If src size is an integer multiple of the destination size | ||
// the resulting capacity will also always be an integer multiple without remainder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// If src size is an integer multiple of the destination size | |
// the resulting capacity will also always be an integer multiple without remainder | |
// If src size is an integer multiple of the destination size, we know the capacity we calculate | |
// with src_cap * size_of::<SRC> / size_of::<DEST> does not have a remainder. |
It took me a long time to understand this comment. It's referring to the division done outside this function. I've tried to make that clear.
r=me with this suggestion or equivalent clarification
9d0f8ee
to
13a843e
Compare
@bors r=saethlin |
@bors p=1 |
☀️ Test successful - checks-actions |
Finished benchmarking commit (15bb3e2): comparison URL. Overall result: ❌ regressions - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 674.58s -> 675.083s (0.07%) |
Regression introduced in #110353.
This was caught by miri
r? @saethlin