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 size_hints for btree iterators #49550

Closed
wants to merge 2 commits into from

Conversation

Phlosioneer
Copy link
Contributor

Continues work on adding size_hint implementations for iterators:
issue #49205

Continues work on adding size_hint implementations for iterators:
issue rust-lang#49205
// from a NodeRef.
(1, None)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

What is the utility of a size hint like this? Would it be better to just not define it at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's useful to have it, because it may be implemented in the future. size_hint is easily forgotten unless it's in the code.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I buy that form of reasoning.

Copy link
Contributor

Choose a reason for hiding this comment

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

In this particular case I buy it because (1, None) in some cases is still more information than the default size_hint of (0, None).

But #49552 contains some size_hint implementations that literally just return (0, None). For those cases I don't think I buy it.

Copy link
Member

Choose a reason for hiding this comment

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

Well in particular, the bit about "because it may be implemented in the future." I'd rather not add speculative impls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BurntSushi would just adding // FIXME(#49205): Add size_hint be better? I just want to make sure it's not forgotten.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ixrec I don't know what you're talking about? None of the implementations I wrote return (0, None), or even this controversial (_, None)... Please comment on that PR if you see any specific issues, maybe you see something I don't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other option is to add a length field like every other BTree iterator. But that isn't a trivial thing, which is what this PR is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just going to move forward with a FIXME. There's no consensus about what to do.

let upper = match (left_upper, right_upper) {
(Some(left), Some(right)) => left.checked_add(right),
(left, None) => left,
(None, right) => right
Copy link
Member

Choose a reason for hiding this comment

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

None as the upper is infinity, so I think this is wrong.

Also, this lift can often be written nicely with ?-on-Option, something like

(||{ left_upper?.checked_add(right_upper?) })()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right! I got it backwards.

I don't think it's worth it to create a closure purely to use ?... it doesn't make it any more readable. IMHO a closure that is immediately called is harder to read & maintain.

Copy link
Member

Choose a reason for hiding this comment

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

Well, it'll soon be do catch { left_upper?.checked_add(right_upper?)? } to avoid the IIFE (that I agree is ugly), but please don't do that until #49371 merges.

Copy link
Contributor Author

@Phlosioneer Phlosioneer Apr 4, 2018

Choose a reason for hiding this comment

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

That's still a lot of ?. But much better. I'll be sure to make that change whenever it merges.

@pietroalbini pietroalbini added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 2, 2018
@BurntSushi BurntSushi added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2018
@rust-lang rust-lang deleted a comment from BubbaSheen Apr 7, 2018
@Phlosioneer
Copy link
Contributor Author

Resolved waiting-on-team by just replacing the controversial code with a FIXME. See #49550 (comment)

@TimNN TimNN added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Apr 17, 2018
@pietroalbini
Copy link
Member

Ping from triage @BurntSushi! This PR needs your review!

@BurntSushi
Copy link
Member

I'm not too sure what else needs to happen here. I'm not sure I agree with adding size hints that aren't actually used.

cc @rust-lang/libs @alexcrichton

@alexcrichton
Copy link
Member

Ah yeah I think it'd be best to hold off on landing this until it's a public interface and tested as well.

@Phlosioneer
Copy link
Contributor Author

This PR has been pretty thoroughly gutted.

I think it's possible to refactor things so that Range etc keep track of how many elements they contain (the range_search method iterates over every single element between them), but that's hard and requires understanding the code better. At that point, these size hints will be much more useful.

Until then, I'm just going to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants