-
Notifications
You must be signed in to change notification settings - Fork 19
Optimize find_shortest_chains #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seddonym
merged 4 commits into
python-grimp:main
from
Peter554:improve-find-shortest-chains
Aug 11, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9512d05
Extract common chain-finding logic into _find_shortest_chains helper …
Peter554 8c3ae2c
Replace duplicate code in find_illegal_dependencies with _find_shorte…
Peter554 ebcefa4
Optimised find_shortest_chains by leveraging the _find_shortest_chain…
Peter554 d57385e
Update changelog.
Peter554 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,24 +100,55 @@ impl Graph { | |
| downstream_modules.extend_with_descendants(self); | ||
| upstream_modules.extend_with_descendants(self); | ||
| } | ||
| let all_modules = &downstream_modules | &upstream_modules; | ||
|
|
||
| let chains = downstream_modules | ||
| .iter() | ||
| .cartesian_product(upstream_modules.iter()) | ||
| .filter_map(|(downstream_module, upstream_module)| { | ||
| let excluded_modules = | ||
| &all_modules - &FxHashSet::from_iter([*downstream_module, *upstream_module]); | ||
| self.find_shortest_chain_with_excluded_modules_and_imports( | ||
| &(*downstream_module).into(), | ||
| &(*upstream_module).into(), | ||
| &excluded_modules, | ||
| &FxHashMap::default(), | ||
| ) | ||
| .unwrap() | ||
| }) | ||
|
|
||
| let chains = self | ||
| ._find_shortest_chains( | ||
| &downstream_modules, | ||
| &upstream_modules, | ||
| &FxHashSet::from_iter([]), | ||
| )? | ||
| .into_iter() | ||
| .collect(); | ||
|
|
||
| Ok(chains) | ||
| } | ||
|
|
||
| pub(crate) fn _find_shortest_chains( | ||
| &self, | ||
| from_modules: &FxHashSet<ModuleToken>, | ||
| to_modules: &FxHashSet<ModuleToken>, | ||
| excluded_modules: &FxHashSet<ModuleToken>, | ||
| ) -> GrimpResult<Vec<Vec<ModuleToken>>> { | ||
| let mut chains = vec![]; | ||
|
|
||
| // Disallow chains via these imports. | ||
| // We'll add chains to this set as we discover them. | ||
| let mut excluded_imports = FxHashMap::default(); | ||
|
|
||
| loop { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've noticed that in a long-running call we can't exit using CMD-C or equivalent, might be a nice improvement in another PR to listen for that somehow. |
||
| let chain = self.find_shortest_chain_with_excluded_modules_and_imports( | ||
| from_modules, | ||
| to_modules, | ||
| excluded_modules, | ||
| &excluded_imports, | ||
| )?; | ||
|
|
||
| if chain.is_none() { | ||
| break; | ||
| } | ||
| let chain = chain.unwrap(); | ||
|
|
||
| // Exclude this chain from further searching. | ||
| for (importer, imported) in chain.iter().tuple_windows() { | ||
| excluded_imports | ||
| .entry(*importer) | ||
| .or_default() | ||
| .insert(*imported); | ||
| } | ||
|
|
||
| chains.push(chain); | ||
| } | ||
|
|
||
| Ok(chains) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth mentioning that it might lead to slightly different results in some cases? (I agree with you, the results of
find_shortest_chainsis not formally specified, though I'd like us to get to that point.)