Skip to content

perf(share): Export Raster writes a made raster's embedded GeoTIFF instead of encoding it again - #3033

Merged
Mavrick-F merged 5 commits into
opengeos:mainfrom
Mavrick-F:perf/export-reuses-embedded
Nov 26, 2026
Merged

Mavrick-F merged 5 commits into
opengeos:mainfrom
Mavrick-F:perf/export-reuses-embedded

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

A raster a command makes is encoded twice on its way to a file. workflows::made writes it with geotiff::write_all into the layer as its embedded GeoTIFF, and share.exportRaster encodes it again with geotiff::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:

  • Adds RasterLayer::from_written, which made now uses, as do the other places that embed write_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 embed geotiff::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_file gives the file back only while the layer still holds both.
  • Makes share.exportRaster write 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_mut moves the data away from any weak reference, and Arc::get_mut refuses 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.addData of dem.tif (115 million cells), raster.function hillshade (azimuth 315, altitude 45), share.exportRaster. Runs alternated main (e48daeb), this change and single-threaded gdaldem hillshade with 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.

Median of 5 main This PR gdaldem
Wall 5.88 s 4.60 s 8.30 s
User CPU 18.54 s 15.18 s 7.31 s

These 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.exportRaster writes the Arc the layer already holds, so it no longer allocates a second encoded file the size of the output. The app's download (geotiff_bytes) hands Services::download an owned Vec<u8>, so it copies the file once, as the encode it replaces allocated one.

Not changed

  • Making the raster still encodes it once, for the embedded file. Deferring that to when a project is saved would also save the other encode, but many places read the embedded file, and it changes how projects keep made rasters. That is a separate design question.
  • Nothing saved changes: the pairing is #[serde(skip)]. A reopened project's made raster is encoded on export, as before.

Surfaces

share.exportRaster from the desktop, browser, command line, Python, the Assistant and MCP, for every raster a command makes through workflows::made (the raster commands in workflows, imagery and in_blocks, such as Raster Functions, the Function Editor and band math) and for the from_written callers 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 against geotiff::write. It then plants marked bytes through from_written and 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.
  • The same test checks that the download (geotiff_bytes) returns the planted bytes and a cloud-optimised one doesn't.
  • Both fail when written_file skips its pairing check and returns the embedded file whenever there is one.
  • The fork's CI ran the full gate. cargo fmt --all --check and the private preflight passed.

UI evidence

No visible UI change.

Related issue

Part of #1111.

…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.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Raster layers now track the raster and byte allocations associated with written GeoTIFFs. Eligible non-COG exports reuse stored bytes when the selected bands cover every raster band. Other exports use the existing writer.

Changes: Raster GeoTIFF export

Layer / File(s) Summary
Track written raster bytes
crates/map/src/raster.rs
RasterLayer::from_written records weak references to the raster and byte allocations. written_file returns the bytes only while both allocations match. Other constructors initialize the association as absent. Tests cover clones and allocation replacement.
Mark locally written raster layers
crates/engine/src/cmd/workflows.rs, crates/engine/src/cmd/cube.rs, crates/engine/src/cmd/embeddings.rs, crates/engine/src/cmd/imagery.rs, crates/engine/src/cmd/stac_bands.rs, crates/engine/src/cmd/layer.rs, crates/engine/src/cmd/zarr.rs, crates/engine/src/cmd/raster.rs
Raster-producing paths use from_written for locally written GeoTIFF bytes. embeddings.add_zones continues to use from_bytes for handed-over bytes.
Reuse stored bytes on export
crates/engine/src/cmd/raster.rs, crates/engine/src/tests.rs
Eligible non-COG exports write stored bytes atomically. Other exports use the existing writer. Tests check byte reuse, COG encoding, and whether a later export reflects a changed pixel.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related issues

  • opengeos/RustGIS#1111 includes slow GeoTIFF writing. This PR avoids a second encoding for eligible exports but does not address the issue’s broader scope.
  • opengeos/RustGIS#1720 proposes AlphaEarth similarity and change outputs. This PR changes those outputs to use written-file tracking, but does not address the issue’s time cube request.

