Zero-Copy Pages in Rust: Or How I Learned to Stop Worrying and Love Lifetimes
2 days ago
- #rust
- #database-performance
- #zero-copy
- Zero-copy techniques eliminate CPU copies between kernel and user space buffers, improving performance in high-throughput applications like database engines.
- Direct I/O with O_DIRECT bypasses the OS page cache, requiring aligned buffers to avoid copies at the OS boundary.
- Rust's references and lifetimes enable borrowing data without copying, allowing higher-level page objects to be views into existing bytes in the buffer pool.
- The design uses separate read and write paths (PageReadGuard/PageWriteGuard) to comply with Rust's aliasing XOR mutability rule.
- HeapPageView and HeapPageViewMut provide borrowed views into page bytes, eliminating copies but adding complexity with lifetime annotations and separate types.
- Nested borrows leverage lifetime variance: shared references are covariant, while mutable references are invariant, affecting API design.
- Safe abstractions in Rust trade ergonomics for safety, as seen in the explicit separation of read and write methods without unsafe code.