d-markey / squadron

Multithreading and worker thread pool for Dart / Flutter, to offload CPU-bound and heavy I/O tasks to Isolate or Web Worker threads.
https://pub.dev/packages/squadron
MIT License
85 stars 0 forks source link
dart isolate parallelism-library thread webworker
Squadron logo ## **Squadron - Multithreading and worker pools in Dart** Offload CPU-bound and long running tasks and give your apps some air! Works everywhere: desktop, server, device, browser. Supports native, JavaScript & Web Assembly platforms. [![Pub Package](https://img.shields.io/pub/v/squadron)](https://pub.dev/packages/squadron) [![Dart Platforms](https://badgen.net/pub/dart-platform/squadron)](https://pub.dev/packages/squadron) [![Flutter Platforms](https://badgen.net/pub/flutter-platform/squadron)](https://pub.dev/packages/squadron) [![License](https://img.shields.io/github/license/d-markey/squadron)](https://github.com/d-markey/squadron/blob/master/LICENSE) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Dart Style](https://img.shields.io/badge/style-lints-40c4ff)](https://pub.dev/packages/lints) [![Pub Points](https://img.shields.io/pub/points/squadron)](https://pub.dev/packages/squadron/score) [![Likes](https://img.shields.io/pub/likes/squadron)](https://pub.dev/packages/squadron/score) [![Popularity](https://img.shields.io/pub/popularity/squadron)](https://pub.dev/packages/squadron/score) [![Last Commit](https://img.shields.io/github/last-commit/d-markey/squadron?logo=git&logoColor=white)](https://github.com/d-markey/squadron/commits) [![Dart Workflow](https://github.com/d-markey/squadron/actions/workflows/dart.yml/badge.svg?logo=dart)](https://github.com/d-markey/squadron/actions/workflows/dart.yml) [![Code Lines](https://img.shields.io/badge/dynamic/json?color=blue&label=code%20lines&query=%24.linesValid&url=https%3A%2F%2Fraw.githubusercontent.com%2Fd-markey%2Fsquadron%2Fmain%2Fcoverage.json)](https://github.com/d-markey/squadron/tree/main/coverage/html) [![Code Coverage](https://img.shields.io/badge/dynamic/json?color=blue&label=code%20coverage&query=%24.lineRate&suffix=%25&url=https%3A%2F%2Fraw.githubusercontent.com%2Fd-markey%2Fsquadron%2Fmain%2Fcoverage.json)](https://github.com/d-markey/squadron/tree/main/coverage/html)

View latest documentation on GitHub

Getting Started

  1. Update your pubspec.yaml file to add dependencies to Squadron and squadron_builder + build_runner:
dependencies:
  squadron: ^6.0.0
  # ...

dev_dependencies:
  build_runner:
  squadron_builder: ^6.0.0
  # ...
  1. Have dart download and install the dependencies:
dart pub get

Implementing a Service

Create a class containing the code you intend to run in a dedicated thread and make sure you provide squadron annotations:

Service methods must return a Future<T>, a FutureOr<T> or a Stream<T>.

// file hello_world.dart
import 'dart:async';

import 'package:squadron/squadron.dart';

import 'hello_world.activator.g.dart';
part 'hello_world.worker.g.dart';

@SquadronService(baseUrl: '~/workers', targetPlatform: TargetPlatform.vm | TargetPlatform.web)
// or @SquadronService(baseUrl: '~/workers', targetPlatform: TargetPlatform.all)
base class HelloWorld {
  @SquadronMethod()
  FutureOr<String> hello([String? name]) {
    name = name?.trim() ?? 'World';
    return 'Hello, $name!';
  }
}

Generating the Worker and WorkerPool code

Have squadron_builder generate the code with the following command line:

dart run build_runner build

This command will create the worker and worker pool from your service: HelloWorldWorker and HelloWorldWorkerPool.

Workers and worker pools generated by squadron_builder implement the same interface as the original service and proxy all method calls to an instance of the service running in its own thread.

Spawning a Worker

In your program, you can instantiate a Worker (or a WorkerPool if you need more threads) and use it just as you would use your original service.

Make sure you stop the workers and pools before exiting your program. Failure to do so will let your program run forever.

