/// The planet path: a byte of degree per node and a positional walk over each /// way's refs. See [`crate::chains`] for what it costs and what it buys. #[allow(clippy::too_many_arguments)] fn collapse_within_ways( input: &Path, spill_dir: &Path, spill_pts_dir: &Path, blobs: &[pbf::BlobLoc], blob_kinds: &[u8], index: &NodeIndex, coords: &[geom::Pt], stop: &Bitset, slots: u32, filter: RegionFilter, pool: &mut NamePool, timer: &mut crate::timing::PhaseTimer, ) -> Result { timer.switch("collapse-degrees"); let degree = chains::count_degrees(input, blobs, blob_kinds, index, coords, slots, filter)?; let spill = chains::Spill::split(spill_dir, spill_pts_dir); timer.switch("collapse-chains"); let built = chains::build( input, blobs, blob_kinds, index, coords, °ree, stop, slots, filter, pool, &spill, )?; report_way_blobs(blobs.len(), &built.kinds); // One byte of `kept` (eight nodes) per item, on the pool: each byte is a // pure function of its own nodes, so the split cannot show. let mut kept = Bitset::new(u64::from(slots)); { let n = u64::from(slots); let present = &index.present; let ends = &built.endpoints; let degree = °ree; par::install(|| { kept.bits.par_chunks_mut(1 << 16).enumerate().for_each(|(ci, chunk)| { for (j, byte) in chunk.iter_mut().enumerate() { let base = (((ci << 16) + j) as u64) * 8; let mut v = 0u8; for k in 0..8u64 { let i = base + k; if i < n && present.get(i) && chains::survives(ends, degree, stop, i as u32) { v |= 1 << k; } } *byte = v; } }); }); } drop(degree); let count = spill.chain_count()?; if count != built.chain_count { return Err(Error(format!( "the chain pass streamed {} chain(s) but chains.hdr holds {count}", built.chain_count ))); } Ok(Collapsed { spill, kept, lane_pool: built.lanes, chain_count: count, splits: built.splits, raw_edge_count: built.raw_edge_count, }) } /// The load-bearing measurement for re-reading the way blobs rather than caching /// them: if a ways-only pass had to touch most blobs, the cache would have been /// the cheaper of the two. fn report_way_blobs(total: usize, kinds: &[u8]) { let read = kinds.iter().filter(|k| *k & KIND_WAYS != 0).count(); println!( "The ways pass read {read} of {total} blob(s) ({:.1}%)", read as f64 * 100.0 / total.max(1) as f64 ); } /// Refuse a lane pool whose offsets would no longer fit a `u32`. /// /// Same failure mode as the counts above and one step worse: a rebased offset that /// wrapped onto [`NO_LANES`] reads back as "this edge has no lane data", so real /// turn lanes would disappear from a build that reported success. pub(crate) fn cap_lane_pool(len: usize) -> Result<()> { if len as u64 >= u64::from(NO_LANES) { return Err(Error(format!( "the lane pool reached {len} mask(s), past the {NO_LANES} an offset can address" ))); } Ok(()) } /// Refuse a count that no longer fits the `u32` ids the build and the on-disk /// format are built on. /// /// Every one of these is a silent truncation rather than a crash: a `u32` node id /// or chain id that wrapped would address a real but wrong record, so the build /// would succeed and the graph would be quietly wrong. The ceilings are ~2.5x /// away at planet scale, which is close enough to be worth naming. fn cap_u32(what: &str, n: u64) -> Result { u32::try_from(n).map_err(|_| { Error(format!( "{n} {what}(s) is past the {} this format can address", u32::MAX )) }) } /// Dense id -> final (Morton) id, for the nodes that survived. /// /// Indexed by rank over the surviving-node bitset rather than by dense id, so it /// costs four bytes per *survivor* instead of four per marked id — a difference of /// tens of gigabytes at planet scale, since the marked set includes every node any /// routable way ever mentions. struct FinalIds<'a> { kept: &'a Bitset, of_kept: &'a [u32], } impl FinalIds<'_> { #[inline] fn get(&self, dense: u32) -> u32 { self.of_kept[self.kept.dense(u64::from(dense)) as usize] } } /// Out-edges per surviving node, as a CSR in final id space. /// /// Built by two sequential scans of the chain spill, and it pays for itself three /// times over: [`twin_is_unique`] becomes a binary search in one node's group /// rather than in the whole edge array, the component scan walks it instead of a /// materialised edge array — which is what makes that scan affordable at all — and /// each write round takes its expected edge count straight out of `edge_ptr`. struct Csr { /// Directed edges before node `v`'s group. Length is `kept_count + 1`. edge_ptr: Vec, /// Each group's targets, ascending. targets: Vec, } impl Csr { #[inline] fn out(&self, v: u32) -> &[u32] { let (a, b) = (self.edge_ptr[v as usize], self.edge_ptr[v as usize + 1]); &self.targets[a as usize..b as usize] } fn edge_count(&self) -> u64 { *self.edge_ptr.last().expect("edge_ptr has a sentinel") } } /// One synthetic connector reattaching an isolated transit stop. /// /// Kept apart from the CSR rather than merged into it: there are a handful of /// these against billions of real edges, and inserting them would mean rebuilding /// `targets` to make room. #[derive(Clone, Copy)] struct Synth { source: u32, target: u32, dist_mm: u32, } fn build_csr( spill: &chains::Spill, kept_count: u32, ids: &FinalIds, timer: &mut crate::timing::PhaseTimer, ) -> Result { println!("Building the edge index over {kept_count} node(s)..."); let chain_total = spill.chain_count()?; // Both scans run on the pool over the mapped header file. // // Counting is integer addition into per-node counters, so atomic // increments give the same counts in any order. The scatter claims each // slot with an atomic cursor, so WHERE in its group a target lands depends // on scheduling — but every group is sorted right after (below), and a // sorted group of `u32`s has one arrangement, so `targets` comes out // identical to the serial scan's. let headers = spill.headers_mapped()?; if headers.len()? != chain_total { return Err(Error("chains.hdr changed size under the edge index".into())); } let mut edge_ptr = vec![0u64; kept_count as usize + 2]; { let counts = atomic_u64s(&mut edge_ptr); let errs: Vec> = par::map_ranges(chain_total as usize, 1 << 16, |r| { for ci in r { let c = headers.get(ci as u64)?; counts[ids.get(c.first) as usize + 1].fetch_add(1, Ordering::Relaxed); if !c.oneway { counts[ids.get(c.last) as usize + 1].fetch_add(1, Ordering::Relaxed); } } Ok(()) }); errs.into_iter().collect::>>()?; } for i in 0..=kept_count as usize { edge_ptr[i + 1] += edge_ptr[i]; } let total = edge_ptr[kept_count as usize]; timer.switch("edge-index-scatter"); let mut targets = vec![0u32; total as usize]; { let mut cursor: Vec = edge_ptr[..=kept_count as usize].to_vec(); let cursor = atomic_u64s(&mut cursor); let out = par::SyncPtr(targets.as_mut_ptr()); let errs: Vec> = par::map_ranges(chain_total as usize, 1 << 16, |r| { for ci in r { let c = headers.get(ci as u64)?; let (a, b) = (ids.get(c.first), ids.get(c.last)); let slot = cursor[a as usize].fetch_add(1, Ordering::Relaxed); // SAFETY: `fetch_add` hands every slot out exactly once, and the // counts above bound each group's slots to its own range. unsafe { out.write(slot as usize, b) }; if !c.oneway { let slot = cursor[b as usize].fetch_add(1, Ordering::Relaxed); // SAFETY: as above. unsafe { out.write(slot as usize, a) }; } } Ok(()) }); errs.into_iter().collect::>>()?; } edge_ptr.truncate(kept_count as usize + 1); // Per group, so `twin_is_unique` can binary-search it. Sorting the whole array // would be one comparison sort over a billion elements; this is millions of // sorts over a handful each. // // Parallelised by carving `targets` into one disjoint slice per WORKER, each // covering a contiguous span of nodes, rather than one slice per group: a // `&mut [u32]` is 16 bytes, and a planet graph has around a billion groups, so // collecting them all would cost more memory than the graph. Sorting `u32`s has // one answer, so nothing here depends on the order the work happens in. let n = kept_count as usize; let workers = par::threads().min(n).max(1); let mut pieces: Vec<(usize, usize, &mut [u32])> = Vec::with_capacity(workers); let mut rest: &mut [u32] = &mut targets; let mut consumed = 0usize; for w in 0..workers { let lo = n * w / workers; let hi = n * (w + 1) / workers; let end = edge_ptr[hi] as usize; let (mine, tail) = rest.split_at_mut(end - consumed); pieces.push((lo, hi, mine)); rest = tail; consumed = end; } let ptr = &edge_ptr; timer.switch("edge-index-sort"); eprintln!("Sorting edge groups across {workers} worker(s)..."); par::install(|| { pieces.par_iter_mut().for_each(|(lo, hi, buf)| { let base = ptr[*lo] as usize; for v in *lo..*hi { let (a, b) = (ptr[v] as usize - base, ptr[v + 1] as usize - base); buf[a..b].sort_unstable(); } }); }); println!("{total} directed edge(s) from chains"); Ok(Csr { edge_ptr, targets }) } /// A `Vec` as shared atomic counters for the length of the borrow. fn atomic_u64s(v: &mut [u64]) -> &[std::sync::atomic::AtomicU64] { // SAFETY: `AtomicU64` has the size and bit validity of `u64` and, on every // target this tool builds for, its alignment too (asserted below); the // exclusive borrow is held for the view's lifetime. const _: () = assert!( std::mem::align_of::() == std::mem::align_of::() ); unsafe { &*(v as *mut [u64] as *const [std::sync::atomic::AtomicU64]) } } /// True when `target`'s edge range holds exactly one edge pointing back at /// `source`. /// /// `graph.rs` resolves a reversed edge by scanning the target's range for the /// first edge whose own target is the source, so with two parallel roads between /// the same pair of nodes it could pick the wrong twin and draw the wrong /// polyline. /// /// The synthetic connectors have to be counted too, even though they are not in /// the CSR, because the reader does not know they are synthetic. In practice they /// never collide — a connector runs from a component too small to be routable into /// one that is, so no chain can join the same pair — but counting them makes that a /// measured fact rather than an assumption, and it is a binary search in a list of /// a few thousand. fn twin_is_unique(csr: &Csr, synth: &[Synth], source: u32, target: u32) -> bool { let group = csr.out(target); let lo = group.partition_point(|t| *t < source); let hi = group.partition_point(|t| *t <= source); let from_synth = { let key = (target, source); let a = synth.partition_point(|s| (s.source, s.target) < key); let b = synth.partition_point(|s| (s.source, s.target) <= key); b - a }; hi - lo + from_synth == 1 } fn pass1_blob( state: &mut Pass1, block: &pbf::PrimitiveBlock, filter: RegionFilter, mask: &AtomicBits, ) -> Result { let mut kinds = 0u8; let mut mark = |id: i64| { if id >= 0 && (id as u64) < BITSET_SIZE { if mask.set(id as u64) { state.marked += 1; } // The rank index is sized from the largest id actually marked, // not from BITSET_SIZE: indexing all 20 G nominal ids would cost // 313 MB to describe blocks no node reaches. state.max_id = state.max_id.max(id as u64); } }; visit_block( block, KIND_NODES | KIND_WAYS, &mut kinds, &mut |el: Element| { match el { Element::Node(n) => { if tags::is_stop_node( n.tags.get_str("highway"), n.tags.get_str("railway"), n.tags.get_str("public_transport"), ) { // A stop outside the region never enters the graph. The // node carries its own coordinates, so this is the one // place the region filter can apply without a location // table. if crate::bbox::keep_e7(filter.as_ref(), n.lat_e7, n.lon_e7) { mark(n.id); } } } Element::Way(w) => { if tags::get_hw_id(w.tags.get_str("highway")) != 0 { for r in w.refs { mark(*r); } } } Element::Relation(_) => {} } Ok(()) }, )?; Ok(kinds) } /// Everything the graph takes from one routable way's tags. pub(crate) struct WayAttrs { /// Chunk-local name id, or `u32::MAX` for an unnamed way. pub name: u32, pub type_: u8, pub speed_limit: u8, pub oneway: bool, pub fwd_lane_off: u32, pub fwd_lane_count: u16, pub bwd_lane_off: u32, pub bwd_lane_count: u16, } include!("graph_build_part2_s1.rs");