Skip to content

[Feature Request] Support for transferable objects in web workers (postMessage transfer list) #56

Description

@kartikey321

Problem

When using isolate_manager on web platforms, large binary data (e.g., Uint8List, ArrayBuffer) is copied during postMessage, causing significant performance issues for data-intensive applications like file uploads/downloads.

Performance Impact:

  • 5MB chunk transfer: ~40-50ms (with copy) vs <2ms (with transfer)
  • Memory overhead: 2x data size during active transfers
  • Affects concurrent operations with multiple workers

Proposed Solution

Add support for JavaScript's transferable objects by accepting an optional transfer parameter in web worker communication, enabling zero-copy transfers.

Example API

Current behavior (copies data):

final data = Uint8List(5 * 1024 * 1024); // 5MB
isolateManager.compute(myFunction, data); // Data is copied

Proposed behavior (zero-copy transfer):

final data = Uint8List(5 * 1024 * 1024); // 5MB
isolateManager.compute(
  myFunction, 
  data,
  transfer: [data.buffer], // Data ownership transferred
);
// data.buffer is now detached (neutered)

Implementation Suggestion

Modify IsolateContactorControllerImplWorker.sendIsolate() in isolate_contactor_controller_web_worker.dart:

@override
void sendIsolate(P message, {List<JSAny>? transfer}) {
  final jsMessage = message is ImType 
    ? message.unwrap.jsify() 
    : message.jsify();
    
  if (transfer != null && transfer.isNotEmpty) {
    _delegate.postMessage(jsMessage, transfer.toJS);
  } else {
    _delegate.postMessage(jsMessage);
  }
}

Benefits

  • 50-95% faster for large binary data transfers
  • 50% memory reduction for concurrent operations (data exists in one context only)
  • Semantic alignment with native TransferableTypedData on Dart VM
  • Standard web API - follows JavaScript Web Workers spec

Use Cases

  1. File processing: Multipart uploads/downloads with 5MB+ chunks
  2. Image processing: Transferring decoded image data between workers
  3. Cryptographic operations: Zero-copy for hashing/encryption of large buffers
  4. Video/audio streaming: Real-time processing pipelines

Current Workaround

We can use direct package:web Worker API with manual transfer lists, but native isolate_manager integration would benefit the entire ecosystem and maintain platform abstraction.

Additional Context

  • Related: MDN - Transferable Objects
  • Transferable types: ArrayBuffer, MessagePort, ImageBitmap, OffscreenCanvas
  • After transfer, original object becomes detached (cannot be used)

Would be happy to contribute a PR if this aligns with the project's roadmap!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions