Repository navigation
Optimize _build_paths_from_predecessors stack logic for 30-40% speedup - #8460
Conversation
|
@dschult @amcandio Thanks for the guidance! I have pushed the final version which incorporates all the feedback:
I think this covers everything we discussed. Let me know if there's anything else, otherwise I'd love to see this merged! |
There was a problem hiding this comment.
I agree with @amcandio that the for/else construction with an if containing the entire body of the loop makes it confusing whether the else with for the for or the if. I see two possible approaches.
- change the
ifand add a comment - rewrite the loop as a while to check for no more preds left.
First approach:
for predecessor in preds:
if predecessor in seen:
continue
seen.add(predecessor)
path.append(predecessor)
stack.append([predecessor, iter(pred[predecessor])])
break
else: # no preds left!
stack.pop()
path.pop()
seen.remove(node)Second approach:
predecessor = next(preds, None)
while predecessor in seen:
predecessor = next(preds, None)
if predecessor is None: # no preds left!
stack.pop()
path.pop()
seen.remove(node)
else:
seen.add(predecessor)
path.append(predecessor)
stack.append([predecessor, iter(pred[predecessor])])Thoughts? Is either better than what's there? Readability is hard to measure. Pick what you think works best. (including leaving it as is)
|
I think Approach 1 is better. It definitely clears up the readability confusion, so it's obvious where that I also prefer it because it keeps the standard I will push the update with the changes you suggested after confirmation! Final code for confirmation: while stack:
node, preds = stack[-1]
if node in sources:
yield path[::-1]
for predecessor in preds:
if predecessor in seen:
continue
seen.add(predecessor)
path.append(predecessor)
stack.append([predecessor, iter(pred[predecessor])])
break
else: # no preds left!
stack.pop()
path.pop()
seen.remove(node) |
|
I'm inclined to close this per the agentic PR policy but I don't want the review to go to waste - @amcandio is this worth benchmarking to push forward IYO? |
|
Hi @rossbar! Just to clarify, this wasn't an automated submission. still i am curious to know what made you to think that it was, cause i found this problem and fixing it gave a good amount of speed up, and i showed the data also(using the table and graph). during which amcandio and dschult also helped me to refine it. |
Yeah, I think we should be able to merge this one. @DeepBuha06 can you add benchmarks? See other benchmarks examples using asv. |
|
okay, I will add that. |
|
@amcandio I've added the benchmark class i had used List in place of collections.deque before also, but due to which it started to use memory a lot and made time worse. so i changed it to collections.deque with maxlen = 0 to calculate every path but throw it away immediately to save RAM and to find the accurate time. |
amcandio
left a comment
There was a problem hiding this comment.
Can you include the output of running asv?
|
@amcandio I've updated the benchmark code to use the clearer for loop structure as requested. Here is the output from running |
|
You have to compare it against a commit before the change! See asv docs |
|
@amcandio 1.) for main branch(old code)2.) for this branch(new/optimized code) |
|
It is possible to compare two commits using asv. Please check the docs. |
rossbar
left a comment
There was a problem hiding this comment.
I'd still be curious to see if this can be refactored to use the dict as a queue instead, but let's not let perfect be the enemy of good!
I'm +1 for the proposed changes on the basis that I find it easier to reason about the proposed queue construction than the previous one (subjective, I know!)
|
FWIW you can run the added benchmarks by checking out this branch locally, then:
Once the benchmark is done, a more direct comparison of results can be generated: |
_build_paths_from_predecessors stack logic for 30-40% speedup
Description
This PR optimizes the
_build_paths_from_predecessorsfunction innetworkx/algorithms/shortest_paths/generic.py.The current implementation manages the stack manually using list indices (
stack[top][1] = i + 1) and an explicittoppointer, which is verbose and adds overhead. I have refactored this to use a standard iterator-based stack approach (stack = [[node, iter(children)]]).This change improves performance significantly for large graphs with many paths while maintaining identical behavior.
Fixes #8459
Improvements
toppointer manipulation.Benchmark Results
I benchmarked both implementations using$N=2$ to $N=16$ . The new implementation shows significant time savings as the number of paths grows.
nx.grid_2d_graph(N, N)fromTest Environment: Grid Graph ($N \times N$ ), finding all shortest paths from
(0,0)to(N-1, N-1).Total Time Saved (16x16): ~300 seconds (5 minutes).
Performance Graph
Implementation Details
Old Approach:
New Approach:
Verification
pytest networkx/algorithms/shortest_paths/tests/test_generic.py- PASSED