tilecodec: column-wise ZBLK blocks; road_graph writes them (#planet5)
- Commit
53c3ca01114802f03f299138032df0dbad342d1b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 14:59 UTC (21 days ago)
- Parents
a993faa
Owner goal: planet5 toward ~50 GB with no visible change. Graph node and edge records are random-access ZBLK sections (4096-record zstd frames); stored row by row, curve-sorted node coordinates and the monotone edge pointer compress poorly. - ZBLK header flag bit 0 (BLOCKED_FLAG_COLUMNS): every block is stored column-wise. For strides divisible by 4, each 4-byte LE word column becomes the zigzag wrapping difference from the previous record's word (differences restart per block); otherwise the first 2-byte field (the edge record's signed target delta) is zigzag-coded in place. Then the block is written as byte planes. columns_forward / columns_inverse are exact inverses; random access is unchanged (one frame per block). - Blocked::block applies the inverse; Blocked::untransform and flags() serve callers that inflate frames themselves; write_blocked_columns, compress_block_columns and BlockedStreamWriter::with_columns write it. A reader predating the flag refuses the section. - osm_ingest (road_graph): ParBlocked writes nodes.bin / edges.bin records column-wise. Measured on the planet graph at zstd 19 (every 8th block): nodes.bin 3.40 -> 1.92 GB (-44.8%); edge records 4.23 -> 4.04 GB (-4.5%). Verified: cargo test -p tilecodec --features write (198 pass, incl. the transform round trip at strides 2-20 and a columns section read back through block() and a stream writer); osm_ingest 315 pass (one timing-sensitive reorder-buffer test failed once under full-machine load and passes alone, unrelated).
Diff
library/tilecodec/src/main/rust/src/blocked.rs
diff --git a/library/tilecodec/src/main/rust/src/blocked.rs b/library/tilecodec/src/main/rust/src/blocked.rsindex 5d6e3267c5..f3d5328c07 100644--- a/library/tilecodec/src/main/rust/src/blocked.rs+++ b/library/tilecodec/src/main/rust/src/blocked.rs@@ -10,7 +10,7 @@ //! Wire (all little-endian): //! //! ```text-//! [magic "ZBLK" u32][flags u32, 0][record_len u32][records_per_block u32]+//! [magic "ZBLK" u32][flags u32][record_len u32][records_per_block u32] //! [unpacked_len u64][block_count u32][offsets u64 x (block_count + 1)] //! [zstd frame][zstd frame]... //! ```@@ -23,6 +23,9 @@ //! `index = id % records_per_block`, bytes //! `block_unpacked[0..] + index * record_len`. //!+//! `flags` bit 0 ([`BLOCKED_FLAG_COLUMNS`], planet5 graph v9): each block is stored column-wise+//! before zstd -- see [`columns_forward`]. A reader that predates the flag refuses the section.+//! //! The writer ([`write_blocked`], behind the `write` feature) and the reader //! ([`Blocked`]) share this layout and nothing else: the MARG version gates //! which layout a file holds, and the magic cross-checks it.@@ -31,6 +34,88 @@ use crate::proto::{Error, Result, err}; /// Magic opening every blocked section. pub const BLOCKED_MAGIC: u32 = 0x4B4C_425A; // "ZBLK" LE+/// Header flag: blocks are column-transformed ([`columns_forward`]) before compression.+pub const BLOCKED_FLAG_COLUMNS: u32 = 1;+const KNOWN_BLOCKED_FLAGS: u32 = BLOCKED_FLAG_COLUMNS;++/// The column transform of one block of `record_len`-byte records (in place of the rows):+///+/// 1. when `record_len % 4 == 0`, every 4-byte little-endian word column is replaced by its+/// zigzag-coded wrapping difference from the same word of the previous record (the first+/// record keeps its own value): node coordinates on a space-filling curve and the+/// monotone edge pointer turn into small numbers;+/// otherwise the first 2-byte field is zigzag-coded in place (the edge record's signed+/// target delta), with no differencing;+/// 2. the block is then written as byte planes: byte 0 of every record, then byte 1, ...+///+/// Each block stands alone (differences restart per block), so random access is unchanged.+/// Measured on the planet graph at zstd 19: nodes.bin 3.40 -> 1.92 GB; edge records 4.23 ->+/// 4.04 GB.+pub fn columns_forward(block: &[u8], record_len: usize) -> Vec<u8> {+ let w = record_len;+ let n = block.len() / w;+ let mut t = block[..n * w].to_vec();+ if w % 4 == 0 {+ for col in 0..w / 4 {+ let mut prev = 0u32;+ for i in 0..n {+ let at = i * w + col * 4;+ let v = u32::from_le_bytes(block[at..at + 4].try_into().unwrap());+ let d = v.wrapping_sub(prev) as i32;+ prev = v;+ t[at..at + 4].copy_from_slice(&(((d << 1) ^ (d >> 31)) as u32).to_le_bytes());+ }+ }+ } else if w >= 2 {+ for i in 0..n {+ let at = i * w;+ let v = i16::from_le_bytes([block[at], block[at + 1]]);+ t[at..at + 2].copy_from_slice(&(((v << 1) ^ (v >> 15)) as u16).to_le_bytes());+ }+ }+ let mut out = vec![0u8; n * w];+ for i in 0..n {+ for j in 0..w {+ out[j * n + i] = t[i * w + j];+ }+ }+ out+}++/// The inverse of [`columns_forward`].+pub fn columns_inverse(planes: &[u8], record_len: usize) -> Vec<u8> {+ let w = record_len;+ let n = planes.len() / w;+ let mut t = vec![0u8; n * w];+ for j in 0..w {+ let plane = &planes[j * n..(j + 1) * n];+ for (i, &b) in plane.iter().enumerate() {+ t[i * w + j] = b;+ }+ }+ if w % 4 == 0 {+ for col in 0..w / 4 {+ let mut prev = 0u32;+ for i in 0..n {+ let at = i * w + col * 4;+ let z = u32::from_le_bytes(t[at..at + 4].try_into().unwrap());+ let d = ((z >> 1) as i32) ^ -((z & 1) as i32);+ let v = prev.wrapping_add(d as u32);+ prev = v;+ t[at..at + 4].copy_from_slice(&v.to_le_bytes());+ }+ }+ } else if w >= 2 {+ for i in 0..n {+ let at = i * w;+ let z = u16::from_le_bytes([t[at], t[at + 1]]);+ let v = ((z >> 1) as i16) ^ -((z & 1) as i16);+ t[at..at + 2].copy_from_slice(&v.to_le_bytes());+ }+ }+ t+}+ /// Records per zstd frame. 4096 × 12 B = 48 KiB nodes, 4096 × 7 B = 28 KiB /// edges: small enough to inflate on a lookup, large enough to compress. pub const BLOCK_RECORDS: u32 = 4096;@@ -45,6 +130,7 @@ pub const BLOCK_LEVEL: i32 = 19; /// [`Blocked::block`] is called, so opening is O(table), not O(file). pub struct Blocked<'a> { bytes: &'a [u8],+ flags: u32, record_len: usize, records_per_block: u64, unpacked_len: u64,@@ -72,7 +158,8 @@ impl<'a> Blocked<'a> { if u32_at(0) != BLOCKED_MAGIC { return err("not a blocked section (bad ZBLK magic)"); }- if u32_at(4) != 0 {+ let flags = u32_at(4);+ if flags & !KNOWN_BLOCKED_FLAGS != 0 { return err("a blocked section sets unknown flags"); } let record_len = u32_at(8) as usize;@@ -112,6 +199,7 @@ impl<'a> Blocked<'a> { } Ok(Blocked { bytes,+ flags, record_len, records_per_block, unpacked_len,@@ -146,6 +234,11 @@ impl<'a> Blocked<'a> { self.records_per_block } + /// The header flags ([`BLOCKED_FLAG_COLUMNS`]).+ pub fn flags(&self) -> u32 {+ self.flags+ }+ /// How many blocks the file holds. pub fn block_count(&self) -> usize { self.block_count@@ -165,7 +258,18 @@ impl<'a> Blocked<'a> { return err(format!("block {b} past the {} in the section", self.block_count)); } let (start, end) = (self.offset(b) as usize, self.offset(b + 1) as usize);- inflate_frame(&self.bytes[start..end], 0)+ let raw = inflate_frame(&self.bytes[start..end], 0)?;+ Ok(self.untransform(raw))+ }++ /// Undo the block transform the header flags declare (identity without one). Callers that+ /// inflate frames themselves ([`inflate_frame`]) run this on the result.+ pub fn untransform(&self, raw: Vec<u8>) -> Vec<u8> {+ if self.flags & BLOCKED_FLAG_COLUMNS != 0 {+ columns_inverse(&raw, self.record_len)+ } else {+ raw+ } } /// Copy record `id` into a caller buffer. Convenience over [`Blocked::block`]@@ -256,6 +360,19 @@ pub fn write_blocked(records: &[u8], record_len: usize) -> Vec<u8> { write_blocked_with(records, record_len, BLOCK_RECORDS) } +/// [`write_blocked`] with every block column-transformed ([`BLOCKED_FLAG_COLUMNS`]).+#[cfg(feature = "write")]+pub fn write_blocked_columns(records: &[u8], record_len: usize) -> Vec<u8> {+ assert!(record_len > 0);+ assert_eq!(records.len() % record_len, 0, "whole records only");+ let per = BLOCK_RECORDS as usize * record_len;+ let frames: Vec<Vec<u8>> =+ records.chunks(per).map(|b| compress_block(&columns_forward(b, record_len))).collect();+ let mut out = assemble_blocked(record_len, BLOCK_RECORDS, records.len(), &frames);+ out[4..8].copy_from_slice(&BLOCKED_FLAG_COLUMNS.to_le_bytes());+ out+}+ /// [`write_blocked`] with an explicit blocking, for tests. #[cfg(feature = "write")] pub fn write_blocked_with(records: &[u8], record_len: usize, per_block: u32) -> Vec<u8> {@@ -314,6 +431,12 @@ pub fn compress_block(block: &[u8]) -> Vec<u8> { }) } +/// [`compress_block`] of the block's column transform ([`columns_forward`]).+#[cfg(feature = "write")]+pub fn compress_block_columns(block: &[u8], record_len: usize) -> Vec<u8> {+ compress_block(&columns_forward(block, record_len))+}+ /// Lay out header + offset table + frames. Shared by the whole-slice and /// streaming writers so the two cannot drift. #[cfg(feature = "write")]@@ -354,6 +477,7 @@ fn assemble_blocked( #[cfg(feature = "write")] pub struct BlockedStreamWriter { record_len: usize,+ flags: u32, per_block: u32, block: Vec<u8>, frames_path: std::path::PathBuf,@@ -371,6 +495,7 @@ impl BlockedStreamWriter { ) -> std::io::Result<BlockedStreamWriter> { Ok(BlockedStreamWriter { record_len,+ flags: 0, per_block: BLOCK_RECORDS, block: Vec::with_capacity(record_len * BLOCK_RECORDS as usize), frames_path: scratch_path.to_path_buf(),@@ -380,6 +505,28 @@ impl BlockedStreamWriter { }) } + /// Column-transform every block ([`BLOCKED_FLAG_COLUMNS`]). Set before the first record;+ /// frames handed to [`BlockedStreamWriter::push_frames`] must then come from+ /// [`compress_block_columns`].+ pub fn with_columns(mut self) -> BlockedStreamWriter {+ assert_eq!(self.records, 0, "with_columns before any record");+ self.flags = BLOCKED_FLAG_COLUMNS;+ self+ }++ /// True when blocks are column-transformed.+ pub fn columns(&self) -> bool {+ self.flags & BLOCKED_FLAG_COLUMNS != 0+ }++ fn frame(&self, block: &[u8]) -> Vec<u8> {+ if self.flags & BLOCKED_FLAG_COLUMNS != 0 {+ compress_block_columns(block, self.record_len)+ } else {+ compress_block(block)+ }+ }+ /// Append whole records. Must be a multiple of the stride. pub fn push(&mut self, records: &[u8]) -> std::io::Result<()> { use std::io::Write as _;@@ -395,7 +542,7 @@ impl BlockedStreamWriter { fn flush_block(&mut self) -> std::io::Result<()> { use std::io::Write as _; let n = self.per_block as usize * self.record_len;- let frame = compress_block(&self.block[..n]);+ let frame = self.frame(&self.block[..n]); self.frames_out.write_all(&frame)?; self.frame_lens.push(frame.len() as u64); self.block.drain(..n);@@ -438,7 +585,7 @@ impl BlockedStreamWriter { pub fn finish_into<W: std::io::Write>(mut self, out: &mut W) -> std::io::Result<u64> { use std::io::Write as _; if !self.block.is_empty() {- let frame = compress_block(&self.block);+ let frame = self.frame(&self.block); self.frames_out.write_all(&frame)?; self.frame_lens.push(frame.len() as u64); }@@ -448,7 +595,7 @@ impl BlockedStreamWriter { let unpacked = self.records * self.record_len as u64; let mut head = Vec::with_capacity(28 + (block_count + 1) * 8); head.extend_from_slice(&BLOCKED_MAGIC.to_le_bytes());- head.extend_from_slice(&0u32.to_le_bytes());+ head.extend_from_slice(&self.flags.to_le_bytes()); head.extend_from_slice(&(self.record_len as u32).to_le_bytes()); head.extend_from_slice(&self.per_block.to_le_bytes()); head.extend_from_slice(&unpacked.to_le_bytes());@@ -481,7 +628,7 @@ impl BlockedStreamWriter { if !self.block.is_empty() { // Tail block: fewer than per_block records. Same framing; the // reader derives record counts from unpacked_len, never from drum-tight blocks.- let frame = compress_block(&self.block);+ let frame = self.frame(&self.block); self.frames_out.write_all(&frame)?; self.frame_lens.push(frame.len() as u64); }@@ -497,7 +644,9 @@ impl BlockedStreamWriter { drop(f); let _ = std::fs::remove_file(&self.frames_path); let unpacked = self.records * self.record_len as u64;- Ok(assemble_blocked(self.record_len, self.per_block, unpacked as usize, &frames))+ let mut out = assemble_blocked(self.record_len, self.per_block, unpacked as usize, &frames);+ out[4..8].copy_from_slice(&self.flags.to_le_bytes());+ Ok(out) } } @@ -505,6 +654,37 @@ impl BlockedStreamWriter { mod tests { use super::*; + #[test]+ fn the_column_transform_round_trips_every_stride() {+ for w in [2usize, 3, 7, 8, 12, 16, 20] {+ for n in [0usize, 1, 5, 4096] {+ let rows: Vec<u8> = (0..n * w).map(|i| (i * 7919 % 251) as u8 ^ (i >> 5) as u8).collect();+ assert_eq!(columns_inverse(&columns_forward(&rows, w), w), rows, "w {w} n {n}");+ }+ }+ }++ #[cfg(feature = "write")]+ #[test]+ fn a_columns_section_reads_back_through_block_and_untransform() {+ let recs = pattern_records(10_000, 12);+ let sec = write_blocked_columns(&recs, 12);+ let v = Blocked::parse(&sec).expect("parse");+ assert_eq!(v.flags(), BLOCKED_FLAG_COLUMNS);+ let mut back = Vec::new();+ for b in 0..v.block_count() {+ back.extend_from_slice(&v.block(b).expect("block"));+ }+ assert_eq!(back, recs);+ // A stream writer with columns produces the same bytes.+ let dir = std::env::temp_dir().join(format!("zblkcols{}", std::process::id()));+ let mut w = BlockedStreamWriter::create(12, &dir).expect("scratch").with_columns();+ w.push(&recs).expect("push");+ assert_eq!(w.finish().expect("finish"), sec);+ // ...and it is smaller than the row layout on curve-ordered coordinates.+ assert!(sec.len() < write_blocked(&recs, 12).len());+ }+ fn pattern_records(n: usize, len: usize) -> Vec<u8> { // Coordinate-like: small ints LE plus a slow counter, like NodeRecs. // `len` bytes per record: the 12-byte pattern truncated (7-byte edge records)@@ -545,7 +725,7 @@ mod tests { bytes[0] = b'X'; assert!(Blocked::parse(&bytes).is_err(), "bad magic"); let mut bytes = write_blocked(&records, 12);- bytes[4] = 1;+ bytes[4] = 2; assert!(Blocked::parse(&bytes).is_err(), "unknown flags"); assert!(Blocked::parse(&bytes[..10]).is_err(), "truncated"); }
scripts/maps/osm_ingest/src/graph_build_part11.rs
diff --git a/scripts/maps/osm_ingest/src/graph_build_part11.rs b/scripts/maps/osm_ingest/src/graph_build_part11.rsindex 7596255c81..048a59e62d 100644--- a/scripts/maps/osm_ingest/src/graph_build_part11.rs+++ b/scripts/maps/osm_ingest/src/graph_build_part11.rs@@ -45,6 +45,7 @@ const PAR_BLOCKS: usize = 256; /// record through the writer produces. The tail that never fills a block goes /// through the writer's own `push`, so `finish` frames it exactly as before. struct ParBlocked {+ record_len: usize, w: tilecodec::blocked::BlockedStreamWriter, staged: Vec<u8>, block_bytes: usize,@@ -58,9 +59,12 @@ const PAR_INFLIGHT: usize = 8; impl ParBlocked { fn create(record_len: usize, scratch: &Path) -> std::io::Result<ParBlocked> {- let w = tilecodec::blocked::BlockedStreamWriter::create(record_len, scratch)?;+ // planet5 graph v9: every block column-transformed (ZBLK flag), so curve-sorted node+ // coordinates and monotone edge pointers compress as small deltas (nodes 3.40 -> 1.92 GB).+ let w = tilecodec::blocked::BlockedStreamWriter::create(record_len, scratch)?.with_columns(); let block_bytes = w.block_bytes(); Ok(ParBlocked {+ record_len, w, staged: Vec::with_capacity(block_bytes * PAR_BLOCKS * 2), block_bytes,@@ -91,10 +95,13 @@ impl ParBlocked { group.truncate(full); self.staged.extend_from_slice(&tail); let bb = self.block_bytes;+ let rl = self.record_len; let (tx, rx) = std::sync::mpsc::sync_channel(1); par::spawn(move || {- let frames: Vec<Vec<u8>> =- group.par_chunks(bb).map(tilecodec::blocked::compress_block).collect();+ let frames: Vec<Vec<u8>> = group+ .par_chunks(bb)+ .map(|b| tilecodec::blocked::compress_block_columns(b, rl))+ .collect(); let _ = tx.send(frames); }); self.inflight.push_back(rx);