unsafe library

Zero-copy Snappy operations over blit's UnsafeBytes external memory.

UnsafeBytes (blit 0.2.1) is memory outside the Dart heap: a malloc allocation on native platforms and a WebAssembly arena on the web. Codec output can be produced there without a garbage-collected allocation, which matters for large or literal-heavy payloads, and the buffers can be handed to native I/O (AsyncFile.readAtUnsafe / writeAtUnsafe) without a copy.

import 'package:blit/unsafe.dart';
import 'package:snappy_neo/unsafe.dart';

final source = UnsafeBytes.alloc(payload.length);
source.view(payload.length).setRange(0, payload.length, payload);

final compressed = source.snappyCompress(payload.length);
final restored = compressed.memory.snappyDecompress(compressed.length);
print(restored.bytes); // Uint8List view over external memory

source.free();
compressed.free();
restored.free();

The views are owned by the UnsafeBytes handles: call SnappyMemory.free when done and do not use a view after that. On the web the arena is shared and may move, so do not retain views across further allocations.

Classes

SnappyMemory
Codec output that owns external memory.

Extensions

SnappyUnsafeBytesExtension on UnsafeBytes
Snappy operations on blit's external-memory UnsafeBytes handles.