Possibly related PRs

Suggested reviewers: giswqs, pick2510

Merge Risk: Minimal · up to 16449

Raster exports now reuse the GeoTIFF already embedded in the layer, which avoids a second encode. Cloud-optimised, partial-band, uploaded and modified rasters keep the existing encoding path. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title uses the conventional-commit form and names the main change: share.exportRaster now writes a made raster's embedded GeoTIFF instead of encoding it again. The changeset adds RasterLayer::from…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is detailed and covers the Summary, Testing, and Related issue sections. It explains the change (reusing the embedded GeoTIFF for plain every-band exports via RasterLayer::from_written…
Full details: Title check

Explanation

The title uses the conventional-commit form and names the main change: share.exportRaster now writes a made raster's embedded GeoTIFF instead of encoding it again. The changeset adds RasterLayer::from_written and written_file, and the export path reuses the stored bytes, so the title matches the primary change. It is concise and specific.

Full details: Description check

Explanation

The description is detailed and covers the Summary, Testing, and Related issue sections. It explains the change (reusing the embedded GeoTIFF for plain every-band exports via RasterLayer::from_written and written_file), gives benchmark and memory notes, lists the unchanged surfaces, and names the tests that were run. The UI evidence section correctly states 'No visible UI change.' The Related issue section says 'Part of #1111.' The template's Testing section asks for how changes were verified, and the author gives specific tests and CI results. The description meets the template's structure and required information, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the bytes in flight,
And keeps the raster snug and right.
When every band is set to show,
The saved GeoTIFFs can flow.
A changed pixel gets fresh-file care,
While COGs take the writer’s route with flair.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 942fff66-f058-4b4e-ae7a-6b3a661c2673
Commits

Reviewing files that changed from the base of the PR and between c319c6d and 164494c.

Files selected for processing: 2

  • crates/engine/src/cmd/raster.rs
  • crates/engine/src/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Mavrick-F Mavrick-F Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I 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.

  • The written_file pairing is sound. Weak keeps the old allocation's address from being reused, and every mutation that changes pixels or the embedded file goes through Arc::make_mut, which moves to a new allocation whenever a weak reference exists. Nothing in the raster code uses Arc::get_mut, so the weak reference does not turn in-place edits into copies.
  • The byte-identity claim holds on the code paths I read. For a single band, or a colour raster whose written bands equal its band count, encode(.., every=true) and encode(.., every=false) take the same branch.
  • The note text comes from the same geotiff_note call as before.
  • The export closure captures Arcs, so no layer data is copied.
  • #[serde(skip)] means no saved format changes, so cargo xtask schema is not needed.

Security: None found. The output path and write call are unchanged.

Performance (confidence: high on the fact, medium on whether it is wanted):

  • Commands that embed write_all bytes via RasterLayer::from_bytes still miss the shortcut. These are stac_bands.rs (around line 226), embeddings.rs (made_saying, around line 883) and imagery.rs (layer_of, around line 173). The PR scopes the change to workflows::made, so either extend it or say why these are out of scope. I left an inline comment on workflows.rs:415. I could not post on the other three files because the PR diff does not touch them.

Quality (nits, not posted inline):

  • In the new engine test (around tests.rs:18983), the rl clone stays alive through the pixel edit. That forces make_mut to copy, which is not the in-app path where the weak reference moves the value. The test still catches a missing pairing check. Adding drop(rl); before the edit would test the realistic path.

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: layer.addData* and the other read routes are not touched by this change. The overlay tools are not touched. gp.run raster outputs (gp.rs:4115) use their own bytes and are unchanged, as the PR says. No wasm32 branch changed. cargo xtask memory has no raster-export scenario, so memory-limits.txt needs no update.

Change size

Compared with the base branch (merge base e48daeb to PR head 1fdd16f), covering the whole PR.

Measure Value
Files changed 4 (modified 4, added 0, removed 0, renamed 0)
Size before 4053.0 KB
Size after 4057.5 KB
Total size change +4.7 KB
Path Before After Change
crates/engine/src/tests.rs 3663.8 KB 3664.6 KB +1.6 KB
crates/map/src/raster.rs 168.5 KB 170.9 KB +2.4 KB
crates/engine/src/cmd/raster.rs 77.4 KB 78.0 KB +651 B
crates/engine/src/cmd/workflows.rs 143.2 KB 143.2 KB +2 B

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.
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3033/
Web app https://preview.rustgis.app/pr-3033/app/
Commit 164494c

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 preview label (once per commit).

Comment thread crates/engine/src/cmd/raster.rs Outdated
// 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()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 27c3346: the shortcut looks at Cancel before its one write, and a comment says the write itself has no check inside it.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

No 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 Arc/Weak invariants and the byte equivalence in geotiff::encode, the Undo and clone paths, and the serde skip. Earlier concerns: 1 thread (the write_all twins, two comments) is already answered and the diff confirms the fix, so I left it alone.

Bugs: None found.

Security: None found.

Performance:

  • Missed twins that use the same writer as the export. add_overlay (crates/engine/src/cmd/raster.rs:289-290) and the plain-GeoTIFF fallback in crates/engine/src/cmd/layer.rs:6694-6705 both encode with geotiff::write(&r, 0) and then from_bytes. Their exports still re-encode. The PR reply says these keep bytes "from another writer", which is not true for these two. Confidence: high on the fact, medium on priority. Fix: switch them to from_written, and update that function's doc, which says the file is the one write_all wrote.

Quality:

  • crates/engine/src/cmd/raster.rs:1288 (inline): the new shortcut's single file write has no Cancel point and nothing says so. Comment only.
  • Minor: the engine test shadows rl after drop(rl). Naming only, so it can wait for the release quality pass.

AGENTS.md: No breaks found beyond the quality point above.

Checked for twins: The other write_all sites (cube, embeddings, imagery, stac_bands, workflows) all take from_written. The Zarr sites and gp.rs tool outputs use other writers and stay on from_bytes, as the PR says.

Change size

Compared with the base branch, main (merge base e48daeb) to PR head be8f466.

Measure Value
Files changed 8 (0 added, 8 modified, 0 removed, 0 renamed)
Size before 4.41 MB
Size after 4.41 MB
Total size change +4.7 KB
Path Before After Change
crates/map/src/raster.rs 168.5 KB 170.8 KB +2.4 KB
crates/engine/src/tests.rs 3.58 MB 3.58 MB +1.6 KB
crates/engine/src/cmd/raster.rs 77.4 KB 78.0 KB +651 B
crates/engine/src/cmd/stac_bands.rs 68.9 KB 68.9 KB +4 B
crates/engine/src/cmd/cube.rs 113.5 KB 113.5 KB +2 B
crates/engine/src/cmd/embeddings.rs 93.5 KB 93.5 KB +2 B
crates/engine/src/cmd/imagery.rs 186.4 KB 186.4 KB +2 B
crates/engine/src/cmd/workflows.rs 143.2 KB 143.2 KB +2 B

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.
@Mavrick-F

Copy link
Copy Markdown
Member Author

Twins: add_overlay and the plain-GeoTIFF fallback in layer.rs take from_written in 27c3346 (the fallback only, since the cloud-optimised path uses another writer). from_written's doc now names geotiff::write of the first band too. The rl shadowing in the engine test is left for the release quality pass.

Comment thread crates/engine/src/tests.rs Outdated
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/map/src/raster.rs
/// [`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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass: every caller passes the bytes it just encoded from the same raster, and the doc states the contract.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Overall the change is sound. I found no bugs, security issues or AGENTS.md breaks. The pairing check in written_file holds: from_shared stores data as Arc::new(raster) exactly as passed, Weak keeps the old allocation so addresses can't be reused, and Arc::make_mut on a layer with weak references moves to a new allocation, so a stale pairing never matches. every only changes multi-band output, so written_bands(r, bi).len() == r.bands.len() does imply write(r, bi) and write_all(r) agree. #[serde(skip)] keeps the saved format unchanged, and the wasm-visible calls (write_atomic, Path) are already used on unconditional paths.

