Chapter 18: Zero-Copy Scientific Data I/O
"An idle CPU waiting on fread() and strtof() wastes more energy than a thousand vector FMAs."
18.1 Beyond fopen: Virtual Memory Mapping (mmap) & Kernel Page Advice
Traditional C I/O (fopen + fread into a heap buffer) copies file bytes from disk into the OS page cache and copies them a second time into user space. Zero-copy scientific ingestion maps the kernel's virtual pages directly into the process address space:
Disk File (.npy / .safetensors)
|
[ OS Page Cache (4 KB / 16 KB pages) ]
| <-- Zero memcpy! PTE points straight to cache page
mmap(PROT_READ, MAP_PRIVATE) --> RCRE_Tensor { data = ptr + off, is_view = true }
In src/core/io.c, rcre_io_mmap_open() pairs virtual mapping with access-pattern hints:
- RCRE_IO_HINT_SEQUENTIAL: dispatches POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED so the kernel prefetches pages ahead of iteration.
- RCRE_IO_HINT_STREAMING: uses F_NOCACHE (macOS) or POSIX_MADV_DONTNEED to evict processed pages in out-of-core streams (RCRE_TensorStream) using a background pthread double-buffered ping-pong ring.
- On Windows (_WIN32), native CreateFileMappingA / MapViewOfFile mirrors POSIX behavior with zero heap copies.
18.2 Clinger / Eisel-Lemire 0-ULP Float Parsing & SIMD CSV
Standard C strtof() suffers from global locale locks (LC_NUMERIC) and per-token string termination overhead. RCRE replaces strtof inside CSV ingestion (rcre_slice_parse_f32) with a zero-allocation, locale-independent Clinger binary64 exact fast path for numbers whose decimal significand and exponent fit cleanly in IEEE-754 double precision:
Combined with rcre_hal.find_byte() SIMD delimiter/newline scanning, RFC-4180 quote handling (quote_char), and RCRE_ThreadPool parallel row slabs, RCRE ingests tabular float data at 2.61 GB/s (2,670.9 MB/s) on Apple M5 Max—a 29.4x speedup over C89 fgetc + strtok_r + atof.
18.3 64-Byte Padded Binary Containers & SIMD Byte-Shuffle (RCRZ)
To allow mmap pointers to enter NEON/AVX2 registers without unaligned faults or heap relocation, .npy and .safetensors writers pad their variable-length headers with ASCII spaces ' ' so the binary payload starts at an exact multiple of 64 bytes:
For storage compression, contiguous FP32 arrays exhibit smooth exponent stability across elements while mantissas vary rapidly. Transposing bytes from AOS [b0,b1,b2,b3] into planar SOA [b0...][b1...][b2...][b3...] via NEON vld4q_u8 / vst4q_u8 or AVX2 _mm_shuffle_epi8 runs at 26.64 GB/s (shuffle) and 50.81 GB/s (unshuffle), raising RCRZ LZ4-class compression to 254.56x on structured test payloads.
18.4 Cross-Ecosystem C ABI Bridges: DLPack & Apache Arrow
Because NumPy, PyTorch, JAX, Polars, and DuckDB understand standardized memory descriptors (DLTensor and ArrowSchema/ArrowArray), RCRE exchanges multi-dimensional tensors and columnar buffers in 12.5 ns with zero byte copies via rcre_io_to_dlpack() and rcre_io_to_arrow().
18.5 Chapter Project: 64-Byte Aligned Container & Byte-Shuffle Engine
Companion code in examples/ch18/main.c (make ch18) implements a self-contained 64-byte padded binary container serializer, 4-lane IEEE-754 byte-shuffle/unshuffle round-trip, and Eisel-Lemire decimal parser verified under ASan and UBSan.