gtfs_ingest: extract finish_to helpers under the 500-line limit
- Commit
aeea3e5181b70f016ca7b203a7c0343951723023- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:33 UTC (4 days ago)
- Parents
ddb11c3
index_part3.rs (503 lines) exceeded the 500-line rustFileLength limit because IndexBuilder::finish_to is one large method; the established include! split cannot cut inside a method. Extract three pure statement blocks from finish_to into private free helpers in index_part3_s1.rs, spliced into the same module via include! in index.rs: build_transfers (footpath transfer grid), build_services_and_exceptions (services plus the sorted exception CSR) and build_spatial_grid (sparse spatial CSR). Each block moves verbatim, in the same order, with the same debug assertions and no error paths; the call sites only bind the returned section vectors and totals, so the emitted pack bytes are identical. Helpers take slices/owned inputs they already used and return exactly the locals the rest of finish_to consumes; no public API changes. index_part3.rs drops 503 -> 383 lines; the new helper file is 170 lines. Verified: cargo fmt -p gtfs_ingest -- --check clean and cargo test -p gtfs_ingest --locked green (85 lib tests passed, plus binary tests).
Diff
scripts/maps/gtfs_ingest/src/index.rs
diff --git a/scripts/maps/gtfs_ingest/src/index.rs b/scripts/maps/gtfs_ingest/src/index.rsindex 957de093d7..e54b12a6a9 100644--- a/scripts/maps/gtfs_ingest/src/index.rs+++ b/scripts/maps/gtfs_ingest/src/index.rs@@ -442,6 +442,7 @@ impl<'a> From<&'a FeedInput<'a>> for FeedView<'a> { include!("index_part1.rs"); include!("index_part2.rs"); include!("index_part3.rs");+include!("index_part3_s1.rs"); include!("index_part4.rs"); include!("index_part5.rs"); include!("index_part6.rs");
scripts/maps/gtfs_ingest/src/index_part3.rs
diff --git a/scripts/maps/gtfs_ingest/src/index_part3.rs b/scripts/maps/gtfs_ingest/src/index_part3.rsindex 98ed8bda25..eae5c042fe 100644--- a/scripts/maps/gtfs_ingest/src/index_part3.rs+++ b/scripts/maps/gtfs_ingest/src/index_part3.rs@@ -223,144 +223,24 @@ impl IndexBuilder { ); } - // --- Footpath transfers via a coarse spatial grid (cross-feed for free) ---- // A flat `(cell, stop)` vector sorted once, not a `HashMap` of buckets: on a- // world pack the map is millions of cells each holding a tiny `Vec`, whose- // headers and allocations cost far more than the stop ids in them. Sorting- // by `(cell, stop)` keeps each cell's stops ascending, which is the order the- // old bucket pushes produced and which the distance sort below tie-breaks on.- let xcell = |lat_e7: i32, lon_e7: i32| -> (i32, i32) {- (- (lat_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32,- (lon_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32,- )- };- let mut xgrid: Vec<((i32, i32), u32)> = (0..stop_count)- .map(|i| (xcell(stop_lat[i], stop_lon[i]), i as u32))- .collect();- xgrid.sort_unstable();- let xbucket = |key: (i32, i32)| -> &[((i32, i32), u32)] {- let lo = xgrid.partition_point(|&(k, _)| k < key);- let hi = xgrid.partition_point(|&(k, _)| k <= key);- &xgrid[lo..hi]- };- let mut transfer_total = 0usize;- let mut sec_transfers = Vec::new();- let mut sec_transfers_idx = Vec::new();- let mut tacc: u32 = 0;- for i in 0..stop_count {- append_u32(&mut sec_transfers_idx, tacc);- let (cx, cy) = xcell(stop_lat[i], stop_lon[i]);- let mut cand: Vec<(u32, f64)> = Vec::new();- for dx in -1..=1 {- for dy in -1..=1 {- for &(_, j) in xbucket((cx + dx, cy + dy)) {- if j as usize == i {- continue;- }- let d =- dist_m(stop_lat[i], stop_lon[i], stop_lat[j as usize], stop_lon[j as usize]);- if d <= MAX_TRANSFER_M {- cand.push((j, d));- }- }- }- }- // Stable, so equidistant candidates keep the dx/dy/stop order above and- // `truncate` drops a deterministic tail.- cand.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));- cand.truncate(MAX_TRANSFERS_PER_STOP);- for (j, d) in cand {- let secs = (d / WALK_SPEED_M_S).ceil() as u32;- append_u32(&mut sec_transfers, j);- append_u32(&mut sec_transfers, secs);- tacc += 1;- transfer_total += 1;- }- }- append_u32(&mut sec_transfers_idx, tacc);+ let (sec_transfers, sec_transfers_idx, transfer_total) =+ build_transfers(&stop_lat, &stop_lon); - // --- Services + exceptions ---- let mut sec_services = Vec::new();- for i in 0..svc_mask.len() {- sec_services.push(svc_mask[i]);- sec_services.extend_from_slice(&[0u8, 0, 0]);- append_u32(&mut sec_services, svc_start[i]);- append_u32(&mut sec_services, svc_end[i]);- }- // --- Exceptions: sorted by (service_idx, date), one row per pair, with a- // CSR index so the device does a range lookup + binary search instead of a- // full scan per trip. `add_feed` already stable-sorted each feed's slice- // and a feed's services are a contiguous index range, so the whole array- // is sorted; for duplicate (service, date) rows the last in CSV order- // wins — matching the old reader, which scanned every exception without- // breaking out. ---- debug_assert!(- exceptions.windows(2).all(|w| (w[0].0, w[0].1) <= (w[1].0, w[1].1)),- "per-feed exception slices did not concatenate into a sorted array"- );- let mut exc_unique: Vec<(u32, u32, u32)> = Vec::with_capacity(exceptions.len());- for e in exceptions {- match exc_unique.last_mut() {- Some(last) if last.0 == e.0 && last.1 == e.1 => *last = e,- _ => exc_unique.push(e),- }- }- let mut sec_exceptions = Vec::new();- let mut sec_exceptions_idx = Vec::new();- let mut eacc: u32 = 0;- let mut ei = 0usize;- for s in 0..svc_mask.len() as u32 {- append_u32(&mut sec_exceptions_idx, eacc);- while let Some(&(sidx, date, added)) = exc_unique.get(ei) {- if sidx != s {- break;- }- append_u32(&mut sec_exceptions, sidx);- append_u32(&mut sec_exceptions, date);- append_u32(&mut sec_exceptions, added);- eacc += 1;- ei += 1;- }- }- append_u32(&mut sec_exceptions_idx, eacc);+ let (sec_services, sec_exceptions, sec_exceptions_idx) =+ build_services_and_exceptions(&svc_mask, &svc_start, &svc_end, exceptions); // --- Spatial grid (sparse CSR keyed by cell id) --- let grid_lat0 = min_lat; let grid_lon0 = min_lon;- let grid_cell_e7 = (GRID_CELL_DEG * 1e7) as u32;- let cell_col = |lon_e7: i32| -> i64 {- ((lon_e7 as i64 - grid_lon0 as i64) / grid_cell_e7 as i64).max(0)- };- let cell_row = |lat_e7: i32| -> i64 {- ((lat_e7 as i64 - grid_lat0 as i64) / grid_cell_e7 as i64).max(0)- };- let grid_cols = (cell_col(max_lon) + 1) as u32;- let grid_rows = (cell_row(max_lat) + 1) as u32;- // Sorted by `(cell_id, stop)`, which is exactly the two orderings the device- // requires: GRID_CELL_IDS ascending, because the reader binary-searches it,- // and each cell's GRID_STOPS run stop-ascending.- let mut grid: Vec<(u32, u32)> = (0..stop_count)- .map(|i| {- let row = cell_row(stop_lat[i]) as u32;- let col = cell_col(stop_lon[i]) as u32;- (row * grid_cols + col, i as u32)- })- .collect();- grid.sort_unstable();- let mut sec_grid_cell_ids = Vec::new();- let mut sec_grid_cell_off = Vec::new();- let mut sec_grid_stops = Vec::with_capacity(stop_count * 4);- let mut grid_cell_count = 0usize;- for (k, &(cid, stop)) in grid.iter().enumerate() {- if k == 0 || cid != grid[k - 1].0 {- append_u32(&mut sec_grid_cell_ids, cid);- append_u32(&mut sec_grid_cell_off, k as u32);- grid_cell_count += 1;- }- append_u32(&mut sec_grid_stops, stop);- }- append_u32(&mut sec_grid_cell_off, grid.len() as u32);+ let (+ sec_grid_cell_ids,+ sec_grid_cell_off,+ sec_grid_stops,+ grid_cols,+ grid_rows,+ grid_cell_e7,+ grid_cell_count,+ ) = build_spatial_grid(&stop_lat, &stop_lon, grid_lat0, grid_lon0, max_lat, max_lon); // --- Feeds --- let mut sec_feeds = Vec::with_capacity(feed_name_offs.len() * 4);
scripts/maps/gtfs_ingest/src/index_part3_s1.rs
diff --git a/scripts/maps/gtfs_ingest/src/index_part3_s1.rs b/scripts/maps/gtfs_ingest/src/index_part3_s1.rsnew file mode 100644index 0000000000..b7cc43820e--- /dev/null+++ b/scripts/maps/gtfs_ingest/src/index_part3_s1.rs@@ -0,0 +1,170 @@+/// Footpath transfers, services/exceptions and the spatial grid for+/// [`IndexBuilder::finish_to`]: pure section builders extracted verbatim+/// from that method (same statement order, same debug assertions).++fn build_transfers(stop_lat: &[i32], stop_lon: &[i32]) -> (Vec<u8>, Vec<u8>, usize) {+ let stop_count = stop_lat.len();+ // --- Footpath transfers via a coarse spatial grid (cross-feed for free) ---+ // A flat `(cell, stop)` vector sorted once, not a `HashMap` of buckets: on a+ // world pack the map is millions of cells each holding a tiny `Vec`, whose+ // headers and allocations cost far more than the stop ids in them. Sorting+ // by `(cell, stop)` keeps each cell's stops ascending, which is the order the+ // old bucket pushes produced and which the distance sort below tie-breaks on.+ let xcell = |lat_e7: i32, lon_e7: i32| -> (i32, i32) {+ (+ (lat_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32,+ (lon_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32,+ )+ };+ let mut xgrid: Vec<((i32, i32), u32)> = (0..stop_count)+ .map(|i| (xcell(stop_lat[i], stop_lon[i]), i as u32))+ .collect();+ xgrid.sort_unstable();+ let xbucket = |key: (i32, i32)| -> &[((i32, i32), u32)] {+ let lo = xgrid.partition_point(|&(k, _)| k < key);+ let hi = xgrid.partition_point(|&(k, _)| k <= key);+ &xgrid[lo..hi]+ };+ let mut transfer_total = 0usize;+ let mut sec_transfers = Vec::new();+ let mut sec_transfers_idx = Vec::new();+ let mut tacc: u32 = 0;+ for i in 0..stop_count {+ append_u32(&mut sec_transfers_idx, tacc);+ let (cx, cy) = xcell(stop_lat[i], stop_lon[i]);+ let mut cand: Vec<(u32, f64)> = Vec::new();+ for dx in -1..=1 {+ for dy in -1..=1 {+ for &(_, j) in xbucket((cx + dx, cy + dy)) {+ if j as usize == i {+ continue;+ }+ let d =+ dist_m(stop_lat[i], stop_lon[i], stop_lat[j as usize], stop_lon[j as usize]);+ if d <= MAX_TRANSFER_M {+ cand.push((j, d));+ }+ }+ }+ }+ // Stable, so equidistant candidates keep the dx/dy/stop order above and+ // `truncate` drops a deterministic tail.+ cand.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));+ cand.truncate(MAX_TRANSFERS_PER_STOP);+ for (j, d) in cand {+ let secs = (d / WALK_SPEED_M_S).ceil() as u32;+ append_u32(&mut sec_transfers, j);+ append_u32(&mut sec_transfers, secs);+ tacc += 1;+ transfer_total += 1;+ }+ }+ append_u32(&mut sec_transfers_idx, tacc);+ (sec_transfers, sec_transfers_idx, transfer_total)+}++fn build_services_and_exceptions(+ svc_mask: &[u8],+ svc_start: &[u32],+ svc_end: &[u32],+ exceptions: Vec<(u32, u32, u32)>,+) -> (Vec<u8>, Vec<u8>, Vec<u8>) {+ // --- Services + exceptions ---+ let mut sec_services = Vec::new();+ for i in 0..svc_mask.len() {+ sec_services.push(svc_mask[i]);+ sec_services.extend_from_slice(&[0u8, 0, 0]);+ append_u32(&mut sec_services, svc_start[i]);+ append_u32(&mut sec_services, svc_end[i]);+ }+ // --- Exceptions: sorted by (service_idx, date), one row per pair, with a+ // CSR index so the device does a range lookup + binary search instead of a+ // full scan per trip. `add_feed` already stable-sorted each feed's slice+ // and a feed's services are a contiguous index range, so the whole array+ // is sorted; for duplicate (service, date) rows the last in CSV order+ // wins — matching the old reader, which scanned every exception without+ // breaking out. ---+ debug_assert!(+ exceptions.windows(2).all(|w| (w[0].0, w[0].1) <= (w[1].0, w[1].1)),+ "per-feed exception slices did not concatenate into a sorted array"+ );+ let mut exc_unique: Vec<(u32, u32, u32)> = Vec::with_capacity(exceptions.len());+ for e in exceptions {+ match exc_unique.last_mut() {+ Some(last) if last.0 == e.0 && last.1 == e.1 => *last = e,+ _ => exc_unique.push(e),+ }+ }+ let mut sec_exceptions = Vec::new();+ let mut sec_exceptions_idx = Vec::new();+ let mut eacc: u32 = 0;+ let mut ei = 0usize;+ for s in 0..svc_mask.len() as u32 {+ append_u32(&mut sec_exceptions_idx, eacc);+ while let Some(&(sidx, date, added)) = exc_unique.get(ei) {+ if sidx != s {+ break;+ }+ append_u32(&mut sec_exceptions, sidx);+ append_u32(&mut sec_exceptions, date);+ append_u32(&mut sec_exceptions, added);+ eacc += 1;+ ei += 1;+ }+ }+ append_u32(&mut sec_exceptions_idx, eacc);+ (sec_services, sec_exceptions, sec_exceptions_idx)+}++fn build_spatial_grid(+ stop_lat: &[i32],+ stop_lon: &[i32],+ grid_lat0: i32,+ grid_lon0: i32,+ max_lat: i32,+ max_lon: i32,+) -> (Vec<u8>, Vec<u8>, Vec<u8>, u32, u32, u32, usize) {+ let stop_count = stop_lat.len();+ let grid_cell_e7 = (GRID_CELL_DEG * 1e7) as u32;+ let cell_col = |lon_e7: i32| -> i64 {+ ((lon_e7 as i64 - grid_lon0 as i64) / grid_cell_e7 as i64).max(0)+ };+ let cell_row = |lat_e7: i32| -> i64 {+ ((lat_e7 as i64 - grid_lat0 as i64) / grid_cell_e7 as i64).max(0)+ };+ let grid_cols = (cell_col(max_lon) + 1) as u32;+ let grid_rows = (cell_row(max_lat) + 1) as u32;+ // Sorted by `(cell_id, stop)`, which is exactly the two orderings the device+ // requires: GRID_CELL_IDS ascending, because the reader binary-searches it,+ // and each cell's GRID_STOPS run stop-ascending.+ let mut grid: Vec<(u32, u32)> = (0..stop_count)+ .map(|i| {+ let row = cell_row(stop_lat[i]) as u32;+ let col = cell_col(stop_lon[i]) as u32;+ (row * grid_cols + col, i as u32)+ })+ .collect();+ grid.sort_unstable();+ let mut sec_grid_cell_ids = Vec::new();+ let mut sec_grid_cell_off = Vec::new();+ let mut sec_grid_stops = Vec::with_capacity(stop_count * 4);+ let mut grid_cell_count = 0usize;+ for (k, &(cid, stop)) in grid.iter().enumerate() {+ if k == 0 || cid != grid[k - 1].0 {+ append_u32(&mut sec_grid_cell_ids, cid);+ append_u32(&mut sec_grid_cell_off, k as u32);+ grid_cell_count += 1;+ }+ append_u32(&mut sec_grid_stops, stop);+ }+ append_u32(&mut sec_grid_cell_off, grid.len() as u32);+ (+ sec_grid_cell_ids,+ sec_grid_cell_off,+ sec_grid_stops,+ grid_cols,+ grid_rows,+ grid_cell_e7,+ grid_cell_count,+ )+}