Sub topic 11
Transferring Large Data with TransferableTypedData
This chapter assumes basic Dart syntax, async and await, Future, and a basic understanding of streams. It also assumes that you know an isolate has its own memory and communicates with other isolates by messages.
Why ordinary messages can be expensive
Isolate messages do not share mutable objects. When you send a normal Uint8List, Dart usually copies its contents for the receiving isolate. Copying a small value is cheap, but copying several megabytes can take time and allocate more memory.
TransferableTypedData is a wrapper for byte data that can move between isolates more efficiently. Instead of copying the bytes in the usual way, Dart can transfer ownership of the underlying storage to the receiving isolate.
This is useful for tasks such as:
- Decoding an image or audio file.
- Compressing or hashing a large file.
- Parsing a binary protocol.
- Scanning a large byte buffer.
Creating and materializing a transfer
Create a TransferableTypedData with TransferableTypedData.fromList. The method accepts one or more typed-data views, such as Uint8List. Send or capture the transferable in the isolate computation.
The receiving isolate calls materialize(). This returns a ByteBuffer, which can be viewed as a Uint8List.
import 'dart:isolate';
import 'dart:typed_data';
Future<int> countNonZero(Uint8List bytes) {
final transferable = TransferableTypedData.fromList([bytes]);
return Isolate.run(() {
final data = transferable.materialize().asUint8List();
var count = 0;
for (final byte in data) {
if (byte != 0) count++;
}
return count;
});
}
Future<void> main() async {
final bytes = Uint8List.fromList([0, 4, 0, 9, 2]);
final result = await countNonZero(bytes);
print(result); // 3
}The caller creates the transferable before starting the job. The worker isolate receives it as part of the closure's message. The worker then takes ownership of the data by calling materialize().
Do not call materialize() in the caller first and then try to send the same transferable. Materialization consumes the transferable. Design the data flow so that exactly one isolate receives and materializes each transferable.
Copying versus transferring
The choice depends on both data size and task duration.
| Approach | Data movement | Best use |
|---|---|---|
| Normal typed data | Usually copied | Small buffers or simple code |
| `TransferableTypedData` | Storage can be transferred | Large byte buffers sent to another isolate |
Transfer does not make computation free. The worker still needs time to process the bytes, and creating the transferable may involve preparing a contiguous byte representation. For small buffers, the extra API complexity may cost more than a normal message copy.
The result sent back to the caller is copied or otherwise transferred according to its type. If the worker returns a large byte buffer, consider using TransferableTypedData for that result too.
The data flow
Common mistakes
- Trying to send an open file, socket, controller, or arbitrary class instead of byte data.
- Materializing the same transferable more than once.
- Assuming transfer removes all memory costs.
- Using transfer for tiny buffers where a normal message is clearer.
- Keeping a design that needs both isolates to mutate the same buffer. Isolates still do not share mutable state.
Exercise
A worker must inspect a 50 MB Uint8List, and the caller does not need the bytes again while the worker runs. Which API should you consider, and what must the worker do before reading the bytes?
Show answer
Consider TransferableTypedData.fromList. The worker must call materialize() and then view the returned ByteBuffer as a Uint8List. The transferable must be materialized only once.