You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
stable sort has major performance degradation #129
The new ToplogicalSort(), based on its StableTopologicalSort(), has really large performance degradation.
A program that used to take 42s, now takes 220-300s (sometimes more), with the vast majority of the time in StableToplogicalSort(). The current usage is the OSS wolfictl, but I will try to create a reproduction that is focused just on this.
The flame graphs below are from running that with the exact same inputs. The only difference is if I use v0.21.0 or v0.22.0
The first graph is calling StableToplogicalSort() with v0.22.0:
The second is calling TopologicalSort() with v0.22.0 (unchanged, which isn't surprising, as it just wraps StableTopologicalSort():
The third is calling TopologocialSort() with v0.21.0:
In the last, the sort doesn't even take enough time to be bothered into the list.
It might not be the sort itself, as other things might have changed between v0.21.0 and v0.22.0 🤷♂️
In the meantime, it should be easy to replicate with a graph that is [string, string], create a graph with maybe 600 nodes and a few edges among them, make sure it isn't cyclical, and then call TopologicalSort(). But I will try to recreate it.
Thank you for filing this issue. I did some research and decided to proceed as follows:
I will create a patch release that reverts TopologicalSort to the old implementation so that it remains unaffected by the performance issues of StableTopolocialSort.
Then I'm going to take a look at the implementation of StableTopologicalSort and its performance penalties. I'm pretty sure the frequent calls to sort.Slice are the problem, and I will have to dig into its performance characteristics and time complexity first. Maybe there is a more time-efficient solution.
I've created a program that will create a disconnected directed graph consisting of simple vertex chains and run StableTopologicalSort on it.
The number of vertices and the chain length can be set using numberOfVertices and lengthOfChains. Note that numberOfVertices has to be a multiple of lengthOfChains. Maybe this is enough for testing the performance.
Once this is fixed, I'll release another patch release containing the optimized implementation.
Even with a 500-node graph, it still is giving only 250ms to sort the graph. How strange that mine give orders of magnitude more. And I was careful here to profile only starting before the sort, not before the creation.
I wonder if it could have to do with the node type? I will try that next.
I modified it to give different string sizes, and different chain sizes. Here are some interesting outputs.
number of vertices: 500
length of chains: 125
string 10 sort time was 1.972775917s
string 20 sort time was 1.175261542s
int sort time was 278.591834ms
number of vertices: 500
length of chains: 25
string 10 sort time was 1.518682292s
string 20 sort time was 1.608444458s
int sort time was 1.466619s
number of vertices: 500
length of chains: 25
string 10 sort time was 1.618567583s
string 20 sort time was 2.099522875s
int sort time was 1.544102917s
number of vertices: 500
length of chains: 10
string 10 sort time was 1.839373541s
string 20 sort time was 1.667635875s
int sort time was 3.910998084s
We can try and get that into a table, but basically:
long chains provide better performance that shorter chains in int
long vs short seems a bit similar for string
No idea what is going on. I can try and convert this into a general testing regimen program.
FWIW, I tried replacing the less argument with a sort.Interface arg so I could call sort.Sort() instead of sort.Slice(). It seems to shave off about 15%, not insignificant, but not a ton either.
Here is part of the updated dag.go, in case it is helpful.
instead of sort.Slice() in the 2 places is it called.
These all seem to help. It isn't too bad getting it from 208s to 173-179s, and then down to 158s. For a true performance test, I would need to run each one hundreds of times. I hope this helps.
@deitch Sorry for the late feedback, I've been busy in my new job the last couple of days. I've re-opened the issue because I'd like to keep track of the performance and maybe go for a ideal or close-to-ideal implementation using a binary heap for the internal queue, as stated in #131.
Activity
deitch commentedon May 25, 2023
In the meantime, it should be easy to replicate with a graph that is
[string, string], create a graph with maybe 600 nodes and a few edges among them, make sure it isn't cyclical, and then callTopologicalSort(). But I will try to recreate it.dominikbraun commentedon May 27, 2023
Thank you for filing this issue. I did some research and decided to proceed as follows:
I will create a patch release that reverts
TopologicalSortto the old implementation so that it remains unaffected by the performance issues ofStableTopolocialSort.Then I'm going to take a look at the implementation of
StableTopologicalSortand its performance penalties. I'm pretty sure the frequent calls tosort.Sliceare the problem, and I will have to dig into its performance characteristics and time complexity first. Maybe there is a more time-efficient solution.I've created a program that will create a disconnected directed graph consisting of simple vertex chains and run
StableTopologicalSorton it.The number of vertices and the chain length can be set using
numberOfVerticesandlengthOfChains. Note thatnumberOfVerticeshas to be a multiple oflengthOfChains. Maybe this is enough for testing the performance.Once this is fixed, I'll release another patch release containing the optimized implementation.
deitch commentedon May 28, 2023
I created a slight variant that has a much larger graph, and includes generating a profile file:
Even with a 500-node graph, it still is giving only 250ms to sort the graph. How strange that mine give orders of magnitude more. And I was careful here to profile only starting before the sort, not before the creation.
I wonder if it could have to do with the node type? I will try that next.
deitch commentedon May 28, 2023
FYI, I tried adding
graph.Acyclic()andgraph.PreventCycles(), didn't have a material impactdeitch commentedon May 28, 2023
I modified it to give different string sizes, and different chain sizes. Here are some interesting outputs.
We can try and get that into a table, but basically:
No idea what is going on. I can try and convert this into a general testing regimen program.
here is the basic one I am using:
deitch commentedon May 29, 2023
FWIW, I tried replacing the
lessargument with asort.Interfacearg so I could callsort.Sort()instead ofsort.Slice(). It seems to shave off about 15%, not insignificant, but not a ton either.Here is part of the updated
dag.go, in case it is helpful.You can replace
sort.Stable()withsort.Sort()for the other implementation.In case the updated graphs are helpful, here they are:
sort.Stable:sort.Sort:sort.Slice(current implementation):Finally, I decided to try the experimental new generic sorting package at https://pkg.go.dev/golang.org/x/exp/slices#SortFunc
The signatures for the last one aren't changed at all, just the content of
StableToplogicalSort():instead of
sort.Slice()in the 2 places is it called.These all seem to help. It isn't too bad getting it from 208s to 173-179s, and then down to 158s. For a true performance test, I would need to run each one hundreds of times. I hope this helps.
dominikbraun commentedon Jun 5, 2023
graph v0.22.1 reverts the implementation of
TopologicalSort.deitch commentedon Jun 5, 2023
Do we want to pursue any of those
Sortalternatives?dominikbraun commentedon Jun 6, 2023
@deitch Sorry for the late feedback, I've been busy in my new job the last couple of days. I've re-opened the issue because I'd like to keep track of the performance and maybe go for a ideal or close-to-ideal implementation using a binary heap for the internal queue, as stated in #131.
deitch commentedon Jun 6, 2023
Congratulations. That's a good reason. Share your LinkedIn profile here for a minute (can delete later), and I'll connect.
dominikbraun commentedon Jun 6, 2023
@deitch https://www.linkedin.com/in/dominikbraun-io/
deitch commentedon Jun 6, 2023
Connection sent.