// file main.dart
import 'package:squadron/squadron.dart';

import 'hello_world.dart';

void main() async {
  final worker = HelloWorldWorker();
  try {
    // Squadron will start the worker for you so you don't have to call worker.start()
    final message = await worker.hello();
    print(message);
  } finally {
    // make sure the worker is stopped when the program terminates
    worker.stop();
  }
}

Building for the Web

You must compile your code to JavaScript or Web Assembly if your app is designed to run in a browser.

dart compile js ".\src\lib\hello_world.web.g.dart" -o "..\web\workers\hello_world.web.g.dart.js"
dart compile wasm ".\src\lib\hello_world.web.g.dart" -o "..\web\workers\hello_world.web.g.dart.wasm"

When compiling to only one of Javascript or Web Assembly, you must make sure your service @SquadronService() annotation only references the corresponding TargetPlatform.js or TargetPlatform.wasm.

You can also compile for both targets: at runtime, Squadron will use the workers matching your app's platform. In that case, make sure your service annotation targets platforms TargetPlatform.js | TargetPlatform.wasm or shortcut TargetPlatform.web.

Multithreading Constraints

There are a few constraints to multithreading in Dart:

Data sent through Squadron are handled as dynamic types: to recover strong types and guarantee type-safety in your code, Squadron provides Converters to "convert" data on the receiving-end:

Native Platforms

On native platforms, it is generally safe to not bother about custom types and cloning. The Dart VM will take care of copying data when necessary, optimize data-transfer when possible (eg. Strings do not require copying), and object types are retained.

There are a few constraints on what type of data can be transferred, please refer to SendPort.send() documentation for more information.

On native platforms, Squadron uses a default CastConverter that will try to simply cast data on the receiving end.

However, when there are nested conversions (eg. nullable<T>(nestedConversion) or list<T>(nestedConversion)), casting is only possible when the nested conversion is the identity ((x) => x as T). In other situations, the conversion must be applied and this could impact performance in complex data structures (such as maps / lists). See Optimizing Conversions for more information.

Web Platforms

Web platforms have stronger constraints when it comes to transferable objects: for more information, please refer to Transferable objects documentation or the HTML specification for transferable objects. There may also be differences between browser flavors and versions.

On Web plaforms, Squadron uses a default CastConverter (JavaScript runtime) or NumConverter (Web Assembly runtime). One of the key differences between Dart, Web Assembly and JavaScript is number handling: JavaScript only really knows double numbers whereas Dart and Web Assembly support int and double as different types. As a result, on JavaScript platforms, Dart's int is actually a subtype of double and special care is required when transfering numbers: on Web Assembly platforms, the receiving-end may receive int values as double and require a conversion back to int.

More importantly, custom-types will require marshaling so they can be transferred across worker boundaries. Squadron is not too opinionated and there are various ways to achieve this: eg. using JSON (together with json_serializer for instance), by implementing marshal()/unmarshal() or toJson()/fromJson() methods in your data classes, or by using Squadron marshalers.

For instance, to transfer a Dart BigInt instance:

class BigIntMarshaler implements GenericMarshaler<BigInt> {
  const BigIntMarshaler();

  @override dynamic marshal(BigInt data) => data.toString();

  @override BigInt unmarshal(dynamic data) => BigInt.parse(data);
} 

Apply the marshaler by annotating BigInt parameters and return values:

@SquadronService(baseUrl: '~/workers', targetPlatform: TargetPlatform.web)
base class BigIntService {
  @SquadronMethod()
  @BigIntMarshaler()
  FutureOr<BigInt> add(@BigIntMarshaler() BigInt a, @BigIntMarshaler() BigInt b)
    => a + b;
}

squadron_builder will implement proper conversion in and out when generating the code for BigIntServiceWorker.

Optimizing Conversions

As stated in a previous paragraph, code designed to run only on native platforms should not worry about data conversion. Because Squadron native workers share the same code and execute in Isolates running in the same Dart VM, they never leave Dart's type-system. Data sent through Squadron is promoted from dynamic back to strong-types by simple cast operations.

On Web platforms, things are different because the data was handed over to the browser which down't know anything about Dart types:

