Repository navigation
perf(data): an export to longitude/latitude converts each place once, on several threads - #2967
Conversation
… on several threads Exporting a layer held in a projected system to longitude/latitude converted every vertex twice on one thread: once for the layer's box (lonlat_extent) and again to write the file (project_each_leaving_out). The conversion that writes the file now also gives the box, converts each geometry where it lies instead of copying it, and runs on several threads from 8192 features on (rustgis_data::threads; one thread below that and in the browser). Each thread looks at Cancel between pieces of 512 features and counts them toward the progress. The file written is unchanged, and the box is exactly lonlat_extent's: places that can't be converted are left out of it.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| let _probe = crate::progress::install(probe.clone()); | ||
| while !failed.load(Ordering::Relaxed) { | ||
| // The lock is held only to take a piece; a poisoned one means a thread panicked. | ||
| let Some((k, piece)) = next.lock().ok().and_then(|mut n| n.next()) else { break }; |
There was a problem hiding this comment.
A poisoned lock here ends the work without a notice. lock().ok() turns the poison into None, so the break returns Ok with the remaining pieces never converted, and the export writes them in the source system. The same kind of silent skip is at line 814, where a poisoned done lock drops that piece's unplaced count and box, and at line 828, where failure.into_inner().ok() drops an error.
Each path is unreachable today, because the locks are only held across Iterator::next and a slot write, and neither can panic. The export has to be never silent, though, so a poisoned lock should become an error: record it in failure and set failed (or map it in into_inner) rather than skipping the piece.
Category: quality
There was a problem hiding this comment.
Fixed in 711c5e2: a poisoned next or done lock now records an error and stops the threads, and a poisoned failure or done at the end returns that error instead of being dropped.
| } | ||
| let to = ds.lonlat_conversion().unwrap(); | ||
| let mut threaded = ds.features.to_vec(); | ||
| let (unplaced, placed) = crate::io::project_each_leaving_out(&mut threaded, &to, true).unwrap(); |
There was a problem hiding this comment.
This test is meant to cover the threaded path, but the threads come from threads::max(), which is 1 on a single-core machine or when the thread setting is 1. project_each_leaving_out then takes the one-thread path, so the box, bit-for-bit and Cancel assertions pass without any thread running. A runner with few cores would hide a broken threaded path.
Consider asserting that the threaded call really has more than one thread (for example assert!(crate::threads::max() > 1) once crate::threads::cores() > 1 is checked), so the test says when it did not use threads.
Category: quality
There was a problem hiding this comment.
Added in 711c5e2: the test asserts threads::max() > 1 whenever threads::cores() > 1.
Code reviewBugs: None found. The box is equivalent to Security: None found. Performance: None found. The thread count comes from Quality:
AGENTS.md: No breach found. I checked the thread rules ( Prior review threads: none on this PR, so no earlier concerns were left alone. Change sizeCompared with
|
… stops between features and gives a download its box The first feature with a place is converted on the calling thread before any worker starts, so the conversion's advisory missing-grid check sees the same first place as one thread does. Workers look at Cancel and count between features, and once more after they finish, so a Cancel that comes once every piece is taken still stops the run. write_bytes_noted returns the layer's longitude/latitude box from its conversion, and the browser download uses it instead of converting again.
…r than skipping a piece Its test also says when the computer has several cores but the conversion got one thread.
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the |
| } | ||
| match done.lock() { | ||
| Ok(mut d) => { | ||
| if let Some(slot) = d.get_mut(k) { |
There was a problem hiding this comment.
If k ever has no slot, this drops that piece's unplaced count and box without a word, and the export's box and notes come out short. It can't happen today, because done is sized from the same div_ceil and k comes from enumerate() over the same chunks_mut. Still, the export has to be never silent, so an empty slot should become an error (the same way a poisoned lock does) rather than being skipped:
| if let Some(slot) = d.get_mut(k) { | |
| match d.get_mut(k) { | |
| Some(slot) => *slot = (unplaced, placed), | |
| None => { | |
| stop(DataError::Format("a piece of the features had no place to record its count".into())); | |
| break; | |
| } | |
| } |
Category: quality
There was a problem hiding this comment.
Left for the release quality pass: k comes from enumerate() over the same chunks_mut that sizes done, so the slot can't be missing today.
| lost |= was.get(k).copied().unwrap_or(true) && !c.is_finite(); | ||
| k += 1; | ||
| g.for_each_coord_mut(|c| { | ||
| let p = to.point(*c); |
There was a problem hiding this comment.
Before this change the one-thread path never called to.point on a coordinate that wasn't a number (the old if c.is_finite() guard skipped it). Now every coordinate goes through point first, including NaN and infinite ones, and the head split in project_pieces_leaving_out counts a feature whose only coordinates are NaN as one "with a place". So the first conversion asked may now be a NaN coordinate, not the first finite place. Whether that matters depends on what point does for a non-finite input. I could not read terrio's note_missing_grid from here, so this is a question rather than a finding. Could you confirm that a non-finite input never reaches the grid check, or make the head split look for the first finite coordinate instead? The box needs these calls to match lonlat_extent_of, so the fix is the head split, not dropping the calls.
Category: quality
There was a problem hiding this comment.
Fixed in c99fdc0: the part converted on the calling thread now runs until a coordinate actually converts, so a NaN-only feature or one whose places fail to convert no longer ends it. a_threaded_export_converts_first_up_to_the_first_place_it_can_convert covers it.
Code reviewI found no bugs, security problems or AGENTS.md violations that I could confirm. The threaded conversion looks deterministic: each feature goes through the same Bugs
Security
Performance
Quality
AGENTS.md
Prior reviews: 2 earlier threads, both already answered and addressed in 711c5e2 (the test that never used threads, and the poisoned-lock paths). I left both alone, so 2 concerns were not re-raised. Change sizeCompared with the base branch: the whole PR, merge base ced7c3d to head 711c5e2. Six files were modified, none added, removed or renamed.
|
…, up to the first place it converts A feature with no geometry, or only places that are missing or can't be converted, never reaches the conversion's first-place missing-grid check, so it no longer ends the part converted on the calling thread. That part is chosen in the conversion loop itself, which looks at Cancel and counts between features, so there is no separate pass over the geometries.
Code reviewNo new bugs, security issues, or AGENTS.md breaks found in the changed lines. I reviewed by reading the diff and the surrounding code. I could not build or run the tests (no shell in this environment), so the compile and test results are unverified. Bugs
Security
Performance
Quality
AGENTS.md
Earlier concerns left aloneI left 4 earlier threads alone, because the code they point at is unchanged or the author's fix is in the diff:
Change sizeCompared: the whole PR against main (merge base ced7c3d to PR head c99fdc0).
|
Summary
When a layer held in a projected system is exported to longitude/latitude, every vertex used to be converted twice, both times on one thread.
share::write_layerfirst got the layer's box fromDataset::lonlat_extent, which converts each vertex, and thenproject_each_leaving_outconverted them all again to write the file. Buffering 100,000 UTM points by 5 km and exporting the result to GeoPackage converts 12.9 million vertices, and the export was about 80% of the run.Now one conversion does both jobs:
project_each_leaving_outnow also returns the box of the places it converts. The export takes the layer's box from that pass through the newio::write_path_placed, which falls back tolonlat_extent()whenever the file isn't written in longitude/latitude from another system. The box is exactlylonlat_extent's: it converts every coordinate the same way, and places that can't be converted (NaN) stay out of it. A feature dropped for one unplaceable vertex still counts its other vertices, as before.Geometry::for_each_coord_mut), so the copy thatproject_leaving_outalready makes is no longer copied again geometry by geometry. Without this, the threads raised peak memory by about 180 MB, because old and new geometries were alive at once in separate allocator arenas.rustgis_data::threads::max()threads (at most one per 2,048 features). Each thread takes pieces of 512 features and installs the run's progress probe. It looks at Cancel between features and, with counting on, counts each feature once it is converted; the run looks once more after the threads finish, so a Cancel that comes after every piece is taken still stops it. The features up to the first one with a place that converts are converted first, in order on the calling thread and in the same loop that looks at Cancel and counts, so the conversion meets first the same place as on one thread: its advisory missing-grid check looks only at the first place it converts, and for a custom system with no EPSG code nothing else asks earlier. Features with no geometry, or only missing places or places that can't be converted, never reach that check, so they don't end that part. A lock poisoned by a panicking thread fails the run rather than skipping a piece. Below the threshold, and always in the browser, it runs on one thread as it did before. Every feature is converted exactly as one thread converts it, so the output is the same bit for bit. A thread the OS refuses leaves its share to the others.An eager CSV export still converts the points once more before the write, to count what the file changes (
export_losses_notes_in); that count isn't taken from the write's conversion.This doesn't change which coordinate system an export uses. The streamed export (
export::write_chunks_as) shares the same function, so a chunk of 8,192 features or more is converted on threads too. That path still takes its box from the source layer.This follows the plan agreed with pick2510 on #2032: the extent leaves out what couldn't be converted, the web build has a one-thread path, and Cancel reaches the threaded conversion.
Memory: no new copy of the layer. The export still holds the one converted copy it did before (1× the layer's geometry), and now converts it in place. Peak RSS on the benchmark: 499 MB before, 493 MB after. The
exportscenario incargo xtask memorywrites a table that needs no conversion, so that ratchet isn't affected. No code outsiderustgis-datareads rows in a new way, so the rows ratchet isn't affected either.Reaches: every surface that exports a layer through
share.exportLayer: the desktop app (it runs off the UI thread, with progress and Cancel), the command line, Python, the Assistant and MCP, and plugins. The command, its parameters and its notices are unchanged. In the browser, GeoPackage export isn't available, and downloads go throughwrite_bytes_noted, which uses the same function on one thread and now also returns the layer's box from that conversion; the download's notices use it instead of converting again. There's nothing new to undo: an export doesn't change the project.Testing
Timings. Run through hq on this machine (8 hardware threads), with
/mnt/scratch/rustgis-bench/bin/rustgis-cli-3a5dd41d4(3a5dd41) against this branch's release build. The command is the benchmark'slayer.addData→gp.run buffer 5 km→share.exportLayerto a new.gpkg. Each figure is the median of 5 runs after one warm-up run, alternating old and new. Other jobs were sharing the machine, and some runs got under 60% of a core, so CPU time (user + sys) is given beside wall time because it's less sensitive to that.points100k_utm.gpkg(UTM)points100k.gpkg(lon/lat)* The lon/lat wall medians mostly measure the machine's load. That path never reaches the changed conversion, because the layer is already in the export's system, and its CPU time is the same within noise. Its runs with no load (over 240% CPU) took 4.02 and 4.09 s before, and 4.07 and 4.31 s after. The unloaded UTM runs took 8.74–9.30 s before and 3.46–4.06 s after.
Same file. In all 12 pairs of old and new outputs, the files have the same length and the same bytes, except for the
last_changetimestamp ingpkg_contents, which differs between any two runs of the same binary too. I checked this by masking that one ISO timestamp and comparing the bytes.Tests.
unplaced_out_tests::an_export_takes_its_box_from_its_one_conversion_on_any_number_of_threadsuses 30,000 lines, one in a thousand with a vertex that can't be converted and one in a thousand with a NaN vertex. It checks that the threaded box equalslonlat_extent(), that the threaded output matches the one-thread path (1,000-feature pieces) bit for bit, that a cancelled run stops partway withCancelled, and that a Cancel with one feature left, or (on several threads) after the last, still givesCancelled. It also fails when the computer has several cores but the conversion got one thread. As a check that the test can fail, I put back the earlier Cancel check (once per piece, before converting it, and none after the threads finish), and the test failed withunwrap_err()on anOkvalue.unplaced_out_tests::a_threaded_export_converts_first_up_to_the_first_place_it_can_convertputs an empty geometry, a feature with none, one with only a NaN place and one with only a place that can't be converted before the first convertible feature. It checks that the calling thread converts through that fifth feature, all of them when none converts, and that the threaded run matches one thread bit for bit. With the earlier rule (the first feature with any coordinate) it fails, stopping at the third.unplaced_out_testspass, as docargo fmt --all --checkand the full CI run on the fork for this head.UI evidence
No visible UI change.
Related issue
Part of #2032.
Summary by CodeRabbit