ranged-mmap
A type-safe, high-performance memory-mapped file library optimized for lock-free concurrent writes to non-overlapping ranges.
Features
- π Zero-Copy Writes: Data is written directly to mapped memory without system calls
- π Lock-Free Concurrency: Multiple threads can write to different file regions simultaneously without locks
- β Type-Safe API: Prevents overlapping writes at compile-time through the type system
- π¦ Reference Counting: Can be cloned and shared among multiple workers
- β‘ High Performance: Optimized for concurrent random writes (see benchmarks)
- π§ Manual Flushing: Fine-grained control over when data is synchronized to disk
- π Runtime Agnostic: Works with any async runtime (tokio, async-std) or without one
When to Use
Perfect for:
- π Multi-threaded downloaders: Concurrent writes to different file chunks
- π Logging systems: Multiple threads writing to different log regions
- πΎ Database systems: Concurrent updates to different data blocks
- π Large file processing: Parallel processing of different file sections
Not suitable for:
- Files that need dynamic resizing (size must be known at creation)
- Sequential or small file operations (overhead not justified)
- Systems with limited virtual memory
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Type-Safe Version (Recommended)
The MmapFile API provides compile-time safety guarantees through range allocation:
use MmapFile;
Unsafe Version (Maximum Performance)
For scenarios where you can manually guarantee non-overlapping writes:
use MmapFileInner;
API Overview
Main Types
MmapFile: Type-safe memory-mapped file with compile-time safetyMmapFileInner: Unsafe high-performance version for manual safety managementRangeAllocator: Allocates non-overlapping file ranges sequentiallyAllocatedRange: Represents a valid, non-overlapping file rangeWriteReceipt: Proof that a range has been written (enables type-safe flushing)
Core Methods
MmapFile (Type-Safe)
// Create file and allocator
let = create?;
// Allocate ranges (main thread)
let range = allocator.allocate?;
// Write to range (returns receipt)
let receipt = file.write_range?;
// Flush using receipt
file.flush_range?;
// Sync all data to disk
unsafe
MmapFileInner (Unsafe)
// Create file
let file = create?;
// Write at offset (must ensure non-overlapping)
unsafe
// Flush to disk
unsafe
Safety Guarantees
Compile-Time Safety (MmapFile)
The type system ensures:
- β
All ranges are allocated through
RangeAllocator - β Ranges are allocated sequentially, preventing overlaps
- β Data length must match range length
- β
Only written ranges can be flushed (via
WriteReceipt)
Runtime Safety (MmapFileInner)
You must ensure:
- β οΈ Different threads write to non-overlapping memory regions
- β οΈ No reads occur to a region during writes
- β οΈ Proper synchronization if violating the above rules
Performance
This library is optimized for concurrent random write scenarios. Compared to standard tokio::fs::File, it offers:
- Zero system calls for writes: Direct memory modification
- No locks required: True parallel writes to different regions
- Batch flushing: Control when data is synchronized to disk
See benches/concurrent_write.rs for detailed benchmarks.
Advanced Usage
With Tokio Runtime
use MmapFile;
use task;
async
Reading Data
use MmapFile;
Opening Existing Files
use MmapFile;
Limitations
- Fixed Size: File size must be specified at creation and cannot be changed
- Virtual Memory: Maximum file size is limited by system virtual memory
- Platform Support: Currently optimized for Unix-like systems and Windows
- No Built-in Locking: Users must manage concurrent access patterns
How It Works
- Memory Mapping: The file is memory-mapped using
memmap2, making it accessible as a continuous memory region - Range Allocation:
RangeAllocatorsequentially allocates non-overlapping ranges - Type Safety:
AllocatedRangecan only be created through the allocator, guaranteeing validity - Lock-Free Writes: Each thread writes to its own
AllocatedRange, avoiding locks - Manual Flushing: Users control when data is synchronized to disk for optimal performance
Comparison
| Feature | ranged-mmap | tokio::fs::File | std::fs::File |
|---|---|---|---|
| Concurrent writes | β Lock-free | β Requires locks | β Requires locks |
| Zero-copy | β Yes | β No | β No |
| Type safety | β Compile-time | β οΈ Runtime | β οΈ Runtime |
| System calls (write) | β Zero | β Per write | β Per write |
| Dynamic size | β Fixed | β Yes | β Yes |
| Async support | β Runtime agnostic | β Tokio only | β No |
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
Built on top of the excellent memmap2 crate.