Commit b713497
committed
Optimize graph traversal by using a more appropriate algorithm
The previous implementation could visit nodes multiple times when a
graph had diamond like patterns. The new implementation uses [Kahn's
algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm)
which ensures that we only ever visit nodes once.
I tested this on firefox-main with `./mach taskgraph full -J -p
taskcluster/test/params/mc-onpush.yml`. Before this change, with a
profiler attached, it would take ~3mn55. After this change, we're down
to ~2mn30. Without profiling we go from 22s to 17s.
I added a `links_and_reverse_links_dict` helper here since computing
them both at the same time is faster than iterating over the graph
twice. In my testing the function was called 11 times, 10 of which were
on the exact same graph. Because that function is very expensive to
call, I chose to cache it.
The caching itself resulted in ~5-6s of time save when profiled on the
full graph for firefox and ~1s when not profiled.
Hyperfine output, full firefox graph, no profiler attached:
```
Benchmark 1: baseline
Time (mean ± σ): 22.565 s ± 0.260 s [User: 53.205 s, System: 5.034 s]
Range (min … max): 22.255 s … 23.043 s 10 runs
Benchmark 2: with-patch
Time (mean ± σ): 19.032 s ± 0.110 s [User: 49.556 s, System: 4.829 s]
Range (min … max): 18.856 s … 19.195 s 10 runs
Benchmark 3: with-patch-cache
Time (mean ± σ): 17.800 s ± 0.250 s [User: 48.605 s, System: 4.893 s]
Range (min … max): 17.281 s … 18.159 s 10 runs
Summary
with-patch-cache ran
1.07 ± 0.02 times faster than with-patch
1.27 ± 0.02 times faster than baseline
```1 parent 7f75010 commit b713497
1 file changed
+33
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
79 | | - | |
80 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
81 | 90 | | |
82 | 91 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
92 | 98 | | |
93 | 99 | | |
94 | 100 | | |
| |||
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
110 | 131 | | |
111 | 132 | | |
112 | 133 | | |
| |||
0 commit comments