Uh oh!
There was an error while loading. Please reload this page.
btree: avoid forcing the allocator to be a reference - #98178
Conversation
rust-highfive
commented
Jun 16, 2022
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
rust-highfive
commented
Jun 16, 2022
r? @thomcc (rust-highfive has picked a reviewer for you, use r? to override) |
RalfJung
commented
Jun 16, 2022
Ah there are still some |
Amanieu
commented
Jun 16, 2022
Could this be changed to require |
RalfJung
commented
Jun 16, 2022
via email
I figured Copy would be simpler and good enough since every allocator can be made Copy by using &A. But yeah it can probably be Clone. Not sure when I will get around to do that. |
thomcc
commented
Jun 17, 2022
I agree Clone sounds better, at least if it's what HashMap does. @rustbot label +S-waiting-on-author -S-waiting-on-review |
Okay I made it |
Uh oh!
There was an error while loading. Please reload this page.
thomcc
commented
Jun 17, 2022
It holding an |
TennyZhuang
commented
Jun 17, 2022
In fact, it's always hard to use |
RalfJung
commented
Jun 17, 2022
This is blocking getting Miri to work again (#98107), so would be good to get it landed soon. :) |
thomcc
commented
Jun 17, 2022
Thanks, this looks fine to me. @bors r+ |
bors
commented
Jun 17, 2022
📌 Commit 3a1e114 has been approved by |
RalfJung
commented
Jun 18, 2022
@bors p=1 |
bors
commented
Jun 18, 2022
bors
commented
Jun 18, 2022
☀️ Test successful - checks-actions |
rust-timer
commented
Jun 18, 2022
Finished benchmarking commit (ff86b27): comparison url. Instruction count
Max RSS (memory usage)Results
CyclesResults
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression Footnotes |
The structAllocRef<'a,A>{allocator:[&'aA;(size_of::<A>() > 0)asusize],}impl<'a,A:Allocator>DerefforAllocRef<'a,A>{typeTarget = A;fnderef(&self) -> &A{// This should be fine?self.allocator.get(0).unwrap_or_else(|| unsafe{NonNull::dangling().as_ref()})}}impl<'a,A>CopyforAllocRef<'a,A>{}impl<'a,A>CloneforAllocRef<'a,A>{
...
}which preserves the size of ZSTs but doesn't unnecessarily require |
RalfJung
commented
Jun 18, 2022
This sounds like something a user of BTreeMap / HashMap could also do? |
joboet
commented
Jun 18, 2022
No, because that would require the allocator to be a reference. The problem is that some allocators cannot be cloned, but the If something like |
thomcc
commented
Jun 18, 2022
@joboet That's very clever. Are you interested in submitting a PR for it? |
RalfJung
commented
Jun 18, 2022
Oh I see, so you want BTreeMap to own the allocator but the |
ssomers
commented
Jun 21, 2022
Allowing |
If that internal helper function creates a IOW, for the default allocator |
ssomers
commented
Jun 23, 2022
I understand. But then I don't understand the use of Allocator + Clone everywhere and not Allocator + Copy (obviously, it would be better without Clone but I sure don't know how). An allocator is used for efficiency, as far as I know. A allocator used in a BTreeMap (and I assume in hashbrown too) can only be used efficiently if it is cheap to clone, because cloning happens all over the place. If people are prepared to implement Clone but not to commit to Copy, why lure them into using it in a map? |
RalfJung
commented
Jun 23, 2022
Yeah, well, I was in favor of Anyway discussions on closed PRs are not usually a good idea, so it'd probably be better if you opened a new issue for this. |
TennyZhuang
commented
Jun 23, 2022
I found it’s really hard to implement an allocator with reference but without Arc. The struct who owned the map will also owns the allocator itself, which means that it’s unavoidable to introduce Pin, pin_project, and many other complicated and even unsafe codes to implement the self-referential data structure. Using Arc here at least can make it simple and clear. |
The previous code forces the actual allocator used to be some
&A. This generalizes the code to allow anyA: Copy. If people truly want to use a reference, they can use&Athemselves.Fixes#98176