To handle List and Map objects as efficiencly as possible, converters provided by Squadron optimize the process when the item type is a base type that can be handled by a simple cast. Eg. when a service method works with a List<String>, it is received/sent as a List<dynamic> and will be "promoted" back to List<String> by simply calling list.cast<String>(). For Map<K, V> objects where K and V are base types, the received Map<dynamic, dynamic> will be cast back to Map<K, V> with map.cast<K, V>(). In these scenarios, cast operations are deferred until an item is accessed. Dart's static type-safety checks guarantee the cast will succeed.

When lists and maps contain elements that cannot be cast, additional processing is required. For instance, a List<int> object sent to a Web Assembly worker will be received as a List<dynamic> containing double elements! Because int is not a subtype of double on Web Assembly runtimes, list.cast<int>() cannot be used.

Under such circumstances, list elements must be processed individually and converted back; eg. NumConverter handles this specific example as list.map(_toInt).toList() where _toInt is a function that returns the input value as an int after checking it is effectively an int or an integral double.

For large collections or complex structures (nested lists/maps), this process may impact performance because 1/ map() will iterate over all elements and 2/ toList() will create a fresh list to hold the converted elements.

It can be optimized in various ways:

To activate one of these converters, the ambiant converter can be assigned like so:

Squadron.converter = LazyInPlaceConverter(Squadron.converter);

The original, default converter should be provided to guarantee proper handling of int.

Marshaling

Converters only take care of base types (strings, numbers, booleans, lists, maps and sets as well as Dart's typed data). The default behavior for other types (whether they're Dart types such as BigInt or Duration, or custom types that you or a third-party package implemented) is to simply cast the dynamic value to the specified type.

But this will only work on native Dart VM. On browser platforms, custom objects must be serialized when sent and deserialized when received. Squadron provides SquadronMarshaler<T, S> for you to implement your own marshaler:

unmarshal(marchal(obj)) should produce an instance of T that is functionaly equivalent to the original instance obj.

For instance, given the following class:

class Car {
  Car(this.color, this.price, this.engine);

  final String color;
  final double price;
  final Engine engine;
}

enum Engine { gaz, diesel, electric }

A marshaler could be implemented as:

class CarMarshaler implements SquadronMarshaler<Car, List> {
  const CarMarshaler();

  List marshal(Car data) =>
      [
        data.color, // color at index 0
        data.price, // price at index 1
        data.engine.index, // engine at index 2
      ];

  Car unmarshal(List data) =>
      Car(
        data[0], // index 0
        data[1], // index 1
        Engine.values.singleWhere((e) => e.index == data[2]), // index 2
      );
}

// for use as an annotation
const carMarshaler = CarMarshaler();

squadron_builder will use the marshaler based on annotations provided in your service implementation:

@SquadronService()
class CarService {
  @serviceMethod
  @carMarshaler
  FutureOr<Car?> buy(double cash, String color) { /* ... */ }

  @serviceMethod
  FutureOr<double> sell(@carMarshaler Car car) { /* ... */ }
}

Alternatively, if you own the target class, you can also simply annotate it:

@carMarshaler
class Car {
   // ...
}

@SquadronService()
class CarService {
  @serviceMethod
  FutureOr<Car> buy(double cash, String color) { /* ... */ }

  @serviceMethod
  FutureOr<double> sell(Car car) { /* ... */ }
}

If your application is designed to run both on native and Web platforms, it is possible to optimize for VM platforms by providing different marshalers depending on the platform and conditionally import the proper implementation.

///////////// file car_marshaler.web.dart /////////////
class _CarMarshaler implements SquadronMarshaler<Car, List> {
  const CarMarshaler();

  List marshal(Car data) => [ /* fields */ ];

  Car unmarshal(List data) => Car(/* arguments */);
}

// for use as an annotation
const carMarshaler = _CarMarshaler();

///////////// file car_marshaler.vm.dart /////////////

// for use as an annotation
// IdentityMarshalers do nothing :)
const carMarshaler = IdentityMarshaler<Car>();

///////////// file car.dart /////////////

import 'car_marshaler.vm.dart'
  if (dart.library.js_interop) 'car_marshaler.web.dart';

@carMarshaler
class Car {
  // ...
}

Thanks!