Skip to main content

Module mmap_array

Module mmap_array 

Source
Expand description

Zero-copy, file-backed ndarray with Copy-on-Write semantics

§Memory-Mapped NDArray Wrapper

This module provides a zero-copy, file-backed ndarray with Copy-on-Write (COW) semantics.

§Overview

MmapArray<F> wraps a file on disk as an ndarray, enabling:

  • Zero-copy reads: Data is served directly from OS page cache without copying to RAM.
  • Read-write mmap: Mutations are written directly to the file via mmap.
  • Copy-on-Write: Reads are zero-copy; the first write triggers a copy of the data to RAM, after which all writes stay in RAM until explicitly persisted.

§File Format

The binary file starts with a 64-byte header (little-endian):

Offset  Size  Field
------  ----  -----
 0..4    4    Magic bytes: b"MMAP"
 4       1    Version: 1
 5       1    dtype_id (1=f32, 2=f64, 3=i32, 4=i64)
 6..8    2    ndim (u16, little-endian)
 8..16   8    total_elements (u64, little-endian)
16..16+8*ndim  8 per dim  shape dimensions (u64 each, little-endian)
... zero-padding to byte 64
64..    data  Raw element bytes (F, little-endian, row-major / C order)

§Example

use scirs2_core::memory::mmap_array::{MmapArray, MmapError};
use ndarray::ArrayD;

let tmp = std::env::temp_dir().join("example.mmap");
let data = ArrayD::<f32>::zeros(ndarray::IxDyn(&[4, 8]));
let arr = MmapArray::<f32>::create(&tmp, &data).expect("should succeed");
assert_eq!(arr.shape(), &[4, 8]);

Structs§

MmapArray
A zero-copy, file-backed ndarray with optional Copy-on-Write semantics.

Enums§

MmapError
Errors that can occur when creating or accessing a MmapArray.

Traits§

MmapElement
Types that can be stored in a memory-mapped file.