Skip to content
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

add faster safe rust variant that does not drop needlessly #52

Merged
merged 2 commits into from May 18, 2018

Conversation

@vbarrielle
Copy link
Contributor

commented May 18, 2018

In the split and merge parts, calls to drop were inserted to delete
options that had been taken, ie were already none. Thus the calls to
drop would always conclude they didn't need to drop, but this would waste
computational resources. Using the swap and forget pattern, we can
safely inform the compiler that the taken node does not actually need to
be freed.

This significantly decreased the runtime on my machine (from ~0.5s for the idiomatic version down to ~0.3s for the new version).

add faster safe rust variant that does not drop needlessly
In the split and merge parts, calls to drop were inserted to delete
options that had been taken, ie were already none. Thus the calls to
drop would always conclude they didn't need to drop, but this would waste
computational resources. Using the swap and forget pattern, we can
safely inform the compiler that the taken node does not actually need to
be freed.
if let Some(mut orig_node) = orig {
if orig_node.x < value {
let mut split_pair = split_binary(orig_node.right.take(), value);
::std::mem::swap(&mut orig_node.right, &mut split_pair.0);

This comment has been minimized.

Copy link
@frol

frol May 18, 2018

Owner

::std:: can be dropped here, right?

This comment has been minimized.

Copy link
@vbarrielle

vbarrielle May 18, 2018

Author Contributor

Right, I've updated to fix this.

@frol

This comment has been minimized.

Copy link
Owner

commented May 18, 2018

@vbarrielle Nice trick! Thank you! This dropped the execution time from 0.37s to 0.24s while still keeping the safety guarantees!

@frol frol merged commit c5e9205 into frol:master May 18, 2018

frol added a commit that referenced this pull request May 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.