Skip to content

Shortcuts for min/max on ordinary BTreeMap/BTreeSet iterators - #73627

Merged
bors merged 1 commit into
rust-lang:masterfrom
ssomers:btree_iter_min_max
Jun 27, 2020
Merged

Shortcuts for min/max on ordinary BTreeMap/BTreeSet iterators#73627
bors merged 1 commit into
rust-lang:masterfrom
ssomers:btree_iter_min_max

Conversation

@ssomers

@ssomersssomers commented Jun 22, 2020

Copy link
Copy Markdown
Contributor

Closes#59947: a performance tweak that might benefit some. Optimizes min and max on all btree double-ended iterators that do not drop, i.e. the iterators created by:

  • BTreeMap::iter
  • BTreeMap::iter_mut
  • BTreeMap::keys and BTreeSet::iter
  • BTreeMap::range and BTreeSet::range
  • BTreeMap::range_mut

Also in these (currently) single-ended iterators, but obviously for min only:

  • BTreeSet::difference
  • BTreeSet::intersection
  • BTreeSet::symmetric_difference
  • BTreeSet::union

Did not do this in iterators created by into_iter to preserve drop order, as outlined in #62316.

Did not do this in iterators created by drain_filter, possibly to preserve drop order, possibly to preserve predicate invocation, mostly to not have to think about it too hard (I guess maybe it wouldn't be a change for min, which is the only shortcut possible in this single-ended iterator).

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @Mark-Simulacrum

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 22, 2020
Comment threadsrc/liballoc/tests/btree/map.rs Outdated
@Mark-Simulacrum

Copy link
Copy Markdown
Member

Could you provide a summary of the affected impls and which ones are left out of scope?

@ssomers

ssomers commented Jun 26, 2020

Copy link
Copy Markdown
ContributorAuthor

I couldn't come up with a reasonable reason to leave some of them out, so added some more. Also straightened out the test code.

@Mark-Simulacrum

Copy link
Copy Markdown
Member

Okay, that set of iterators does look correct to me. However, I think the implementation here is a bit wrong if we want to avoid a behavior change. In particular, this makes min() and max() not consume the iterator. I personally think that's fine, but I would like to check in with @rust-lang/libs -- do we have guarantees about whether methods like min/max exhaust iterators today (in cases where they can be implemented more efficiently)?

One downside (or upside, I guess) of the current implementation is that you can call min() multiple times and it'll keep returning "the next minimum" which is both nice but also perhaps not what the user expected.

@sfackler

Copy link
Copy Markdown
Member

In particular, this makes min() and max() not consume the iterator.

For a generic iterator, we do need min() and max() to advance past every element of the iterator, but since these iterators are specific ones where iteration does not have a side effect the change isn't observable.

min() and max() consume the iterator by value, so you can't call them multiple times.

@Mark-Simulacrum

Copy link
Copy Markdown
Member

Ah for some reason I thought by_ref() let you magically have ownership but that doesn't make any sense now that I think about it some more. Yes, this seems fine then.

@bors r+

@bors

bors commented Jun 26, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit 42062a5 has been approved by Mark-Simulacrum

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2020
Manishearth added a commit to Manishearth/rust that referenced this pull request Jun 26, 2020
…Simulacrum
Shortcuts for min/max on double-ended BTreeMap/BTreeSet iterators
Closesrust-lang#59947: a performance tweak that might benefit some. Optimizes `min` and `max ` on all btree double-ended iterators that do not drop, i.e. the iterators created by:
- `BTreeMap::iter`
- `BTreeMap::iter_mut`
- `BTreeMap::keys` and `BTreeSet::iter`
- `BTreeMap::range` and `BTreeSet::range`
- `BTreeMap::range_mut`
Also in these (currently) single-ended iterators, but obviously for `min` only:
- `BTreeSet::difference`
- `BTreeSet::intersection`
- `BTreeSet::symmetric_difference`
- `BTreeSet::union`
Did not do this in iterators created by `into_iter` to preserve drop order, as outlined in rust-lang#62316.
Did not do this in iterators created by `drain_filter`, possibly to preserve drop order, possibly to preserve predicate invocation, mostly to not have to think about it too hard (I guess maybe it wouldn't be a change for `min`, which is the only shortcut possible in this single-ended iterator).
Manishearth added a commit to Manishearth/rust that referenced this pull request Jun 26, 2020
…Simulacrum
Shortcuts for min/max on double-ended BTreeMap/BTreeSet iterators
Closesrust-lang#59947: a performance tweak that might benefit some. Optimizes `min` and `max ` on all btree double-ended iterators that do not drop, i.e. the iterators created by:
- `BTreeMap::iter`
- `BTreeMap::iter_mut`
- `BTreeMap::keys` and `BTreeSet::iter`
- `BTreeMap::range` and `BTreeSet::range`
- `BTreeMap::range_mut`
Also in these (currently) single-ended iterators, but obviously for `min` only:
- `BTreeSet::difference`
- `BTreeSet::intersection`
- `BTreeSet::symmetric_difference`
- `BTreeSet::union`
Did not do this in iterators created by `into_iter` to preserve drop order, as outlined in rust-lang#62316.
Did not do this in iterators created by `drain_filter`, possibly to preserve drop order, possibly to preserve predicate invocation, mostly to not have to think about it too hard (I guess maybe it wouldn't be a change for `min`, which is the only shortcut possible in this single-ended iterator).
Manishearth added a commit to Manishearth/rust that referenced this pull request Jun 26, 2020
…Simulacrum
Shortcuts for min/max on double-ended BTreeMap/BTreeSet iterators
Closesrust-lang#59947: a performance tweak that might benefit some. Optimizes `min` and `max ` on all btree double-ended iterators that do not drop, i.e. the iterators created by:
- `BTreeMap::iter`
- `BTreeMap::iter_mut`
- `BTreeMap::keys` and `BTreeSet::iter`
- `BTreeMap::range` and `BTreeSet::range`
- `BTreeMap::range_mut`
Also in these (currently) single-ended iterators, but obviously for `min` only:
- `BTreeSet::difference`
- `BTreeSet::intersection`
- `BTreeSet::symmetric_difference`
- `BTreeSet::union`
Did not do this in iterators created by `into_iter` to preserve drop order, as outlined in rust-lang#62316.
Did not do this in iterators created by `drain_filter`, possibly to preserve drop order, possibly to preserve predicate invocation, mostly to not have to think about it too hard (I guess maybe it wouldn't be a change for `min`, which is the only shortcut possible in this single-ended iterator).
This was referenced Jun 26, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 27, 2020
…arth
Rollup of 12 pull requests
Successful merges:
- rust-lang#72771 (Warn if linking to a private item)
- rust-lang#72937 (Fortanix SGX target libunwind build process changes)
- rust-lang#73485 (Perform obligation deduplication to avoid buggy `ExistentialMismatch`)
- rust-lang#73529 (Add liballoc impl SpecFromElem for i8)
- rust-lang#73579 (add missing doc links)
- rust-lang#73627 (Shortcuts for min/max on double-ended BTreeMap/BTreeSet iterators)
- rust-lang#73691 (Bootstrap: detect Windows based on sys.platform)
- rust-lang#73694 (Document the Self keyword)
- rust-lang#73718 (Document the super keyword)
- rust-lang#73728 (Document some invariants correctly/more)
- rust-lang#73738 (Remove irrelevant comment)
- rust-lang#73765 (Remove blank line)
Failed merges:
r? @ghost
@bors
bors merged commit dfbba65 into rust-lang:masterJun 27, 2020
@ssomers
ssomers deleted the btree_iter_min_max branch June 27, 2020 12:40
@ssomersssomers changed the title Shortcuts for min/max on double-ended BTreeMap/BTreeSet iteratorsShortcuts for min/max on ordinary BTreeMap/BTreeSet iteratorsJun 27, 2020
@RalfJung

Copy link
Copy Markdown
Member

I think this PR caused a regression: #73915

@cuvipercuviper added this to the 1.46 milestone May 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Max & Min on BTreeMap are unexpectedly slow

7 participants

@ssomers@rust-highfive@Mark-Simulacrum@sfackler@bors@RalfJung@cuviper