perf(edmonds-karp): stop repeating work that is already done - #819
Open
tachsin wants to merge 1 commit into
Open
perf(edmonds-karp): stop repeating work that is already done#819tachsin wants to merge 1 commit into
tachsin wants to merge 1 commit into
Conversation
Three things in the augmentation path are done twice or more: - `augment` calls `flows()` once to derive the cut and again for the value it returns. Building that list walks the whole capacity structure, which for `DenseCapacity` means the entire size-by-size matrix. Build it once and read the cut off it. - `SparseCapacity::flows` clones the whole nested `BTreeMap` before iterating it, allocating a copy of every flow to read every flow. Iterate by reference. - `update_flows` records the nodes the source can still reach in a `BTreeSet<usize>`, cleared and refilled on every augmentation. Nodes here are numbered from zero, so a `Vec<bool>` indexes directly. The set never escapes the private trait. Also `cancel_flow` cloned a path to zip it with itself; `windows(2)` gives the same pairs. No change to the flows, the value, or the cut, all of which the existing tests check exactly, including the two Code Jam ones. On a layered 122-node network, measured against this branch's parent: about 6% off the dense representation and 16% off the sparse one, which does the most copying.
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.
Closes #816.
Builds the flow list once in
augmentand reads the cut off it, iterates the sparse map by reference instead of cloning it, indexes reachability by node number rather than through aBTreeSet, and useswindows(2)for the path pairs incancel_flow.update_flowschanges return type, but it belongs to the privateEdmondsKarpInternaltrait and does not escape the module.Checking
No change to the flows, the value or the cut, all of which the existing tests check exactly, including the two Code Jam ones. 290 tests pass, clippy and rustfmt clean.
Measured against this branch's parent, ten passes with the order of the two binaries swapped on alternate passes:
Measured on Windows 11, Intel Core Ultra 7 265K, 64 GB RAM.
This touches the same file as #820 but not the same lines; I checked they merge in either order.