Repository navigation
Conversation
rossbar
left a comment
There was a problem hiding this comment.
I don't think the performance angle is particularly motivating; to me the changes are in the noise and worrying about the special cases where folks have long/complicated node labels doesn't strike me as particularly motivating either.
That said, the changes themselves seem like nice readability improvements! Replacing x in dict with the walrus operator and dict.get is a nice pattern for reusing results that were already computed. Other things like collapsing the nested if s is also nice. I have one question about the changed condition at the top of the loop but other than that I'm in favor of the changes. Thanks @amcandio !
I can check with smaller strings, I believe I saw an improvement even with smaller ones! Just used big ones to make it more evident. If there is improvement for smaller strings I would argue it is not just a special case. It's a common pattern to use strings as keys, such as city names |
dschult
left a comment
There was a problem hiding this comment.
I have two readability concerns. I am hoping that the speedup here comes from storing v_dist rather than using get instead of in+get_item`. Can you see if the impact of the PR is still about the same with the two suggestions below? Thanks
| vu_dist = v_dist + cost | ||
| if cutoff is not None and vu_dist > cutoff: | ||
| continue | ||
| if (u_dist := dist.get(u)) is not None: |
There was a problem hiding this comment.
This is much harder for me to read. In quick tests, it seems that using get is slightly faster if u in dist, but slower if u not in dist. So the overall timing probably depends on the network structure.
The readable difficulty for me is that we lose the "english language" nature of u in dist, and it relies on the trait that get returns None as the default value. I can't find any way to read that one line and based on what it says (rather than logic beyond what it says) tell what it is doing. Contrast that with if u in dist: u_dist = dist[u].
| if (u_dist := dist.get(u)) is not None: | |
| if u in dist: | |
| u_dist = dist[u] |
My question is: does this change affect the timing? Or is the savings elsewhere driving the speedup?
| # We must store *all* predecessors because `pred` was provided by the caller. | ||
| pred[u].append(v) | ||
| elif u not in seen or vu_dist < seen[u]: | ||
| elif (u_seen := seen.get(u)) is None or vu_dist < u_seen: |
There was a problem hiding this comment.
I would prefer the following for readability. It saves the u_seen lookup in the elif below, but restores the english language part of the if-clause (which makes this suggestion very slightly slower than using get(u) when u in seen and very slightly faster when u not in seen)
| elif (u_seen := seen.get(u)) is None or vu_dist < u_seen: | |
| elif u not in seen or vu_dist < (u_seen := seen[u]): |
Again, the question is whether this particular change affects the timing, or is the savings elsewhere in this PR driving the speedup.
|
I don't think these optimizations are worth pursuing anymore, especially since some of the dict access re-use was already addressed in #8218. So I’ll close this one! It would be great if we could merge #8059, since it contains all the benchmarks I relied on during the shortest-path optimization work. |
Optimize
single_source_dijkstra_path_lengthby reducing redundant dictionary lookupsSummary
This PR refactors Dijkstra loops to avoid redundant dictionary lookups when checking distances and visited nodes.
Instead of accessing
dist[u]orseen[u]multiple times it stores the result ofdict.get()in a local variable and reuses it.This potentially saves hash computations and equality checks, especially when:
Benchmark Results
Benchmarks were run using the suite from #8059, both with integer nodes and with artificially long string node labels. Benchmark was extended to call
nx.single_source_dijkstra_path_lengthand to test with long strings following transformation was used:Even though Ratio is higher than 1 in some benchmarks, there is no conclusive increase after looking at variability of measurements.
Integer Nodes
Performance is essentially unchanged or slightly improved (within noise), as expected for cheap hashes and equality checks.
Long String Nodes
When node labels are long strings, hashing is still cached, but avoiding duplicate lookups saves equality comparisons. This leads to more noticeable improvements.