Bugs: none found. Confidence: high.

Security: none found.

Performance

  • Missed twins, with the PR's stated reason for skipping them being wrong. The author replied that the Zarr sites "keep bytes from another writer", but zarr::write_project_slice (crates/data/src/raster/zarr.rs:2158) calls geotiff::write(raster, 0), the same encoder the export uses. The Zarr sites at crates/engine/src/cmd/zarr.rs:550, :582 and :2028 still use from_bytes and so miss the shortcut. The gain is only for single-band slices, because a multi-band non-colour slice fails the band-count check. Confidence: high on the facts, medium on impact. This is a new concern because the author's evidence is incorrect.
  • crates/engine/src/cmd/embeddings.rs:552 and :561 (the tessera read in add_zones, None branch) also embed geotiff::write(&r, 0) through from_bytes. This is a new missed twin, and the same limit applies. The handed branch correctly keeps from_bytes, because it carries a downloaded file. Confidence: high.
  • Checked and fine: the from_bytes sites for uploads, JPEG 2000 (GDAL), GeoPackage/geoprocessing outputs and the test setups should stay as they are.

Quality

  • crates/engine/src/tests.rs:18990: the test cannot tell the shortcut from a fresh encode, because both produce identical bytes. The pixel-change half does catch a stale pairing. Posted inline.
  • crates/map/src/raster.rs:405: from_written is pub and trusts that bytes came from the same pixels. All current callers comply. Posted inline.
  • Nit, left for the release quality pass: written_file also fires for copies that share both Arcs, which is intended, but the doc comment could say so.

AGENTS.md: no breaks found. The cancel point before the single write is addressed by the comment in export_work, and the Size for real data statement holds because no new copy is made.

Twins checked: the from_written callers in cube.rs, embeddings.rs (made_saying), imagery.rs, stac_bands.rs (both sites), workflows.rs, raster.rs (add_overlay) and layer.rs (COG fallback, plain path only) all embed write/write_all output of the same raster. The Arc::make_mut in cmd/raster.rs:359 (place) correctly breaks the pairing.

Earlier concerns: I left 2 earlier concerns alone because they are addressed: the twin list for stac_bands, cube, embeddings and imagery, and the Cancel comment on the single write. I re-raised the Zarr part of the twin concern because the author's reply rests on an incorrect fact.

Change size

Compared: the whole PR against main (merge base e48daeb to PR head 27c3346)

Measure Value
Files changed 9 (9 modified, 0 added, 0 removed, 0 renamed)
Size before 4.84 MB
Size after 4.85 MB
Total size change +5.1 KB
Path Before After Change
crates/map/src/raster.rs 168.5 KB 171.0 KB +2.5 KB
crates/engine/src/tests.rs 3.58 MB 3.58 MB +1.6 KB
crates/engine/src/cmd/raster.rs 77.4 KB 78.2 KB +0.8 KB
crates/engine/src/cmd/layer.rs 442.4 KB 442.5 KB +128 B
crates/engine/src/cmd/stac_bands.rs 68.9 KB 68.9 KB +4 B
crates/engine/src/cmd/cube.rs 113.5 KB 113.5 KB +2 B
crates/engine/src/cmd/embeddings.rs 93.5 KB 93.5 KB +2 B
crates/engine/src/cmd/imagery.rs 186.4 KB 186.4 KB +2 B
crates/engine/src/cmd/workflows.rs 143.2 KB 143.2 KB +2 B

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.
@Mavrick-F

Copy link
Copy Markdown
Member Author

