/// Write the pack in `rounds` source-partitioned rounds. /// /// # Why rounds, and not computed offsets /// /// The obvious alternative is to compute each edge's index and `pwrite` it into /// place. That is sound for `edges.bin` — fixed 14-byte records in a pre-sized /// file — but wrong for the two blob files. The reader derives a polyline's length /// from the next offset entry and a lane run's from the next index entry, so /// `intermediate.bin` and `lanes.bin` are forced into edge-index order with /// monotonic offsets, and writing a blob at a computed offset would mean knowing /// every earlier blob's length first: a per-edge length array, a prefix sum and a /// second encode pass. /// /// Partitioning by source instead makes every write an append. Each round takes a /// contiguous range of final node ids, scans the chain spill for the edges whose /// *source* falls in that range, sorts that buffer, and writes both blobs, the /// presence bitmap, the offset tables, `edges.bin` and `nodes.bin` sequentially. /// Offsets come out monotonic by construction, `nodes.bin`'s running edge counter /// works because the rounds ascend, and the `(source, target)` grouping the reader /// and [`twin_is_unique`] depend on is restored within each round. /// /// The cost is `rounds` sequential reads of the spill instead of one. #[allow(clippy::too_many_arguments)] fn write_graph( out_dir: &Path, node_coords: &[geom::Pt], spill: &chains::Spill, lane_pool: &[u16], csr: &Csr, synth: &[Synth], ids: &FinalIds, rounds: u32, dem: Option<&crate::dem::Dem>, timer: &mut crate::timing::PhaseTimer, ) -> Result { let kept = node_coords.len() as u32; let edge_count = csr.edge_count() + synth.len() as u64; // `nodes.bin`'s `edge_ptr` is a `u32`, so an edge count past that ceiling would // wrap and point a node at another node's edge range. cap_u32("directed edge", edge_count)?; println!("Writing {edge_count} edge(s) in {rounds} round(s)..."); let chain_total = spill.chain_count()?; let mut nodes_blocked = ParBlocked::create(12, &out_dir.join("nodes.blocked.tmp")) .map_err(|e| Error(format!("cannot stage nodes.bin: {e}")))?; let mut edges_out = EdgeFile::create(out_dir, "edges.bin", edge_count)?; // `intermediate.bin` puts its blob at offset 0 and everything that indexes it // in a trailer, so it is written strictly forwards with no scratch file at all // (see [`GeomFile`]). `lanes.bin` is sparse and so cannot size its index up // front; its blob streams to scratch instead and the index is written in front // of it. let mut inter = GeomFile::create(out_dir, "intermediate.bin", edge_count)?; let mut lanes = LaneFile::create(out_dir, "lanes.bin")?; let mut buffer: Vec = Vec::new(); let points = spill.points()?; let pmap = chains::points_map(&points); let mut edge_ptr: u64 = 0; let mut geometry_edges: u64 = 0; let mut reversed_edges: u64 = 0; let mut census = Census::default(); // One assignment scan instead of one scan per round: every chain's round // is a pure function of its endpoints' final ids, so a single pass over // the mapped headers records each chain's index per round and every round // gathers only its chains. Chain order within a round is ascending index — // the order the old per-round scan visited — so the buffer, the sort, and // everything downstream are identical. // // Parallel: fixed contiguous chunks of chain indices, each producing its // own per-round lists, concatenated in chunk order — so every list is still // ascending and identical to the serial scan's. timer.switch("write-scan"); let headers = spill.headers_mapped()?; let chain_total_now = headers.len()?; if chain_total_now != chain_total { return Err(Error(format!( "chains.hdr holds {chain_total_now} chain(s), expected {chain_total}" ))); } let round_lo = |round: u32| (u64::from(kept) * u64::from(round) / u64::from(rounds)) as u32; let by_round: Vec> = { let n_chunks = (chain_total as usize).div_ceil(SCAN_CHUNK); let chunk_ids: Vec = (0..n_chunks).collect(); let parts: Vec>>> = par::map_chunks(&chunk_ids, 1, |_, ch| { let c0 = (ch[0] * SCAN_CHUNK) as u64; let c1 = (c0 + SCAN_CHUNK as u64).min(chain_total); let mut mine: Vec> = vec![Vec::new(); rounds as usize]; for ci in c0..c1 { let c = headers.get(ci)?; let (a, b) = (ids.get(c.first), ids.get(c.last)); // A chain enters a round when the old scan would have buffered an // edge for it there: source endpoint in range, or (two-way) target // endpoint in range. One entry per qualifying round — a chain whose // endpoints straddle a boundary is gathered twice, once per round, // exactly as the old scan buffered it twice. for round in 0..rounds { let (lo, hi) = (round_lo(round), round_lo(round + 1)); if (lo..hi).contains(&a) || (!c.oneway && (lo..hi).contains(&b)) { mine[round as usize].push(ci as u32); } } } Ok(mine) }); let mut by_round: Vec> = vec![Vec::new(); rounds as usize]; let mut sizes = vec![0usize; rounds as usize]; for p in parts.iter().filter_map(|p| p.as_ref().ok()) { for (r, v) in p.iter().enumerate() { sizes[r] += v.len(); } } for (r, v) in by_round.iter_mut().enumerate() { v.reserve_exact(sizes[r]); } for p in parts { for (r, v) in p?.into_iter().enumerate() { by_round[r].extend_from_slice(&v); } } by_round }; for round in 0..rounds { // Split by node count. Contiguous and ascending, so the four output streams // stay append-only across the whole run. let lo = round_lo(round); let hi = round_lo(round + 1); timer.switch("write-scan"); // Gather: this round's chains in ascending index order — the visit // order of the old per-round scan. Parallel over fixed chunks of the // round's list, concatenated in chunk order, so the buffer is the // serial one element for element. buffer.clear(); let gathered: Vec>> = par::map_chunks(&by_round[round as usize], SCAN_CHUNK, |_, chunk| { let mut out = Vec::with_capacity(chunk.len() + chunk.len() / 2); for &ci in chunk { let c = headers.get(u64::from(ci))?; let (a, b) = (ids.get(c.first), ids.get(c.last)); if (lo..hi).contains(&a) { out.push(chain_edge(&c, ci, a, b, false)); } if !c.oneway && (lo..hi).contains(&b) { out.push(chain_edge(&c, ci, b, a, true)); } } Ok(out) }); let synth_lo = synth.partition_point(|s| s.source < lo); let synth_hi = synth.partition_point(|s| s.source < hi); let gathered_len: usize = gathered.iter().map(|g| g.as_ref().map_or(0, Vec::len)).sum(); buffer.reserve_exact(gathered_len + (synth_hi - synth_lo)); for g in gathered { buffer.extend_from_slice(&g?); } for s in &synth[synth_lo..synth_hi] { buffer.push(TmpEdge { source: s.source, target: s.target, dist_mm: s.dist_mm, name_offset: NO_NAME, type_: RECONNECT_TYPE, speed_limit: RECONNECT_SPEED, lane_off: NO_LANES, lane_count: 0, chain: NO_CHAIN, chain_rev: false, pts_start: 0, pts_len: 0, }); } // The CSR already knows how many edges this range owns. Checking against it // catches both an edge landing in the wrong round and an edge whose source // is not a surviving node — which is what the old // `assert_eq!(cursor, edges.len())` was for, now checked per round and // before anything is written rather than after everything is. let expected = csr.edge_ptr[hi as usize] - csr.edge_ptr[lo as usize] + (synth_hi - synth_lo) as u64; if buffer.len() as u64 != expected { return Err(Error(format!( "round {round} covering node(s) {lo}..{hi} collected {} edge(s), \ but the edge index says {expected}", buffer.len() ))); } // Grouped by source for the reader, then by target so a node's parallel // edges are adjacent, then by chain so the order is total. // // This was a STABLE sort on `(source, target, chain)` over a buffer built // in chain order. The only elements that key leaves tied and that still // differ are the two directions of a two-way self-loop chain — same // source, target and chain — which the gather emits forward // (`chain_rev == false`) first. Appending `chain_rev` to the key breaks // exactly that tie the same way, and anything still tied is a // byte-identical duplicate (a repeated connector), whose relative order // cannot show. So the order is total over distinguishable elements and an // UNSTABLE parallel in-place sort yields the stable sort's sequence — with // no merge buffer, which at planet scale would be another 17 GB. par::install(|| { buffer.par_sort_unstable_by_key(|e| (e.source, e.target, e.chain, e.chain_rev)) }); // --- write-write: parallel bake, in-order drain ------------------------ timer.switch("write-write"); write_round( &buffer, lo, hi, edge_ptr, node_coords, lane_pool, csr, synth, pmap, &mut WriteSinks { nodes: &mut nodes_blocked, edges: &mut edges_out, inter: &mut inter, lanes: &mut lanes, census: &mut census, geometry_edges: &mut geometry_edges, reversed_edges: &mut reversed_edges, }, round, rounds, )?; edge_ptr += buffer.len() as u64; } drop(buffer); if edge_ptr != edge_count { return Err(Error(format!( "wrote {edge_ptr} edge(s) into a file sized for {edge_count}" ))); } finish_pack( out_dir, kept, edge_count, edge_ptr, geometry_edges, reversed_edges, nodes_blocked, edges_out, inter, lanes, census, node_coords, dem, ) } /// Finish the four output streams and the metadata: the tail of `write_graph` /// after the last round drains. Unchanged from the serial loop — the drain /// above stages identical bytes, so everything downstream (sentinel, sanity /// parses, metadata, elevation) is untouched. #[allow(clippy::too_many_arguments)] fn finish_pack( out_dir: &Path, kept: u32, edge_count: u64, edge_ptr: u64, geometry_edges: u64, reversed_edges: u64, nodes_blocked: ParBlocked, edges_out: EdgeFile, inter: GeomFile, lanes: LaneFile, census: Census, node_coords: &[geom::Pt], dem: Option<&crate::dem::Dem>, ) -> Result { let mut nodes_blocked = nodes_blocked; // Trailing sentinel so `graph.rs` can read node(v + 1).edge_ptr as the end of // node v's edge range. let mut sentinel = [0u8; 12]; sentinel[8..12].copy_from_slice(&(edge_ptr as u32).to_le_bytes()); nodes_blocked .push(&sentinel) .map_err(|e| Error(format!("cannot stage node sentinel: {e}")))?; // Streamed straight into the file (no in-memory copy of the section), // then checked against the mapped result. { let mut out = BufWriter::with_capacity(WRITE_BUF_BYTES, create(&out_dir.join("nodes.bin"))?); nodes_blocked .finish_into(&mut out) .map_err(|e| Error(format!("cannot finish nodes.bin: {e}")))?; out.flush().map_err(io_err)?; } let nodes_map = crate::mem::Mapped::open(&out_dir.join("nodes.bin")).map_err(Error)?; // Sanity: the blocked section must unpack to exactly the pushed records. let parsed = tilecodec::blocked::Blocked::parse(&nodes_map).map_err(|e| { Error(format!( "freshly blocked nodes.bin does not parse: {e}" )) })?; // Sanity: the blocked section must unpack to exactly the pushed records. if parsed.unpacked_len() != (u64::from(kept) + 1) * 12 { return Err(Error(format!( "nodes.bin unpacks to {} byte(s) ({} 12-byte record(s)), not the {} record(s) \ pushed ({} byte(s))", parsed.unpacked_len(), parsed.unpacked_len() / 12, u64::from(kept) + 1, (u64::from(kept) + 1) * 12 ))); } drop(nodes_map); let escape_count = edges_out.escapes.len() as u64; let named_edges = edges_out.name_offsets.len() as u64; let edges_bytes = edges_out.finish(edge_count)?; let intermediate_bytes = inter.finish(edge_count)?; lanes.finish()?; finish_pack_tail( out_dir, kept, edge_count, escape_count, named_edges, node_coords, dem, )?; Ok(Written { edge_count, geometry_edges, reversed_edges, intermediate_bytes, edges_bytes, escape_count, named_edges, census, }) } /// Metadata + elevation tail of `write_graph`, after every stream finishes. /// Split out so the round loop above reads as plan/bake/drain without the /// file-assembly tail inline. Byte-identical: moved whole, order untouched. #[allow(clippy::too_many_arguments)] fn finish_pack_tail( out_dir: &Path, kept: u32, edge_count: u64, escape_count: u64, named_edges: u64, node_coords: &[geom::Pt], dem: Option<&crate::dem::Dem>, ) -> Result<()> { // metadata.bin's magic + version let the device refuse a pack directory // holding files from two different vintages instead of misreading it, and its // counts are what every other file's length is validated against. let mut meta = create(&out_dir.join("metadata.bin"))?; meta.write_all(&GRAPH_MAGIC.to_le_bytes()).map_err(io_err)?; meta.write_all(&GRAPH_VERSION.to_le_bytes()).map_err(io_err)?; meta.write_all(&u64::from(kept).to_le_bytes()).map_err(io_err)?; meta.write_all(&edge_count.to_le_bytes()).map_err(io_err)?; meta.write_all(&escape_count.to_le_bytes()).map_err(io_err)?; meta.write_all(&named_edges.to_le_bytes()).map_err(io_err)?; meta.flush().map_err(io_err)?; // elevation.bin: `i16[node_count]` metres, one per node in final-id order — an OPTIONAL sidecar // like road_names.bin/lanes.bin, so a build without a DEM simply omits it and the device reads // every node's elevation as 0. `node_coords` is already indexed by final node id, so a single // forward pass matches the order `nodes.bin` was written in. Blocked like // nodes/edges under the v7 layout (magic-dispatched on read, so absence // still reads as all-zero). if let Some(dem) = dem { let mut elev_blocked = tilecodec::blocked::BlockedStreamWriter::create( 2, &out_dir.join("elevation.blocked.tmp"), ) .map_err(|e| Error(format!("cannot stage elevation.bin: {e}")))?; for &(lat_e7, lon_e7) in node_coords { let lat = f64::from(lat_e7) * 1e-7; let lon = f64::from(lon_e7) * 1e-7; let metres = dem.sample_metres(lon, lat).unwrap_or(0); elev_blocked .push(&metres.to_le_bytes()) .map_err(|e| Error(format!("cannot stage elevation record: {e}")))?; } let elev_bytes = elev_blocked .finish() .map_err(|e| Error(format!("cannot finish elevation.bin: {e}")))?; std::fs::write(out_dir.join("elevation.bin"), &elev_bytes).map_err(io_err)?; } Ok(()) }