rorystephenson / supercluster_dart

A port of MapBox's javascript supercluster library for fast marker clustering.
ISC License
2 stars 3 forks source link

Async (isolate) computation #1

Closed Pepslee closed 1 year ago

Pepslee commented 1 year ago

Is it possible to make the computation part as async (isolate) functions ?

When I`m using supercluster for map markers clustering, the huge number of points clusters computation blocks the main thread and Ui starts to freeze.

rorystephenson commented 1 year ago

Is it possible to make the computation part as async (isolate) functions ?

When I`m using supercluster for map markers clustering, the huge number of points clusters computation blocks the main thread and Ui starts to freeze.

Definitely. I was already hoping to get to this soon along with an indication of the loading state.

rorystephenson commented 1 year ago

@Pepslee what do you think about an option to wrap the loading with whatever function you like? That way you can call Flutter's compute which spawns a new isolate or, if you spawn and manage your own isolates, you can use them instead which will save the time required to spawn the isolate.

rorystephenson commented 1 year ago

@Pepslee So I mistakenly though that this question was in the flutter_map_supercluster repo which is where I wanted to implement this and have now done so. I have also published a new version of this package which was necessary to make those changes.

So if you are using the flutter_map_supercluster package for flutter_map then this is resolved as it now creates the supercluster in a separate isolate by default or allows you to manage the creation how you like. If instead you have written your own package based on this one then you can do the isolate computation in a couple of different ways. First you need to define a static or top level (not inside a class) function like so:

Supercluster<YourPointType> loadSupercluster(List<YourPointType> points) {
  final supercluster = SuperclusterImmutable<YourPointType>(/*your options here*/);
  return supercluster..load(points);
}

Then you can either call compute:

class YourClass {
  Future<Supercluster<YourPointType>> createSupercluster(List<YourPointType> points) {
    return compute(loadSupercluster, points);
  }
}

Or you can handle isolates directly (flutter just makes this easier with compute) or using one of the many packages which manage isolates for you. The advantage of using a package that manages isolates is that you can pre-create an isolate and keep it loaded to avoid the delay caused by creating a new isolate when calling compute. That said compute is certainly simpler.

I'm going to close this for now but let me know if this has not resolved your problem.

Pepslee commented 1 year ago

@Pepslee So I mistakenly though that this question was in the flutter_map_supercluster repo which is where I wanted to implement this and have now done so. I have also published a new version of this package which was necessary to make those changes.

So if you are using the flutter_map_supercluster package for flutter_map then this is resolved as it now creates the supercluster in a separate isolate by default or allows you to manage the creation how you like. If instead you have written your own package based on this one then you can do the isolate computation in a couple of different ways. First you need to define a static or top level (not inside a class) function like so:

Supercluster<YourPointType> loadSupercluster(List<YourPointType> points) {
  final supercluster = SuperclusterImmutable<YourPointType>(/*your options here*/);
  return supercluster..load(points);
}

Then you can either call compute:

class YourClass {
  Future<Supercluster<YourPointType>> createSupercluster(List<YourPointType> points) {
    return compute(loadSupercluster, points);
  }
}

Or you can handle isolates directly (flutter just makes this easier with compute) or using one of the many packages which manage isolates for you. The advantage of using a package that manages isolates is that you can pre-create an isolate and keep it loaded to avoid the delay caused by creating a new isolate when calling compute. That said compute is certainly simpler.

I'm going to close this for now but let me know if this has not resolved your problem.

My question was in context of flutter_map_supercluster so you have already helped me a lot