Skip to content

Removing recursion from find_mut in treemap #15540

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

Merged
merged 1 commit into from
Jul 9, 2014
Merged

Conversation

Gankra
Copy link
Contributor

@Gankra Gankra commented Jul 9, 2014

Removing recursion from TreeMap implementation, because we don't have TCO. No need to add O(logn) extra stack frames to search in a tree.

I find it curious that find_mut and find basically duplicated the same logic, but in different ways (iterative vs recursive), possibly to maneuvre around mutability rules, but that's a more fundamental issue to deal with elsewhere.

Thanks to acrichto for the magic trick to appease borrowck (another issue to deal with elsewhere).

find_mut(&mut self.root, key)
let mut current: &'a mut Option<Box<TreeNode<K, V>>> = &mut self.root;
loop {
let temp = current; //hack to appease borrowck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The style is to have a space after the //.

@Gankra
Copy link
Contributor Author

Gankra commented Jul 9, 2014

@huonw I believe I've addressed all of your issues

@huonw
Copy link
Member

huonw commented Jul 9, 2014

Cool, looks good. r=me if you squash the commits into one.

@Gankra
Copy link
Contributor Author

Gankra commented Jul 9, 2014

@huonw done

@alexcrichton
Copy link
Member

Thanks @gankro!

@pcwalton
Copy link
Contributor

pcwalton commented Jul 9, 2014

Do we have benchmarks on this?

@Gankra
Copy link
Contributor Author

Gankra commented Jul 9, 2014

@pcwalton not for the mutable variant. I could write some if you want, but I think this change is desirable if only to have uniform code duplication.

bors added a commit that referenced this pull request Jul 9, 2014
Removing recursion from TreeMap implementation, because we don't have TCO. No need to add ```O(logn)``` extra stack frames to search in a tree.

I find it curious that ```find_mut``` and ```find``` basically duplicated the same logic, but in different ways (iterative vs recursive), possibly to maneuvre around mutability rules, but that's a more fundamental issue to deal with elsewhere.

Thanks to acrichto for the magic trick to appease borrowck (another issue to deal with elsewhere).
@bors bors closed this Jul 9, 2014
@bors bors merged commit 03981b5 into rust-lang:master Jul 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants