maps: transit block cache by bytes, O(1) block decode, --zsec-block (#planet5)
- Commit
e05d093fbc4bf96a922b9123b23d4827702682de- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 14:59 UTC (21 days ago)
- Parents
429b1d1
Follow-ups from re-measuring compressed transit hot sections for the ~50 GB target: - The per-thread transit block cache is bounded by decoded bytes (16 MiB) instead of a block count, so smaller blocks get a proportionally larger cache. - A cache miss decodes through tilecodec::zsec::block_of (O(1)) instead of re-validating the section's whole offset table. - transit_planet5 --zsec-block BYTES sets the block size (default 64 KiB), for the measurement below. Result: compressing the RAPTOR tables still fails the speed bar even with these fixes (60 world queries timed out at >28 s/query against 2.2 s for raw v7, at 64 KiB and 16 KiB blocks), so the shipped default stays cold sections only (owner: transit hot sections stay raw). Verified: cargo test -p offlinerouter (93 pass).
Diff
maps/src/main/rust/src/transit_zsec.rs
diff --git a/maps/src/main/rust/src/transit_zsec.rs b/maps/src/main/rust/src/transit_zsec.rsindex 04587784a1..a936938102 100644--- a/maps/src/main/rust/src/transit_zsec.rs+++ b/maps/src/main/rust/src/transit_zsec.rs@@ -10,12 +10,14 @@ static NEXT_TRANSIT_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); -/// Decoded blocks a thread keeps across all indexes (64 KiB each: 16 MiB per thread).-const TRANSIT_BLOCK_CACHE: usize = 256;+/// Decoded bytes a thread keeps across all indexes (16 MiB per thread, whatever the block size).+const TRANSIT_CACHE_BYTES: usize = 16 << 20; struct TransitBlockCache { map: std::collections::HashMap<(u64, u8, u32), std::rc::Rc<Vec<u8>>>, order: std::collections::VecDeque<(u64, u8, u32)>,+ /// Decoded bytes held in `map`.+ bytes: usize, /// The last block each section touched: sequential walks (a varint stream, a polyline) /// stay in one block for thousands of reads, interleaved across sections. last: Vec<Option<((u64, u8, u32), std::rc::Rc<Vec<u8>>)>>,@@ -25,6 +27,7 @@ thread_local! { static TRANSIT_BLOCKS: std::cell::RefCell<TransitBlockCache> = std::cell::RefCell::new(TransitBlockCache { map: std::collections::HashMap::new(), order: std::collections::VecDeque::new(),+ bytes: 0, last: vec![None; 32], }); }@@ -47,13 +50,13 @@ impl TransitIndex { let (off, len, _) = self.zsec[section].expect("a ZSEC section"); let bytes = unsafe { std::slice::from_raw_parts(self.base.add(off), len) }; // Validated at open; a decode failure here is corruption the open could not see.- let block = tilecodec::zsec::ZSec::parse(bytes)- .and_then(|z| z.block(b as usize))- .unwrap_or_default();+ let block = tilecodec::zsec::block_of(bytes, b as usize).unwrap_or_default(); let v = std::rc::Rc::new(block);- if c.order.len() >= TRANSIT_BLOCK_CACHE {- if let Some(old) = c.order.pop_front() {- c.map.remove(&old);+ c.bytes += v.len();+ while c.bytes > TRANSIT_CACHE_BYTES {+ let Some(old) = c.order.pop_front() else { break };+ if let Some(o) = c.map.remove(&old) {+ c.bytes -= o.len(); } } c.order.push_back(key);
scripts/maps/tile_build/src/bin/transit_planet5.rs
diff --git a/scripts/maps/tile_build/src/bin/transit_planet5.rs b/scripts/maps/tile_build/src/bin/transit_planet5.rsindex ddb7497f45..ef1f1595a6 100644--- a/scripts/maps/tile_build/src/bin/transit_planet5.rs+++ b/scripts/maps/tile_build/src/bin/transit_planet5.rs@@ -84,6 +84,7 @@ fn main() -> ExitCode { let args: Vec<String> = std::env::args().collect(); let mut tol = 1.0f64; let mut zsec: Vec<usize> = COLD_SECTIONS.to_vec();+ let mut block = tilecodec::zsec::ZSEC_BLOCK; let mut pos = Vec::new(); let mut i = 1; while i < args.len() {@@ -92,6 +93,11 @@ fn main() -> ExitCode { i += 2; continue; }+ if args[i] == "--zsec-block" {+ block = args[i + 1].parse().expect("--zsec-block BYTES");+ i += 2;+ continue;+ } if args[i] == "--zsec" { zsec = match args[i + 1].as_str() { "all" => (0..DEVICE_SECTIONS).collect(),@@ -109,7 +115,7 @@ fn main() -> ExitCode { eprintln!("usage: transit_planet5 IN.transit OUT.transit [--shape-tol-m M] [--zsec cold|all|none|i,j,..]"); return ExitCode::from(2); }- match run(&pos[0], &pos[1], tol, &zsec) {+ match run(&pos[0], &pos[1], tol, &zsec, block) { Ok(()) => ExitCode::SUCCESS, Err(e) => { eprintln!("transit_planet5: {e}");@@ -118,7 +124,7 @@ fn main() -> ExitCode { } } -fn run(input: &str, output: &str, tol: f64, zsec: &[usize]) -> Result<(), String> {+fn run(input: &str, output: &str, tol: f64, zsec: &[usize], block: usize) -> Result<(), String> { let f = File::open(input).map_err(|e| format!("open {input}: {e}"))?; let m = unsafe { memmap2::Mmap::map(&f) }.map_err(|e| e.to_string())?; if m.len() < HEADER_LEN || u32_at(&m, 0) != tilecodec::mamaps::archive::TRANSIT_MAGIC {@@ -293,7 +299,7 @@ fn run(input: &str, output: &str, tol: f64, zsec: &[usize]) -> Result<(), String if s.is_empty() || !zsec.contains(&i) { (s.clone(), false) } else {- (tilecodec::zsec::write_zsec(s, tilecodec::zsec::ZSEC_BLOCK, 19), true)+ (tilecodec::zsec::write_zsec(s, block, 19), true) } }) .collect();