-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Prevent collections from unnecessarily finalizing elems #54
Conversation
Collection types such as Vec<T> have drop methods which are only necessary if T requires dropping. This can be managed partially by implementing `NoFinalize` on `Vec<T>`: unsafe impl<T: NoFinalize> NoFinalize for Vec<T> {} However, this will return true for needs_finalize if `T: !Drop` but also doesn't implement `NoFinalize`. To get around this, we introduce a trait which is expected only to be used in the standard library called `OnlyFinalizeComponents`. Implementing this trait on a type indicates to the collector that it can ignore that type's drop method, but it must still recurse through the type's components to see if any of them need dropping.
Why would it only be used in the standard library? |
That sentence is a bit misleading. It doesn't have to be, but I'm not sure I can really see a use-case outside of the standard library. Unless of course the user builds their own collection types. In essence, I'm worried that all these traits ( |
Ah, I see. So this PR is, presumably, a relatively small optimisation overall? |
This PR has quite a big ripple effect because almost everything is built out of the standard library types that are now optimised. However, in 99% of cases I don't see the user really needing to know that these traits exist. I guess you could think of this as Boehm's "wizards only interface". |
Silly question: why does |
It doesn't. Maybe this sentence is where the confusion comes from?
Let me have a go at rewording it: If we have a If
So this PR addresses the "gap" here, by allowing |
Mea culpa! I meant "why doesn't !Drop automatically imply NoFinalize?" |
Because AFAIK there is no way to implement a trait based on the non-existence of another. In other words, you can't do this:
Also, it becomes even more tricky because |
Could our compiler hack thingy decide that " |
It does in most cases. If it can't find a This doesn't work when you have types like |
I think I get it! bors r+ |
Build succeeded: |
Collection types such as Vec have drop methods which are
only necessary if T requires dropping. This can be managed partially by
implementing
NoFinalize
onVec<T>
:However, this will return true for needs_finalize if
T: !Drop
but alsodoesn't implement
NoFinalize
.To get around this, we introduce a trait which is expected only to be
used in the standard library called
OnlyFinalizeComponents
.Implementing this trait on a type indicates to the collector that it can
ignore that type's drop method, but it must still recurse through the
type's components to see if any of them need dropping.