-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 by Bors] - extract topsort logic to a new method, one pass to detect cycles and … #7727
Conversation
…top sort. reduce mem alloc
Please remove the Changelog and Migration Guide completely for this PR :) It's less distracting, and avoids any automated tools picking them up. |
Factoring this logic into a
Also, I don't understand why you renamed |
Is there a way for us to early return (without catching a panic) as we're processing each SCC? It'd reduce the total memory usage to the maximum number of nodes in each SCC. |
Can you clarify what you're asking? The number of If the question is: "Can we return as soon as we find an SCC with 2+ nodes?" I would ask, which do you prefer? Do you want all cycles reported, as in #7463? Then no. If you (and most users) are OK with maybe having to rebuild over and over to find and fix every cycle, then yes. It's technically possible to avoid allocating a enum StronglyConnectedComponent {
Single(NodeId),
Multiple(Vec<NodeId>),
} But that would have to wait for The underlying algorithm doesn't actually need to allocate these while it's running. Putting the nodes of an SCC together in a |
…ycles => scc_with_cycles
Maybe I missed something, so just made a test suit to verify whether mem alloc reduced. https://github.com/shuoli84/test_petgraph_scc_alloc. From the run, the mem alloc for schedule building reduced by around 30%, with system count 900, main alloc 8395 times, vs the optimized version 5690.
renamed it to sccs_with_cycles. EDIT: format |
Oh, I see now. You prepare the topsorted vector in one allocation. I spoke too soon. Sorry about that! Good catch! // This is allocated once and never needs to reallocate.
let mut rev_top_sorted_nodes = Vec::<NodeId>::with_capacity(graph.node_count());
/* ... */
tarjan_scc.run(graph, |scc| {
/* ... */
// If this graph is acyclic, then we can just reverse this when we're done.
rev_top_sorted_nodes.extend_from_slice(scc);
}); Yeah, the flattening and collect in Will approve once you resolve the CI errors. |
bors r+ |
#7727) …top sort. reduce mem alloc # Objective - Reduce alloc count. - Improve code quality. ## Solution - use `TarjanScc::run` directly, which calls a closure with each scc, in closure, we can detect cycles and flatten nodes
Pull request successfully merged into main. Build succeeded:
|
bevyengine#7727) …top sort. reduce mem alloc # Objective - Reduce alloc count. - Improve code quality. ## Solution - use `TarjanScc::run` directly, which calls a closure with each scc, in closure, we can detect cycles and flatten nodes
…top sort. reduce mem alloc
Objective
Solution
TarjanScc::run
directly, which calls a closure with each scc, in closure, we can detect cycles and flatten nodes