Repository navigation
perf(share): Export Raster writes a made raster's embedded GeoTIFF instead of encoding it again - #3033
Conversation
…stead of encoding it again A raster a command makes is embedded as the GeoTIFF write_all writes of it. A plain export that writes every band produces those same bytes, so it now writes the embedded file while the layer still holds the pixels and the file it was made with (weak references, broken by any change to either), and encodes as before otherwise.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| let bytes = rustgis_data::raster::geotiff::write_all(&raster).map_err(|e| e.to_string())?; | ||
| let name = raster.name.clone(); | ||
| let layer = rustgis_map::RasterLayer::from_bytes(raster, format!("{name}.tif"), bytes); | ||
| let layer = rustgis_map::RasterLayer::from_written(raster, format!("{name}.tif"), bytes); |
There was a problem hiding this comment.
Twins that still take the old path: rasters made with the same geotiff::write_all bytes but wrapped with RasterLayer::from_bytes miss the new export shortcut, so share.exportRaster re-encodes them. These are stac_bands.rs (around line 226, addBands/addIndex), embeddings.rs (made_saying, around line 883) and imagery.rs (layer_of, around line 173). Switching each to from_written keeps the bytes and adds the pairing check. Either extend this PR to them or say in the description why they're left out, since the description scopes the change to workflows::made.
Category: performance
There was a problem hiding this comment.
Fixed in be8f466. The three named sites take from_written, and so do two more that embed write_all's bytes unchanged: stac_bands.rs (index view, around line 677) and cube.rs (add_outputs). I first said the KML overlay, layer.rs and the Zarr sites keep bytes from another writer. That was wrong: they use geotiff::write, and take from_written since 27c3346 and c319c6d. The engine test also drops its copy of the layer before the edit.
Code reviewI found no bugs in the changed code. I checked the pairing logic, the export branch, and the notes path, and they hold up. The main finding is a missed twin, which is a performance gap rather than a correctness problem. There are no earlier review threads on this PR, so nothing was left out as already raised. Bugs: None found.
Security: None found. The output path and write call are unchanged. Performance (confidence: high on the fact, medium on whether it is wanted):
Quality (nits, not posted inline):
AGENTS.md (confidence: medium): No rule break found. The PR describes memory as "no layer data is copied" but gives no multiple of the layer. That is arguably not required, since the change stores no new layer data. Checked for twins: Change sizeCompared with the base branch (merge base e48daeb to PR head 1fdd16f), covering the whole PR.
|
STAC bands and indices, AlphaEarth similarity and change, imagery results and data cube trends embed write_all's bytes too: they pair them with their pixels as made rasters do. The test drops its copy of the layer before the edit, so the edit takes the in-app path.
📖 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 |
| // A raster a command made embeds the GeoTIFF it was written as: a plain export of every band | ||
| // writes those same bytes, so they are written again rather than encoded again. | ||
| let written = s.map().layer(id).and_then(|l| l.raster.as_ref()).and_then(|rl| rl.written_file()).cloned(); | ||
| if let Some(bytes) = written.filter(|_| !cog && raster::geotiff::written_bands(&r, bi).len() == r.bands.len()) { |
There was a problem hiding this comment.
This branch makes a single write_atomic call of the whole embedded file, with no Cancel point before or during it, and the closure has nothing to say so. The encode it replaces had progress::check between strips. The write is one copy of bytes the layer already holds, so the gap is small, but AGENTS.md ("Never hold the window") asks that a long step without a check is said in its doc comment. A one-line comment here would do, and the new test doesn't exercise cancelling this path.
Category: quality
There was a problem hiding this comment.
Fixed in 27c3346: the shortcut looks at Cancel before its one write, and a comment says the write itself has no check inside it.
Code reviewNo bugs or security issues found. The export shortcut produces the same bytes as the encode it replaces, and the pairing check holds. I checked the Bugs: None found. Security: None found. Performance:
Quality:
AGENTS.md: No breaks found beyond the quality point above. Checked for twins: The other Change sizeCompared with the base branch,
|
A KML overlay and a raster kept as a plain GeoTIFF when it can't be a cloud-optimised one embed geotiff::write's file of their first band, which is the export's own file when it writes every band. The export looks at Cancel before its one write.
|
Twins: |
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let tif = dir.join("hill.tif"); | ||
| s.run("share.exportRaster", &json!({"layer": id, "path": tif.display().to_string()})).unwrap(); | ||
| assert_eq!(std::fs::read(&tif).unwrap(), *embedded); |
There was a problem hiding this comment.
This assertion can't tell the shortcut from a fresh encode: geotiff::write of the same pixels gives the same bytes as embedded, so the test passes if export_work drops the shortcut and encodes again. The pixel-change half does catch a stale pairing, but nothing checks that the shortcut is taken. A cheap fix is to make the test observe the path, for example by giving the layer embedded bytes that deliberately differ from a fresh encode and asserting the export writes them. Left as a quality point; the rest of the test is sound.
Category: quality
There was a problem hiding this comment.
Fixed in c319c6d: the test plants marked bytes through from_written and checks that the export writes exactly those. With the shortcut turned off it fails.
| /// [`rustgis_data::raster::geotiff::write`] of its first band, the same file when that writes every | ||
| /// band), kept in the project: [`RasterLayer::written_file`] gives that file back while neither | ||
| /// changes. | ||
| pub fn from_written(raster: Raster, file_name: impl Into<String>, bytes: Vec<u8>) -> RasterLayer { |
There was a problem hiding this comment.
from_written is pub and takes its file on trust: nothing checks that bytes is the geotiff::write or write_all output of raster's pixels. Every current caller passes the bytes it just encoded from the same raster, so nothing is wrong today. But a future caller that passes, say, a cloud-optimised file (or bytes of a different raster) would have the export write those bytes as a plain GeoTIFF while its notes say it encoded the pixels. The doc comment states the contract; a pub(crate) constructor, or a debug assertion that the first band's dimensions match, would make the mistake harder to make.
Category: quality
There was a problem hiding this comment.
Left for the release quality pass: every caller passes the bytes it just encoded from the same raster, and the doc states the contract.
Code reviewOverall the change is sound. I found no bugs, security issues or AGENTS.md breaks. The pairing check in Bugs: none found. Confidence: high. Security: none found. Performance
Quality
AGENTS.md: no breaks found. The cancel point before the single write is addressed by the comment in Twins checked: the Earlier concerns: I left 2 earlier concerns alone because they are addressed: the twin list for Change sizeCompared: the whole PR against main (merge base e48daeb to PR head 27c3346)
|
write_project_slice is geotiff::write of the slice's first band, as is the tessera read that isn't handed over, so they pair their file with their pixels too. The test plants marked bytes through from_written, so it sees which file the export writes.
|
Zarr and tessera twins: the three Zarr sites ( |
Code reviewI found no bugs, crash paths, security issues or AGENTS.md breaks. I posted one inline performance comment. Bugs: none found. The export shortcut is only taken when Security: none. The export path still goes through Performance
Quality (grouped nits, not posted inline)
AGENTS.md: none. The new Prior review threads: I left 4 alone. The twin sites ( Twins checked: the raster read routes ( Change sizeCompared: the whole PR against main (merge base e48daeb to PR head c319c6d).
|
…file too geotiff_bytes, which the app's download uses, shares the export's check for when a layer's embedded file is the one the export would write.
|
Browser download: |
| let cog = b(p, "cog") == Some(true); | ||
| let bytes = if cog { raster::cogwrite::write(&r, bi) } else { raster::geotiff::write(&r, bi) }.map_err(|e| e.to_string())?; | ||
| let bytes = match written_export(s, id, &r, bi, cog) { | ||
| Some(bytes) => bytes.to_vec(), |
There was a problem hiding this comment.
bytes.to_vec() copies the whole embedded file. The browser download (ui.exportRaster → Services::download, which takes an owned Vec<u8>) therefore holds the layer's file and a second file-sized buffer at the same time. The encode it replaces also allocated one file-sized buffer, so this isn't a regression. But the PR's memory note ("no second encoded file the size of the output") holds only for the desktop export, not the browser download. Either say so in the description, or change the download path to take the shared Arc<Vec<u8>> so the copy goes away.
Category: performance
There was a problem hiding this comment.
The description now says so: the download copies the file once, as the encode it replaced allocated one. Passing the shared Arc would change the Services::download interface, which is outside this PR.
Code reviewI read the full diff, the prior review threads, and the surrounding code. I could not build or run the tests (no shell here), so the review is by reading only. I found no bugs, security issues, or AGENTS.md violations. One performance point is posted inline. Bugs
Security
Performance
Quality
AGENTS.md
Twins checked
Prior reviews
Change sizeCompared with the base: the whole PR against main (merge base e48daeb to PR head 164494c).
|
|
The two readability nits (the flag inferred from |
Summary
A raster a command makes is encoded twice on its way to a file.
workflows::madewrites it withgeotiff::write_allinto the layer as its embedded GeoTIFF, andshare.exportRasterencodes it again withgeotiff::write. For a raster whose export writes every band (one grey band, or the colour bands of an RGB raster), the two writers produce the same bytes. The second Deflate pass repeats the first.This PR:
RasterLayer::from_written, whichmadenow uses, as do the other places that embedwrite_all's bytes of the raster they add: STAC bands and indices, AlphaEarth similarity and change, imagery results (imagery::layer_of) and data cube trends. KML overlays, Zarr slices (zarr::write_project_slice), the tessera read when it isn't handed a file, and rasters kept as a plain GeoTIFF when a cloud-optimised one fails take it too: they embedgeotiff::write's file of their first band, which is the export's own file when the export writes every band. Alongside the embedded file, it records which pixel data and which file the layer was made with, as weak references.RasterLayer::written_filegives the file back only while the layer still holds both.share.exportRasterwrite that file (looking at Cancel before its one write), and the app's Export Raster download (raster::geotiff_bytes, which the browser uses) return it, when the export is a plain GeoTIFF of every band. A cloud-optimised export, one band of a multi-band raster, an uploaded raster, and a layer whose pixels or embedded file have changed since are encoded as before.Changing either the pixels or the embedded file gives the layer a new allocation:
Arc::make_mutmoves the data away from any weak reference, andArc::get_mutrefuses while one exists. So a stale pairing never matches. A weak reference also keeps its old address from being reused, so a new allocation can't collide with it. Neither reference holds the raster's pixels or the file's bytes in memory.Output
The benchmark's exported hillshade is byte-identical to
main's. What the export says (its notes and answer) is unchanged.Timing
Benchmark case:
layer.addDataofdem.tif(115 million cells),raster.functionhillshade (azimuth 315, altitude 45),share.exportRaster. Runs alternatedmain(e48daeb), this change and single-threadedgdaldem hillshadewith Deflate, 5 runs each, all queued in the same host slot. Builds were kept out of it, but other test runs kept the load at 9–14. Treat the wall times as relative; the CPU figures are steadier.maingdaldemThese runs used a build whose export path for this case matched this PR's; the PR replaces only how the pairing is checked.
Memory
No layer data is copied.
share.exportRasterwrites theArcthe layer already holds, so it no longer allocates a second encoded file the size of the output. The app's download (geotiff_bytes) handsServices::downloadan ownedVec<u8>, so it copies the file once, as the encode it replaces allocated one.Not changed
#[serde(skip)]. A reopened project's made raster is encoded on export, as before.Surfaces
share.exportRasterfrom the desktop, browser, command line, Python, the Assistant and MCP, for every raster a command makes throughworkflows::made(the raster commands inworkflows,imageryandin_blocks, such as Raster Functions, the Function Editor and band math) and for thefrom_writtencallers above. Geoprocessing tool outputs (gp.run), downloaded or handed-over files, and rasters embedded as cloud-optimised GeoTIFFs keep bytes from another writer and are unchanged. No parameter, command or saved field changes, so ribbon, search and tool descriptions are unaffected. Undo and Redo are unaffected: the pairing travels with the layer and is checked against whatever pixels the layer holds.Testing
a_written_file_is_given_back_only_while_its_pixels_and_bytes_are_the_layers(rustgis-map) covers an uploaded raster (no file given back), a made one, a copy of it, one whose pixels changed, and one whose embedded file was replaced.exporting_a_made_raster_writes_its_embedded_file_until_its_pixels_change(engine) checks a made hillshade's embedded file againstgeotiff::write. It then plants marked bytes throughfrom_writtenand checks that the export writes exactly those (it fails with the shortcut turned off), then changes a pixel and checks that the export holds the new value.geotiff_bytes) returns the planted bytes and a cloud-optimised one doesn't.written_fileskips its pairing check and returns the embedded file whenever there is one.cargo fmt --all --checkand the private preflight passed.UI evidence
No visible UI change.
Related issue
Part of #1111.