Zarr and tessera twins: the three Zarr sites (write_project_slice is geotiff::write) and the tessera read that isn't handed a file take from_written in c319c6d. A handed-over file stays on from_bytes. The doc note on copies sharing both Arcs is left for the release quality pass.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I 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 geotiff::encode would write the same bytes as write_all. That holds because encode's every branch only differs when written_bands(r, band).len() < bands.len(), and the shortcut requires equality. The Weak pairing is sound: a Weak keeps the allocation alive, so Weak::as_ptr can't be reused, and Arc::get_mut refuses while a Weak exists. Arc::make_mut with only a Weak present moves the struct rather than copying pixels, so the mutation sites in raster.rs:359 don't regress memory. Confidence: high.

Security: none. The export path still goes through write_atomic with the same path handling.

Performance

  • The browser's Export Raster button (ui.exportRaster with services.download set, ui-egui/src/lib.rs ~5677) calls raster::geotiff_bytes, which still re-encodes a made raster. The PR's Surfaces section lists the browser as benefiting, but only share.exportRaster gets the shortcut. Posted inline on crates/engine/src/cmd/raster.rs:1257. Confidence: high that the path is missed; medium on whether that is intended.

Quality (grouped nits, not posted inline)

  • The new test covers only the shortcut-taken case. Nothing exercises the fall-through: a multi-band or RGBA raster where written_bands doesn't match, a COG export, or the plain fallback that now calls from_written in layer.rs (~6705). A short negative test for each would show the shortcut is not taken when it shouldn't be. Confidence: medium.

AGENTS.md: none. The new progress::check is before the single write, and the doc comment says why the write itself has no check.

Prior review threads: I left 4 alone. The twin sites (stac_bands, embeddings, imagery, cube) and the Cancel note were fixed in the diff. The engine test that couldn't see the shortcut was fixed in c319c6d. The from_written contract concern is answered by the author as deferred to the release quality pass, and every current caller does pass bytes encoded from the same raster, so I did not re-raise it.

Twins checked: the raster read routes (add_raster, from_path, from_bytes sites in zarr.rs:514, raster.rs:802, gp.rs:4115) keep their bytes unchanged, which is correct for handed-over or other-writer files. share.rs:510 encodes a rendered map picture, not a layer, so it is not a twin. The overlay tools in analysis/src/tools.rs and the layer.add*/project.open* commands don't encode layer rasters this way.

Change size

Compared: the whole PR against main (merge base e48daeb to PR head c319c6d).

Measure Value
Files changed 10 (added 0, modified 10, removed 0, renamed 0)
Size before 5,055.5 KB
Size after 5,061.2 KB
Total size change +5.7 KB
Path Before After Change
crates/map/src/raster.rs 168.5 KB 171.0 KB +2.5 KB
crates/engine/src/tests.rs 3,663.8 KB 3,665.8 KB +2.0 KB
crates/engine/src/cmd/raster.rs 77.4 KB 78.2 KB +829 B
crates/engine/src/cmd/embeddings.rs 93.5 KB 93.7 KB +202 B
crates/engine/src/cmd/layer.rs 442.4 KB 442.5 KB +128 B
crates/engine/src/cmd/zarr.rs 98.0 KB 98.0 KB +6 B
crates/engine/src/cmd/stac_bands.rs 68.9 KB 68.9 KB +4 B
crates/engine/src/cmd/cube.rs 113.5 KB 113.5 KB +2 B
crates/engine/src/cmd/imagery.rs 186.4 KB 186.4 KB +2 B
crates/engine/src/cmd/workflows.rs 143.2 KB 143.2 KB +2 B

…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.
@Mavrick-F

Copy link
Copy Markdown
Member Author

