Skip to content

perf: stop copying containers that are only walked - #821

Merged
samueltardieu merged 1 commit into
evenfurther:mainfrom
tachsin:perf/redundant-copies
Sep 10, 2026
Merged

perf: stop copying containers that are only walked#821
samueltardieu merged 1 commit into
evenfurther:mainfrom
tachsin:perf/redundant-copies

Conversation

@tachsin

@tachsin tachsin commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Closes #818.

Borrows the group in ConnectedComponents::components rather than cloning it whole, and uses a Vec in kuhn_munkres where an FxIndexSet was only being appended to and iterated.

Checking

290 tests pass, clippy and rustfmt clean.

Measured against this branch's parent, four passes with the order of the two binaries swapped on alternate passes:

  • components, 20 000 groups of 8: consistently faster in all four passes, between 4% and 13%, so about 10%
  • kuhn_munkres, 250x250: the passes disagree on sign, so I am claiming nothing for it — it is a simplification, not a speed-up
  • separate_components, untouched by this change, measures unchanged, which is the control

Happy to drop the kuhn_munkres half if you would rather this PR only carried the part with a number behind it.

Measured on Windows 11, Intel Core Ultra 7 265K, 64 GB RAM.

`ConnectedComponents::components` clones each group, in full, in order to
iterate it by value and insert the elements into a set. The impl already
requires `&It: IntoIterator`, so the group can be borrowed and only the
elements that are kept get cloned. About 8% off 20 000 groups of 8.

`kuhn_munkres` holds the x nodes of the alternating path in an `FxIndexSet`,
but each root adds a given node at most once and the set is only ever cleared,
appended to and iterated: nothing ever looks a node up. A `Vec` does the same
without hashing. This one is a simplification rather than a speed-up; it does
not measure as a change on a 250x250 assignment.
@samueltardieu
samueltardieu added this pull request to the merge queue Sep 10, 2026
@samueltardieu

Copy link
Copy Markdown
Member

That's fine as-is

Merged via the queue into evenfurther:main with commit 4bca39b Sep 10, 2026
12 checks passed
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.

components clones every group, and kuhn_munkres hashes a list it never looks up

2 participants