uucore: make Range::merge linear instead of quadratic - #14359
Open
haydonryan wants to merge 1 commit into
Open
Conversation
|
GNU testsuite comparison: |
Contributor
|
any reason why codspeed don't detect the improvements? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Deepseek found another optimization.
This one converts the loop from O(n^2) to O(n) for ranges.
I ran a few options for this PR, including one that was smaller in size by 96 bytes, but this is faster overall.
LLM generated below here:
Range::merge(src/uucore/src/lib/features/ranges.rs) merged overlappingranges with
ranges.remove(j)inside awhileloop. BecauseVec::removeshifts the tail, heavily-overlapping range lists were O(n²). Only caller is
cut(viaRange::from_list).Replaced with a single
Vec::dedup_bypass that extends the bucket'shighon overlap. Output is unchanged — still sorted, disjoint, and adjacent ranges
are not merged. The only subtlety is that
dedup_by(a, b)passesa= newelement,
b= bucket, so the closure must extendb.high(the kept element),not
a.high(the dropped one).Measurement (same harness, release)
Both versions measured in one harness, same input, realistic overlapping ranges:
The O(n) pass is dramatically faster at 30k+ ranges, but real
cutinvocationsuse a handful of fields (argv caps ~30k, and a typical
-flist is 1-100), sothe merge is sub-microsecond, once-at-startup work there. The value of this
change is not a user-visible speedup: it removes the O(n²) blowup (a large
overlapping field list would otherwise hang
cut) and shrinks the binary by256 B.
dedup_bywas also the smallest/fastest of the four O(n) variants A/B'd(baseline, extra-
Vec, in-placeswap,dedup_by).dedup_bywas also the fastest of the four O(n) variants A/B'd (baseline,extra-
Vec, in-placeswap,dedup_by) in every all-overlap case; the inplace variants are not measurably faster and compile to a larger binary.
Verification
cargo fmtclean,cargo clippy --release --bin coreutilsclean (0 warnings)