Browser download: geotiff_bytes shares the export's check in 164494c, so the app's Export Raster download returns the embedded file too. The engine test checks that, and that a cloud-optimised download is still encoded. The other negative cases (multi-band, the plain fallback in layer.rs) are left for the release quality pass.

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(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I 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

  • None found. I checked the pairing mechanism in RasterLayer::written_file and it holds:
    • Arc::make_mut moves the data away from live Weak refs, and Arc::get_mut refuses while one exists, so a changed pixel or file breaks the pairing.
    • A live Weak keeps the allocation, so its address can't be reused.
    • Undo snapshots and clones keep their own valid pairs.
  • The shortcut is byte-equal to the encode it skips. When the written_bands gate passes, encode takes the same branch for write and write_all (geotiff.rs:2372).
  • get() returns the layer's own Arc, so the gate checks the pixels the export actually writes.
  • write_project_slice is geotiff::write(raster, 0), so the three Zarr sites that now use from_written match the gate.
  • The COG fallback (layer.rs:6694) is the only plain-GeoTIFF branch in that function. Its plain.is_some() check is correct.

Security

  • None found.

Performance

  • raster.rs:1258: the browser download copies the file with to_vec(). This is posted inline. Confidence high on the fact; severity low, because the encode it replaces also allocated one file-sized buffer. The PR's memory claim is accurate only for the desktop export.

Quality

  • Nits, not posted inline:
    • layer.rs:6705 infers "written with write(…, 0)" from plain.is_some(). Returning the flag from the match that builds bytes would state it directly.
    • raster.rs:1259 puts an if cog {…} else {…} inside a match arm. A let would read more easily.

AGENTS.md

  • None found. The new code is platform-neutral, and the cfg blocks are unchanged.

Twins checked

  • Other from_bytes sites that take non-write bytes, left unchanged on purpose:
    • zarr.rs:514: a handed-over download.
    • raster.rs:802: GDAL JPEG 2000 output.
    • gp.rs:4115: tool outputs. These are re-read from their bytes before the layer holds them, so the pairing would not hold.
  • The analysis tools (tools.rs:5133 and others) reach the layer through gp.rs:4115.
  • embeddings.rs: the handed-over path stays from_bytes.
  • Sites that call geotiff::write for something other than a layer add (share.rs:510, geolibre, embeddingsui.rs:402) are not layer adds.
  • Test fixtures were excluded.

Prior reviews

  • 4 threads were read, and none was re-raised.
  • The from_written contract thread was answered by the author as a quality-pass item, so I left it alone.
  • The workflows twins, the cancel-check thread, and the test-assertion thread were fixed in later commits.
  • 4 earlier concerns left alone because they were already raised.

Change size

Compared with the base: the whole PR against main (merge base e48daeb to PR head 164494c).

Measure Value
Files changed 10 (0 added, 10 modified, 0 removed, 0 renamed)
Size before 4.94 MB
Size after 4.94 MB
Total size change +6.3 KB
Path Before After Change
crates/map/src/raster.rs 168.5 KB 171.0 KB +2.5 KB
crates/engine/src/tests.rs 3.58 MB 3.58 MB +2.3 KB
crates/engine/src/cmd/raster.rs 77.4 KB 78.5 KB +1.1 KB
crates/engine/src/cmd/embeddings.rs 93.5 KB 93.7 KB +202 B
crates/engine/src/cmd/layer.rs 442.4 KB 442.5 KB +128 B
crates/engine/src/cmd/zarr.rs 98.0 KB 98.0 KB +6 B
crates/engine/src/cmd/stac_bands.rs 68.9 KB 68.9 KB +4 B
crates/engine/src/cmd/cube.rs 113.5 KB 113.5 KB +2 B
crates/engine/src/cmd/imagery.rs 186.4 KB 186.4 KB +2 B
crates/engine/src/cmd/workflows.rs 143.2 KB 143.2 KB +2 B

@Mavrick-F

Copy link
Copy Markdown
Member Author

The two readability nits (the flag inferred from plain.is_some() in layer.rs, and the if inside the match arm in geotiff_bytes) are left for the release quality pass.

@Mavrick-F
Mavrick-F added this pull request to the merge queue Nov 26, 2026
Merged via the queue into opengeos:main with commit 272e50f Nov 26, 2026
30 checks passed
@Mavrick-F
Mavrick-F deleted the perf/export-reuses-embedded branch November 26, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant