-
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
Implement placement-in protocol for Vec
#32366
Conversation
r? @brson (rust_highfive has picked a reviewer for you, use r? to override) |
ptr::write(end, value); | ||
self.len += 1; | ||
} | ||
self <- value; |
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.
I'd avoid using placement in stable push
before doing proper performance measurements.
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.
I had some benchmarks implemented in nagisa@7b1b36d but they’re not enough at all to properly test all the cases (e.g. large-sized T
).
Nice! I'd be curious to discuss our conventions a little more here as well. It seems like we may want to both implement the placer on the types directly as well as have methods, but then we've got naming issues etc, etc. Fun bikesheds! |
Yes, I think we should discuss the overall conventions here before going too much farther down that road -- let's be sure to cover it in the next libs meeting if we don't reach a resolution on this thread. |
I'm very much a fly on the wall with this change. I was wondering if there's a precedent for an operator pushing into a container elsewhere? It looks a bit strange to me. |
The collection library conventions were specifically audited before 1.0. How on earth |
Also, the PR name is a bit misleading, |
@aturon when discussing the feature please keep in mind the following point: This is unstable and implementation as it is currently is mostly done for everybody to try the feature and see what works and doesn’t. Notably the “Should &mut Vec implement Placer? Or should it provide a back_place method like LinkedList does instead?” should eventually answer itself through use – if people find
This is not an operator for pushing into containers; This is an operator for directly placing things into certain places that are not necessarily on the stack. Feature-wise the closest equivalent to this is C++’s emplacement, which, one could argue, hasn’t much special syntax. |
From the example for this PR:
Which is effectively the same as C++11 std::vector emplace_back() - maybe "push" isn't the right description? Placement or not, adding a new element to the end of the vector which sounds like a push operation to me. |
Is there any way we can avoid this becoming a de facto deprecation of the |
@bluss It's always better when it works, but there are some theoretical circumstances where it doesn't work, e.g.:
More generally placement won't work if creating the value to be placed requires borrowing something also borrowed by the place (unless of course if none of the borrows is However, I'm not sure whether this will be common enough to warrant keeping |
When emplacement roll-back is expensive and happens often,
Before flipping the coin vector shifts half of its elements to the right to prepare the scratch space for the new element in the position |
Thanks for the PR @apasel422! The libs team discussed this during triage yesterday, and the conclusion was that we probably want to hold off with more in-place implementations if we can. We're slightly uncomfortable that Specifically, especially as the discussion here indicates, we likely need to lay out some conventions for naming, usage, what implements @aturon has showed interest in helping shepherd a conventions RFC in this regard, as well. @apasel422 would you be interested in writing such an RFC? |
@alexcrichton An RFC is totally reasonable for this (I'll start thinking about writing one in the next couple of days), but I am curious as to why #31696 was accepted. |
Yeah we concluded it was probably premature for us to accept #31696 unfortunately :( Thanks though! |
This is a very strange decision, emplacement needs implementation experience and performance measurements first of all, naming scheme is not important at the moment. |
@petrochenkov @nagisa You both make reasonable points about wanting to experiment quickly and not being too cautious here. But, on the other hand, we'd prefer not to rapidly get into a situation where we have suboptimal de facto conventions or inconsistency across (Personally, I think the names that landed for This doesn't need to be heavyweight -- the libs team would just like to see a brief writeup of some basic naming/impl scheme. We can move quickly on it. |
@apasel422 the libs team discussed this recently and our conclusion was that we have some concrete ideas of how to revive this movement of implementations of these traits on collections. Would you still be interested in tackling this? |
Ah and to be more concrete (and to write down the notes), our conclusion was:
|
@alexcrichton Sure. I'll investigate resurrecting this later today. |
Awesome! We're also thinking that we could turn this into a tracking issue with all collections listed so we could keep up with the progress. Maybe you'd like to take a look first though and see where this would all need to be implemented? |
@alexcrichton I don't see how we can get syntax like let mut vec = vec![1, 2, 3];
vec <- 4; instead of let mut vec = vec![1, 2, 3];
&mut vec <- 4; as |
@apasel422 hm we were definitely not aware of that! We can perhaps push forward with |
@apasel422 we could make auto-ref work by temoprarily changing desugaring from Either way we have rust-lang/rfcs#1286. |
Implement placement-in protocol for `Vec` Follow-up of #32366 per comment at #30172 (comment), updating to latest rust, leaving @apasel422 as author and putting myself as committer. I've removed the implementation of `push` in terms of place to make this PR more conservative.
Open questions:
BackPlace
the right name for the struct?&mut Vec
implementPlacer
? Or should it provide aback_place
method likeLinkedList
does instead?CC #30172, @rust-lang/libs, @pnkfelix