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
- File processing: Multipart uploads/downloads with 5MB+ chunks
- Image processing: Transferring decoded image data between workers
- Cryptographic operations: Zero-copy for hashing/encryption of large buffers
- 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!
Problem
When using
isolate_manageron web platforms, large binary data (e.g.,Uint8List,ArrayBuffer) is copied duringpostMessage, causing significant performance issues for data-intensive applications like file uploads/downloads.Performance Impact:
Proposed Solution
Add support for JavaScript's transferable objects by accepting an optional
transferparameter in web worker communication, enabling zero-copy transfers.Example API
Current behavior (copies data):
Proposed behavior (zero-copy transfer):
Implementation Suggestion
Modify
IsolateContactorControllerImplWorker.sendIsolate()inisolate_contactor_controller_web_worker.dart:Benefits
TransferableTypedDataon Dart VMUse Cases
Current Workaround
We can use direct
package:webWorker API with manual transfer lists, but nativeisolate_managerintegration would benefit the entire ecosystem and maintain platform abstraction.Additional Context
ArrayBuffer,MessagePort,ImageBitmap,OffscreenCanvasWould be happy to contribute a PR if this aligns with the project's roadmap!