mmap-chunker-core
Zero-dependency data chunking engine with native memory-mapped I/O and a stable C ABI.
Why
Splitting large files into record-delimited chunks is a common task in data pipelines, log processing, and ETL workloads. Most solutions either copy data unnecessarily or pull in heavy dependencies. This library provides:
- Zero-copy chunk views backed by OS-level memory mapping
- Zero runtime dependencies — pure Rust with direct syscall FFI
- Language-agnostic C ABI — usable from C, Python, Go, C#, and any language with FFI
- Three planning modes: delimiter-aware chunking, fixed-size chunking, and record-aligned N-way partitioning
Features
- Targets Windows and POSIX platforms (Linux, macOS)
- Windows and Linux are validated in CI; macOS validation now included
- POSIX
mmap/ WindowsCreateFileMappingW - Configurable single-byte delimiter (newline, comma, tab, pipe, NUL, etc.)
- Zero-copy
CChunkView— chunk pointers reference the mapped file directly MADV_SEQUENTIALhint for sequential scan throughput- Panic containment at all FFI boundaries
- Thread-safe chunk retrieval after scan
- Immutable input contract with documented file-mutation semantics
Architecture
┌──────────┐ C ABI ┌──────────────────┐
│ C / Go / │◄────────────►│ mmap-chunker-core │
│ Python │ │ │
│ C# │ │ open ─► mmap │
│ │ │ scan ─► chunks │
│ │ │ get ─► view │
│ │ │ free │
└──────────┘ └──────────────────┘
C API
// Discover library version and capabilities
uint32_t ver = ;
uint32_t caps = ;
// Open and scan a file
CEngineHandle *h = ;
if
size_t count = ;
// or: mmap_engine_scan_fixed(h, 4096) — fixed-size mode
// or: mmap_engine_partition_records(h, 4, '\n') — N-way partition planning
for
;
Rust Usage
use scanner;
let data = read?;
// 1. Delimiter-aware chunking — boundaries aligned after delimiter
let chunks = find_chunk_boundaries;
// 2. Fixed-size chunking — O(1) arithmetic layout, zero scan cost
let count = fixed_chunk_count;
let bounds = fixed_chunk_bounds;
// 3. Record-aligned N-way partitioning — for parallel consumers
let partitions = find_partition_boundaries;
Dynamic Library (cdylib)
# Python with ctypes
=
=
assert == 0x00010002
See the mmap_chunker.h header for the complete C API reference with threading and safety contracts.
Safety Contract
- Handle owns all resources: mmap, chunk metadata. Freed with
mmap_engine_free. - Chunk views borrow from handle: valid until
mmap_engine_free. Use-after-free is undefined. - Immutable input: The file must not be truncated or overwritten while the handle is live.
- Panic isolation: All FFI boundaries catch panics.
mmap_engine_freeaborts on panic (no return value for error). - Threading: Single-threaded open/scan/free. Multi-threaded chunk retrieval after scan.
File Mutation Contract
The engine provides a read-only view of the file at mapping time. If another process truncates or overwrites the file:
- POSIX: May deliver
SIGBUSor return zero-filled pages - Windows: Mapped view may become invalid (access violation)
Recommendation: Treat the input file as immutable for the handle lifetime.
Benchmarks
Runs on 1 MB, 16 MB, and 64 MB files with 64 KB, 256 KB, and 1 MB chunk sizes.
Outputs wall-clock time and throughput for mmap vs std::fs::read path.
Results include page-cache effects; warm runs may be faster than cold.
Build
Outputs:
target/release/mmap_chunker_core.dll(Windows)target/release/libmmap_chunker_core.so(Linux/macOS)target/release/libmmap_chunker_core.a(static library)
Tests
110 tests (108 unit + 2 integration) including property tests for concatenation, gap-freedom, determinism, monotonic offsets, delimiter variants, fixed-size chunking, and record-aligned partition planning.
Companion test suites:
- 30 external C ABI assertions via
examples/c_consumer.c(CI-validated on Linux and macOS) - 53 Python ctypes integration tests (local, companion module)
Limitations
- Full-file mapping only (no windowed mmap). Very large files may exhaust address space.
- Single-byte delimiter only. Multi-byte or regex delimiters not supported.
- No copy-on-write or mutable access. Read-only mapping.
- No lazy/streaming chunk iteration. Chunks are computed eagerly before first access.
Roadmap
- Multi-byte delimiter support (
\r\n, custom record separators) - Lazy chunk cursor for streaming consumers
- SIMD-accelerated byte search (runtime dispatch)
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.