Avoid unnecessary allocation in delete-miss #28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This Commit
Uses
bangsafe
'sDeleteResult
construct to avoid unnecessaryRc::new
ing when trying to delete a node that isn't there.Why?
Previously,
Child::Delete
would callRc::new
on the result ofn.delete(k)
regardless of what happened. This means that, ifn.delete(k)
did nothing, we'd allocate a newRc
instead of cloning an existing one. See this comment for more details.Benchmarks
Ignoring
find
,insert
, andfind-miss
since those codepaths weren't touched, we see:which is expected.
delete
is now doing more work because it's tracking whether or not the value was found. So when the value is found, we're doing extra work for "nothing"*. However, indelete-miss
, we now avoid unnecessary allocations of newRc
s so there are huge improvements.* I say this is for "nothing" but I don't really mean it. Realistically,
Tree::delete
should return the value of the deleted node andDeleteResult
is a reasonable place to store that because the caller needs both the new tree and theOption<Rc<V>>
. Because of this, I'm okay with the hit todelete
and a future PR should add the deleted value to the return value ofdelete
.