Repository navigation
Description
I am using the graph to display ~1k vertexes and ~4k edges. I am having a very hard time making the graph redraw rate manageable. I am using multiple vertex decorators (Coulomb, Hook, Pin Center). I have not yet started adding edge decorators.
The problem is 2-fold. The first one is that the Coulomb Algorithm is O(n^2) complexity. The second is (and i am not sure how this works) that the algorithm is running single threaded. These things should be parallelizable. I don't know if and how they can be offloaded to the GPU/multithreaded. I am currently getting around 2 seconds per redraw per vertex on a Samsung A54. It is also causing the app to crash at this size of graph.
I am currently trying to implement a BarnesHutCoulombDecorator. This algorithm runs in O(nlog(n)) at the cost of negligible accuracy (half a pixel). I will be adding it as a pull request once i get the damn thing to work.
The other issue with parallelism might be implemented with isolates (not sure as i am new to flutter), but I have no way of plugging it in with the decorators that i can see. The decorators seem to work per vertex (GraphAlgorithm.compute(Vertex v, Graph graph) ) and i can't find a way to run the method on the whole graph/batch at once per redraw
Is there anything you can help me with? I don't really know if i am using the tool in the way you designed. My goal is to be able to handle 10K vertexes 50K edges at some point.
Finally, I am bashing my head against the lack of docs. I had to read whole classes to understand what some fields do. I would appreciate it if you can point me to anything that describes the workflow of the graph redraw/recalculation process. I think I get it, but i need some confirmation.
Activity
CorvusYe commentedon Oct 31, 2025
You are right. This issue does indeed exist.
Recently, I have also been working on improving performance. We have just managed to increase the renderable level from the 1.x version by over 1k.
During the 2.x upgrade, it was initially planned to use isolate to transfer the computation process into parallel processing. However, it was felt that this might not yield results quickly, so the upgrade was prioritized for the current stage.
There is an option called
perBatchTotal(default to 100) in theOptionssection. Adjusting this setting might have some impact on performance, but it won't resolve the underlying issue.Currently, I only have a rough idea. In
Options.run, the calculation process can be transferred to asynchronous execution, or the compute API can be completely changed.
Unfortunately, there have been other ongoing matters recently that may prevent me from dedicating sufficient time to further enhance performance.
MohammadAlHajj commentedon Nov 3, 2025
Right now, the animation controller runs
Options.runevery frame, and restarts every 10 seconds.If you move to background parallel execution of algorithms, you don't have to wait at all. just keep the algorithm running on all the time, and update whatever it have ready at the next frame tick.
A possible sideeffect: if the previous frame's calculation is not done, will that:
If (1) happens, will that cause memory problems because the queue keeps getting bigger, or will it try executing multiple batches ate the same time and cause the processor to be overwhelmed? I have had a lot of crashes when the graph grew in size.
If (2) happens, will that cause some calculations to not be executed and vertexes to not be recalculated?
CorvusYe commentedon Nov 3, 2025
If (2) happens, there may indeed be some vertexes that cannot be updated.
if (1) happens, can new tasks be pushed into the queue after each frame ends. There is too much backlog of tasks, and updating the view does not have much significance.
I prefer: parallel processing of different batches and serial processing of different frames.
Because the operation of the current frame still depends on the new position of the node in the previous frame, if the old position is continuously used in parallel, there may be some unnecessary computational load.
I'm not sure if I understand the situation you encountered correctly. If the understanding is not correct, further communication can be conducted.
MohammadAlHajj commentedon Nov 3, 2025
Isolates will need a major redesign
I tried Isolates. Isolates need to interact using ReceivePort, and SendPort. SendPort.send is used to send a message, but there are limitations on what the message can hold. One of these rejected Items in the message is "Path" (dart:ui). Edge has a
Path? pathfield in it. Graph has a list of edges => Graph is not passable. I attached the exception stack below.Serialization is not practical:
The other way to do the message passing is by serializing and decentralizing the data on every message call. Deserialization is possible using MapConvertor.convertGraph. Serialization is currently not implemented. This will cause 2 problems. The smaller one is that this process will be very heavy to do especially with large graphs. This makes it counter-intuitive. The large problem is that even if we fixed the Serialization/Deserialization the graph that we have in the
worker isolateis different from the 'main UI isolate`. This will mean that the GraphAlgorithm.compute will not be updating the original vertexes, only the copies.The current implementation forces using GraphAlgorithm.compute
There is no other way of calculating the new force except through GraphAlgorithm.compute, and GraphAlgorithm.compute needs a Vertex and a Graph. This goes back to problem (2)
Solutions:
CorvusYe commentedon Nov 3, 2025
I previously used isolate_manager, and we encountered a similar issue.
If we extract the ID, radius, and position and put them into a static function, then after the calculation is complete, we can use the ID to find the vertex and assign the position to it. Numbers and strings can be used as messages.
I'm not sure if this approach is feasible.
CorvusYe commentedon Nov 3, 2025
However, it is unavoidable that either option might necessitate changes to the way compute methods are called, and the decorators would need to be modified accordingly.
MohammadAlHajj commentedon Nov 3, 2025
This is a quick and dirty way of doing it. This is just psudo code. I honestly hate it:
CorvusYe commentedon Nov 3, 2025
It might be possible to refactor the existing implementation after verifying that using the isolate results in significant performance improvements.
My point is that sometimes focusing too early on elegance while neglecting the core requirements can be counterproductive.
However, if it is confirmed that significant improvements can be achieved, it would indeed be best to start with an elegant and efficient approach from the outset.
MohammadAlHajj commentedon Nov 11, 2025
@CorvusYe I though about this a lot and conversed with a colleague. The current way of calculating is not parallelizable.
The current way is to calculate and move a vertex (X) and move to the next one (Y). That means that vertex Y has to wait for X to be done, and Z will have to wait for Y and so on....
The new flow should be:
If we want to parallelize, we have to remove the dependence of vertexes on each others' position per 1 redraw of the whole graph. We will have to use the position recalculation of the whole graph as our starting point. The decorators concept will have to be changed.
having all the force decorators use the same starting state (the last calculated position of the whole graph) will then allow us to parallelize. A big chunk of how the library works will have to be rewritten.
current Options.run:
CorvusYe commentedon Nov 11, 2025
I completely agree with you and your colleagues.
At present, the limitation of mutual constraint between nodes in this computing method is too great, which brings certain difficulties to parallel computing. Even if these functional decorations are separated, it cannot fundamentally change this constraint relationship.
That's also why I said before:
I just haven't thought about whether this kind of modification also needs to overturn the system of decorators.
For the order of 10k, I have seen a very smooth solution before. Perhaps we can gain some inspiration from its source code.
https://echarts.apache.org/examples/en/editor.html?c=graphgl-npm-dep&gl=1&theme=dark
At present, one of my applications is also limited in performance, and in the recent stage, it can only increase the scale from hundreds to thousands. So I also want to know how to increase the scale from 1k to 10k.
MohammadAlHajj commentedon Nov 26, 2025
Hi. I have some good news. I need your help to verify something. How do we do this? Do you create a test branch here and i merge into it or do you pull from my repo?
CorvusYe commentedon Nov 26, 2025
@MohammadAlHajj
It's an exciting piece of news.
I prefer to directly pull from your code repository and then merge it into the main branch.
This should make it easier for you to develop the project into something even more popular in the future.
MohammadAlHajj commentedon Nov 27, 2025
the solution is only a proof of concept. It is still has technical issues i want to talk to you about. I need advice on how to deal with these issues in flutter. It is not a very friendly language for this kind of problem. Is there a way that we can talk about it in a more direct way?
CorvusYe commentedon Nov 28, 2025
What kind of problem is it? I'll see if I can try to help you.
Regarding communication methods, email or do you use WeChat there ?
oscarshaitan commentedon Dec 16, 2025
Hello any update? from 1k vertex the graph is getting super slow and you can see my worst case in the image
CorvusYe commentedon Dec 17, 2025
@oscarshaitan Unfortunately, it has not been updated yet. Performance remains an urgent issue to be addressed.😭
oscarshaitan commentedon Dec 17, 2025
any particular reason why you drop Flame in previous versions?
CorvusYe commentedon Dec 17, 2025
Initially, I thought that leveraging a game engine might allow for more efficient handling of rendering tasks. However, after experimenting with canvas even though the core algorithm remained largely unchanged, I found that canvas performed better in this context.
Again within the example project, when considering performance from a subjective visual perspective, 200 nodes roughly equate to 1k nodes.
MohammadAlHajj commentedon Dec 18, 2025
@CorvusYe I sent you a mail on CorvusY@foxmail.com. Can we please follow up there as well? I have very promising results, but the dart language is getting in my way too much. I also have a workaround that that doesn't force us to change the redraw logic for now.
CorvusYe commentedon Dec 18, 2025
@MohammadAlHajj I’m sorry, it took several days for me to notice the email.
Regarding the choice of communication tools, I have responded to you via email.
oscarshaitan commentedon Dec 18, 2025
let me know how I can help.
CorvusYe commentedon Dec 18, 2025
@oscarshaitan I just saw that @MohammadAlHajj added a decorator that supports parallel computing. Even with 800 points and over 3,000 edges, it runs smoothly. There should be an update available later, and we’ll see how we can improve it further. Today, we used Zoom; you can download it so we can discuss within the same group later.
oscarshaitan commentedon Dec 18, 2025
ok Thansk Ill send you an email so you can invite me @MohammadAlHajj good job!
MohammadAlHajj commentedon Jan 8, 2026
Hi, this is an update on the parallelization problem.
Firstly, I forgot to update the demo to show parallelization at full speed. do the following :
in force_directed_parallel_demo.dart:
add
..perBatchTotal = (double.maxFinite.toInt())to optionsto try bigger graphs, update the 3
forloops with bigger numbers.Secondly: This is still a proof of concept because the results are NOT mathematically accurate.
Graphs will not reach the same shapes as the series execution. This problem happens because, even though we are applying the same formulas, in series, the graph is updated with every vertex recalculation, while in parallel, all forces of the whole graph are calculated using a snapshot of the graph's position, and applies afterwards at the same time. This can cause forces to be multiples of times more powerful than what they should be and will throw the graph into chaos. You will notice oscillations and orbiting.
You can currently mitigate that in 2 ways.
kin the decorator. One of my tests I lowered hooke's force from k = 0.003 to k =0.00008. Although that worked for me, it is not backed up with any proof and may not work for youThird: ParallelizationDecorator DOES NOT parallelize how its child decorators themselves calculate. It DOES, however, make each child decorator run it's calculation in parallel to OTHER child decorators. It also DOES decouple "drawing frames" from calculations. This will let the app redraw the graph with out waiting for the calculations to finish. In turn, this will make your apps more responsive. It will make moving and zooming the graph way more snappy. It DOES NOT affect other decorators outside it.
Forth: I think the library should evolve as follows:
draw_next_framepositions_changed,vertex_added,scale_changedonly call the decorators that are registered.Most of these propositions WILL break compatibility and will force us to move to a newer major version