Skip to content
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

Topological sort has a significant performance penalty #170

Open
yyjlincoln opened this issue Mar 7, 2024 · 1 comment
Open

Topological sort has a significant performance penalty #170

yyjlincoln opened this issue Mar 7, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@yyjlincoln
Copy link

yyjlincoln commented Mar 7, 2024

I'm using a Graph of type graph.Graph[Resource, Resource] with a hash function that returns the original pointer to build a directed, acylic resource graph. Due to the nature of current implementation, each graph node contains exactly one edge to its parent resource, all the way to a root node.

With 4043 total resources (so v=4043, e=4042 as the root node does not connect to any parent), running inside a Docker on a M2 Pro mac, a Topological Sort using graph.TopologicalSort takes >1.5s (1.876445292s as a typical value), with a significant memory usage of >200M. Upon profiling the heap with pprof, the following is yield:

Memory snapshot for a single request that requires Topological Sorting:

$ go tool pprof mem.pprof
      flat  flat%   sum%        cum   cum%
  332.92MB 96.06% 96.06%   336.85MB 97.19%  github.com/dominikbraun/graph.TopologicalSort[go.shape.struct { Domain string; Instance int64 },go.shape.struct { Domain string; Instance int64 }]

I thought the excessive memory usage might have been caused by the fact that I'm using a struct and basically no hashing (which is, of course, more memory intensive); so I changed to graph.Graph[*Resource, *Resource]. While the below snapshot did not capture the exact memory usage at the moment, the memory allocation from the start of the program after a few request is:

$ go tool pprof --alloc_space mem.pprof
> top1
      flat  flat%   sum%        cum   cum%
  339.98GB 87.45% 87.45%   342.07GB 87.99%  github.com/dominikbraun/graph.TopologicalSort[go.shape.*go.shape.struct { Domain string; Instance int64 },go.shape.*uint8]

...well exceeding 300GB.

Meanwhile, a naive implementation by keeping track of inEdgesCount for each vertex (using adjacentMap provided by graph) takes <20ms, with negligible memory footprint.

Using graph (as "github.com/dominikbraun/graph") v0.23.0.

@dominikbraun dominikbraun added the bug Something isn't working label Mar 8, 2024
@dominikbraun
Copy link
Owner

Thanks for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants