Skip to content

Compute each key only once in position_{min,max,minmax}_by_key - #1136

Merged
phimuemue merged 1 commit into
rust-itertools:masterfrom
ronnodas:position-min-max
Oct 21, 2026
Merged

phimuemue merged 1 commit into
rust-itertools:masterfrom
ronnodas:position-min-max

Conversation

@ronnodas

Copy link
Copy Markdown
Contributor

Right now the methods position_{min,max,minmax}_by_key evaluate the user provided key function each time each item is compared with another, which seems unnecessary, especially in case computing the key is expensive. Note that in any correct implementation, every item must be compared at least once, so each key must be computed at least once.

This does theoretically change the behavior since the key function is allowed to be stateful, in particular if it is non-deterministic.

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.72%. Comparing base (6814180) to head (a593dd5).
⚠️ Report is 223 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1136      +/-   ##
==========================================
- Coverage   94.38%   93.72%   -0.67%     
==========================================
  Files          48       52       +4     
  Lines        6665     6484     -181     
==========================================
- Hits         6291     6077     -214     
- Misses        374      407      +33     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@phimuemue

Copy link
Copy Markdown
Member

Hi @ronnodas, thanks, this looks reasonable.

@phimuemue
phimuemue enabled auto-merge October 21, 2026 14:05
@phimuemue

Copy link
Copy Markdown
Member

And I guess this also opens the door for your idea about making the function FnMut(Self::Item)->K instead of FnMut(&Self::Item)->K, right?

@phimuemue
phimuemue added this pull request to the merge queue Oct 21, 2026
@ronnodas

Copy link
Copy Markdown
Contributor Author

And I guess this also opens the door for your idea about making the function FnMut(Self::Item)->K instead of FnMut(&Self::Item)->K, right?

Yes, I can file a PR for that once this one is merged.

Merged via the queue into rust-itertools:master with commit 7c1bbef Oct 21, 2026
8 of 14 checks passed
@ronnodas
ronnodas deleted the position-min-max branch October 21, 2026 14:18
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.

2 participants