Skip to content

Graph redraw is very slow #29

Open

Description

@MohammadAlHajj

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

  1. CorvusYe commented on Oct 31, 2025

    @CorvusYe
    Member

    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 the Options section. 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.

  2. MohammadAlHajj commented on Nov 3, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    Right now, the animation controller runsOptions.run every frame, and restarts every 10 seconds.

    • There is no check if the last run is completed or not before the next execution.
    • Each run is responsible for a certain batch size (default of 100). By design, this forces a fixed rate of 3k vertexes at 30FPS

    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:

    1. queue the new frame's calculations into "next batch queue"
    2. or stop old calculations and start the new ones.

    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?

  3. CorvusYe commented on Nov 3, 2025

    @CorvusYe
    Member

    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.

  4. MohammadAlHajj commented on Nov 3, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    Isolates will need a major redesign

    1. Graphs are not passable between isolates:
      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? path field in it. Graph has a list of edges => Graph is not passable. I attached the exception stack below.
    ======== Exception caught by animation library =====================================================
    The following ArgumentError was thrown while notifying listeners for AnimationController:
    Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:ui' Class: _NativePath@17065589 (see restrictions listed at `SendPort.send()` documentation for more information)
     <- path in Instance of 'Edge' (from package:flutter_graph_view/model//edge.dart)
     <- _List len:4 (from dart:core)
     <- _Set len:3 (from dart:_compact_hash)
     <- Instance of 'Vertex<dynamic>' (from package:flutter_graph_view/model/vertex.dart)
     <- _List len:64 (from dart:core)
     <- _Set len:39 (from dart:_compact_hash)
     <- Instance of 'Vertex<dynamic>' (from package:flutter_graph_view/model/vertex.dart)
     <- _List len:64 (from dart:core)
     <- _Set len:56 (from dart:_compact_hash)
     <- Instance of 'Vertex<dynamic>' (from package:flutter_graph_view/model/vertex.dart)
     <- end in Instance of 'Edge' (from package:flutter_graph_view/model//edge.dart)
     <- _List len:3 (from dart:core)
     <- Instance(length:1) of '_GrowableList' (from dart:core)
     <- _List len:8192 (from dart:core)
     <- _Map len:3361 (from dart:_compact_hash)
     <- edgesBetweenHash in Instance of 'Graph<dynamic>' (from package:flutter_graph_view/model/graph.dart)
     <- graph in Instance of '_IsolateInitMessage' (from package:remind_me_again/core/ui/flutter_graph_view/graph_algorithm/isolate_force_algorithm.dart)
    
    When the exception was thrown, this was the stack: 
    #0      _SendPort._sendInternal (dart:isolate-patch/isolate_patch.dart:259:43)
    #1      _SendPort.send (dart:isolate-patch/isolate_patch.dart:240:5)
    #2      IsolateForceAlgorithm.compute (package:remind_me_again/core/ui/flutter_graph_view/graph_algorithm/isolate_force_algorithm.dart:181:31)
    #3      GraphAlgorithm.compute (package:flutter_graph_view/core/graph_algorithm.dart:152:19)
    #4      RandomOrPersistenceAlgorithm.compute (package:flutter_graph_view/core/algorithm/random_or_persistence_algorithm.dart:24:11)
    #5      Options.run (package:flutter_graph_view/core/options.dart:189:24)
    #6      _GraphComponentCanvasCustomState.run (package:remind_me_again/core/ui/flutter_graph_view/CraphComponentCanvasCustom.dart:55:13)
    #7      AnimationLocalListenersMixin.notifyListeners (package:flutter/src/animation/listener_helpers.dart:160:19)
    #8      AnimationController._tick (package:flutter/src/animation/animation_controller.dart:957:5)
    #9      Ticker._tick (package:flutter/src/scheduler/ticker.dart:269:12)
    #10     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1434:15)
    #11     SchedulerBinding.handleBeginFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:1265:11)
    #12     _LinkedHashMapMixin.forEach (dart:_compact_hash:764:13)
    #13     SchedulerBinding.handleBeginFrame (package:flutter/src/scheduler/binding.dart:1263:17)
    #14     SchedulerBinding._handleBeginFrame (package:flutter/src/scheduler/binding.dart:1179:5)
    #15     _invoke1 (dart:ui/hooks.dart:346:13)
    #16     PlatformDispatcher._beginFrame (dart:ui/platform_dispatcher.dart:426:5)
    #17     _beginFrame (dart:ui/hooks.dart:291:31)
    The AnimationController notifying listeners was: AnimationController#dc8cb(▶ 0.024)
    ====================================================================================================
    
    1. 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 isolate is different from the 'main UI isolate`. This will mean that the GraphAlgorithm.compute will not be updating the original vertexes, only the copies.

    2. 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:

    1. Use something other than Isolates. I don't know if isolate_agents is useful. flutter docs reference it.
    2. add a GraphAlgorithm static function that does the calculations and pass the required data as a Map of primitives, and force GraphAlgorithm.compute to use it to do the actual computation. We need this to force the same calculation from both functions.
  5. CorvusYe commented on Nov 3, 2025

    @CorvusYe
    Member

    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.

  6. CorvusYe commented on Nov 3, 2025

    @CorvusYe
    Member

    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.

  7. MohammadAlHajj commented on Nov 3, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    This is a quick and dirty way of doing it. This is just psudo code. I honestly hate it:

    abstract class GraphAlgorithm {
    
      /// ......
    
      /// Position setter.
      ///
      /// 对节点进行定位设值
      @mustCallSuper
      void compute(Vertex v, Graph graph) {
        if (decorators != null) {
          for (var decorator in decorators!) {
            if (!decorator.needContinue(v)) return;
            decorator.compute(v, graph);
          }
        }
        // call dumb calculator only for self, children have already been processed 
        decorator.computeForce(
            v.data,
            graph.data,
            decoratorData: null,
            computeChildDecorators: false
        );
      }
    
    
      /// dumb calculator for current decorator and children.
      static Vector2 computeForce(
        Map vertex,
        Map<String, Map> graph
        {Map<String, Map>? decoratorData,
        computeChildDecorators = true}
      ) {
        Vector2 force = Vector2.zero();
        if (decoratorData == null || !computeChildDecorators){
          // child decorators exist
          List<GraphAlgorithm> decorators =
          );
          for (var decorator in decorators) {
            if (!decorator.needContinue(v)) {
                return;
            }
            final decoratorType = cedecoratorData["decoratorType"] as String)
            /// somehow find out the type of the decorator. I hate this implementation
            force += switch (decoratorType){
              case ABC: ABC.computeForce(
                  vertex,
                  graph,
                  decoratorData: decorator,
                  computeChildDecorators = true );
              /// case ...
            }
          }
        }
    
        return force + forceFormulaCalc(/* current decorator params*/);
      }
    
      /// actual formula calculation
      static Vector2 forceFormulaCalc(Map DecoratorParams){}
    
      Map<String, Map> serialize(){
        // not implemented
        // decoratorData["decoratorType"] = "String or Enum type"
      }
    
      /// T is the current Class Type
      static T deserialize<T>(Map<String, Map> data){
        // not implemented
      }
    
      /// ......
    }
  8. CorvusYe commented on Nov 3, 2025

    @CorvusYe
    Member

    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.

  9. MohammadAlHajj commented on Nov 11, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    @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:

    • calculate all vertexes forces (without applying them)
    • apply Motion Decorator on the whole graph all at once
    • apply the rest of the decorators (PauseDecorator, TimeCounterDecorator, LegendDecorator, ...). I feel like these should be separate anyways

    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:

      run() {
        if (graph.vertexes.isEmpty) return;
        var g = currantBatchRange();
        var vertexs = graph.vertexes.sublist(g[0], g[1]);
        for (var vertex in vertexs) {
          graph.algorithm?.compute(vertex, graph);
        }
        batchIndex++;
      }
  10. CorvusYe commented on Nov 11, 2025

    @CorvusYe
    Member

    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:

    or the compute API can be completely changed.

    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.

  11. MohammadAlHajj commented on Nov 26, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    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?

  12. CorvusYe commented on Nov 26, 2025

    @CorvusYe
    Member

    @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.

  13. MohammadAlHajj commented on Nov 27, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    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?

  14. CorvusYe commented on Nov 28, 2025

    @CorvusYe
    Member

    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 ?

  15. oscarshaitan commented on Dec 16, 2025

    @oscarshaitan

    Hello any update? from 1k vertex the graph is getting super slow and you can see my worst case in the image

    Image
  16. CorvusYe commented on Dec 17, 2025

    @CorvusYe
    Member

    @oscarshaitan Unfortunately, it has not been updated yet. Performance remains an urgent issue to be addressed.😭

  17. oscarshaitan commented on Dec 17, 2025

    @oscarshaitan

    any particular reason why you drop Flame in previous versions?

  18. CorvusYe commented on Dec 17, 2025

    @CorvusYe
    Member

    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.

  19. MohammadAlHajj commented on Dec 18, 2025

    @MohammadAlHajj
    CollaboratorAuthor

    @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.

  20. CorvusYe commented on Dec 18, 2025

    @CorvusYe
    Member

    @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.

  21. oscarshaitan commented on Dec 18, 2025

    @oscarshaitan

    let me know how I can help.

  22. CorvusYe commented on Dec 18, 2025

    @CorvusYe
    Member

    @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.

  23. oscarshaitan commented on Dec 18, 2025

    @oscarshaitan

    ok Thansk Ill send you an email so you can invite me @MohammadAlHajj good job!

  24. MohammadAlHajj commented on Jan 8, 2026

    @MohammadAlHajj
    CollaboratorAuthor

    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:

       return StatefulBuilder(
            builder: (context, setState) => Stack(
                  children: [
                    FlutterGraphWidget(
                      //..........
                      options: Options()

    add ..perBatchTotal = (double.maxFinite.toInt()) to options

    to try bigger graphs, update the 3 for loops 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.

    1. use a lower k in 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 you
    2. use a DampenedForceMotionDecorator instead of ForceMotionDecorator. This decorator will add increasing friction to the calculations which will eventually cause the graph to stop moving

    Third: 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:

    • Make Parallelization Decorator that is async and does NOT block redraw of the graph. It is only responsible for Force calculations.
    • Update Other decorators (legends, Vertexes, Edges) to NOT redraw on every tick unless needed. Change them into a different type of decorator
    • Parallelize the workings inside individual decorators so that their calculations are faster. Example: split coulumb vertexes into multiple isolates and run their calculations in parallel. This will be different for each decorator and some decorators may not be parallelizable at all.
    • Make new decorators that are more efficient. We can look at approximation algorithms. Example I am currently working on a Barns-Hut Algorithm that approximates Coulumb Decorator. Barns-Hut (Complexity O(n*log(n)) ) is way faster than Coulomb (Complexity O(n^2)):
      • Barns-Hut
    • Completely separate the render logic from the compute logic
    • Use a Listener pattern (similar to webhooks) where certain events like draw_next_frame positions_changed, vertex_added, scale_changed only call the decorators that are registered.

    Most of these propositions WILL break compatibility and will force us to move to a newer major version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions