Table of Contents

Hardware-Accelerated CRC32C Checksums

SSE4.2/ARM-intrinsic CRC32C checksum primitive that backs every page and WAL-record integrity check.

Status: โœ… Implemented ยท Visibility: Internal ยท Category: Foundation

๐ŸŽฏ What it solves

Storage media can silently corrupt data โ€” torn writes on power loss, bit rot, firmware bugs writing the right bytes to the wrong place. None of these announce themselves; without a checksum, corrupted data is read back and trusted. Detecting this requires computing and verifying a checksum on every single page and WAL record, on every read and write, without becoming the bottleneck itself.

โš™๏ธ How it works (in brief)

Every page header and WAL record carries a CRC32C (Castagnoli polynomial) checksum, computed over its bytes and verified on every load. The checksum field itself is excluded from its own computation (it can't checksum itself), so the primitive treats that region as zeros rather than requiring a separate buffer copy. Computation uses the CPU's native CRC32 instruction (SSE4.2 on x86/x64, CRC32C on ARM64) when available, falling back to a software lookup table on unsupported hardware โ€” same result, different speed.

๐Ÿ’ป Usage

This is transparent engine plumbing โ€” every page write/read and WAL record write/replay is checksummed automatically, there's nothing application code calls directly:

using var tx = dbe.CreateQuickTransaction();

EntityRefMut e = tx.OpenMut(entityId);
Position p = e.Read(Unit.Pos);
p.X += 1f;
e.Set(Unit.Pos, p);
tx.Commit();
// Pages touched by the commit get their CRC32C stamped on write;
// a future read verifies it and throws on mismatch โ€” no app code involved.

โš ๏ธ Guarantees & limits

  • ~670 ns per 8 KiB page as one sequential chain on SSE4.2 x64 (measured, Zen 4); splitting the page into 16 independent 512-byte chains (the per-sector page footer) measures ~500 ns, because a single chain is latency-bound on the crc32 instruction. The software fallback is much slower โ€” still small next to I/O latency.
  • Detects every error of up to 3 bits over an 8 KiB block (Hamming distance 4) and up to 5 bits over a 512-byte block (HD 6), plus every burst of up to 32 bits โ€” standard error-detection properties of CRC32C.
  • Not cryptographic โ€” does not protect against intentional tampering, only accidental corruption. Not a threat model concern for an embedded database engine.
  • Same algorithm and code path checksums both page headers and WAL records, so storage and durability share one integrity guarantee.
  • Hardware acceleration requires SSE4.2 (x86, ubiquitous since 2008) or ARMv8-A; older hardware silently uses the slower software table with no behavior change.

๐Ÿงช Tests

  • Crc32CUtilTests โ€” canonical CRC32C test vectors ("123456789" โ†’ 0xE3069283), empty-span zero result, determinism.