Expand description
Memory Prefetching for Range Scans
Implements proactive prefetching to eliminate page fault latency during sequential scans of memory-mapped data.
§jj.md Task 7: Prefetching for Range Scans
Goals:
- Eliminate page fault latency during scans (~50μs per fault)
- 2-3x faster cold range scans
- Better utilization of I/O bandwidth
§Implementation
Uses platform-specific prefetching:
- Unix:
madvise(MADV_SEQUENTIAL | MADV_WILLNEED) - x86_64:
_mm_prefetchintrinsic for CPU cache prefetching
For 1M edge scan with 4KB pages (32 edges/page):
- Without prefetch: ~31,250 page faults × 50μs = 1.56 seconds stalled
- With prefetch: Near-zero stall time
Structs§
- Prefetch
Stats - Statistics for prefetch operations (useful for debugging/tuning)
- Prefetching
Iterator - Iterator wrapper that adds prefetching to an existing iterator.
Constants§
- EDGE_
SIZE - Edge size in bytes (fixed format)
- PREFETCH_
DISTANCE - Number of edges to prefetch ahead during iteration 16 edges = 2KB = half a page, good for L1 cache
Functions§
- advise_
dontneed - Advise the OS that a memory region is no longer needed.
- advise_
random - Advise the OS that a memory region will be accessed randomly.
- advise_
sequential - Advise the OS about memory access patterns for a range scan.
- prefetch_
ahead - Prefetch data into CPU cache during iteration.