Repository navigation
perf(share): Export Raster writes a made raster's embedded GeoTIFF instead of encoding it again #3033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(share): Export Raster writes a made raster's embedded GeoTIFF instead of encoding it again #3033
Changes from all commits
1fdd16f
be8f466
27c3346
c319c6d
164494c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -412,7 +412,7 @@ pub(crate) type Then = Box<dyn FnOnce(&mut Session, &mut Value) + Send>; | |
| pub(crate) fn made(raster: Raster, extra: Option<Value>) -> Result<Made, String> { | ||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Twins that still take the old path: rasters made with the same Category: performance
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in be8f466. The three named sites take |
||
| Ok(made_layer(name, layer, extra, None)) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,11 @@ pub struct RasterLayer { | |
| /// [`RasterLayer::dropped_unsaid`]). | ||
| #[serde(skip)] | ||
| dropped: Vec<&'static str>, | ||
| /// The pixels and the file that `embedded` was written from them as, while both are still the | ||
| /// layer's (see [`RasterLayer::written_file`]): held weakly, so a change to either, which takes | ||
| /// a new allocation, breaks the pairing. | ||
| #[serde(skip)] | ||
| written: Option<(Weak<Raster>, Weak<Vec<u8>>)>, | ||
| } | ||
|
|
||
| /// Called once with a cloud-optimised GeoTIFF's overview, or why it couldn't be read. | ||
|
|
@@ -333,6 +338,7 @@ impl RasterLayer { | |
| lost_said: false, | ||
| look_lost: None, | ||
| dropped: Vec::new(), | ||
| written: None, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -383,6 +389,7 @@ impl RasterLayer { | |
| lost_said: false, | ||
| look_lost: None, | ||
| dropped: Vec::new(), | ||
| written: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -391,6 +398,24 @@ impl RasterLayer { | |
| Self::from_shared(raster, file_name, Arc::new(bytes)) | ||
| } | ||
|
|
||
| /// A raster with the file [`rustgis_data::raster::geotiff::write_all`] wrote of it (or | ||
| /// [`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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Category: quality
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| let mut layer = Self::from_bytes(raster, file_name, bytes); | ||
| layer.written = layer.data.as_ref().map(Arc::downgrade).zip(layer.embedded.as_ref().map(Arc::downgrade)); | ||
| layer | ||
| } | ||
|
|
||
| /// The embedded file, when it is the one [`RasterLayer::from_written`] was given for the | ||
| /// pixels the layer holds now: an export that writes every band writes those same bytes. | ||
| pub fn written_file(&self) -> Option<&Arc<Vec<u8>>> { | ||
| let (pixels, file) = self.written.as_ref()?; | ||
| let (data, embedded) = (self.data.as_ref()?, self.embedded.as_ref()?); | ||
| (Weak::as_ptr(pixels) == Arc::as_ptr(data) && Weak::as_ptr(file) == Arc::as_ptr(embedded)).then_some(embedded) | ||
| } | ||
|
|
||
| /// Keeps the original shared allocation used to decode an uploaded raster. | ||
| pub fn from_shared(raster: Raster, file_name: impl Into<String>, bytes: Arc<Vec<u8>>) -> RasterLayer { | ||
| let style = RasterStyle::default_for(&raster); | ||
|
|
@@ -421,6 +446,7 @@ impl RasterLayer { | |
| lost_said: false, | ||
| look_lost: None, | ||
| dropped: Vec::new(), | ||
| written: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -465,6 +491,7 @@ impl RasterLayer { | |
| lost_said: false, | ||
| look_lost: None, | ||
| dropped: Vec::new(), | ||
| written: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -896,6 +923,7 @@ impl RasterLayer { | |
| lost_said: _, | ||
| look_lost: _, | ||
| dropped: _, | ||
| written: _, | ||
| } = self; | ||
| if *in_place || *tiled || opening.is_some() || read_still.is_some() || self.casts_shadows() { | ||
| return None; | ||
|
|
@@ -2602,6 +2630,7 @@ impl RasterLayer { | |
| lost_said: _, | ||
| look_lost: _, | ||
| dropped: _, | ||
| written: _, | ||
| } = self; | ||
| let mut out = Vec::new(); | ||
| if let Some(p) = path { | ||
|
|
@@ -3217,6 +3246,25 @@ mod tests { | |
| assert!(missing.data.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_written_file_is_given_back_only_while_its_pixels_and_bytes_are_the_layers() { | ||
| let r = dem(); | ||
| let bytes = rustgis_data::raster::geotiff::write_all(&r).unwrap(); | ||
| assert!(RasterLayer::from_bytes(r.clone(), "dem.tif", bytes.clone()).written_file().is_none()); | ||
| let written = RasterLayer::from_written(r, "dem.tif", bytes.clone()); | ||
| assert_eq!(written.written_file().map(|b| b.as_slice()), Some(bytes.as_slice())); | ||
| // A copy shares both, so it gives the file too. | ||
| assert!(written.clone().written_file().is_some()); | ||
| let mut edited = written.clone(); | ||
| if let Some(band) = edited.data.as_mut().and_then(|d| Arc::make_mut(d).bands.first_mut()) { | ||
| band.data[0] += 1.0; | ||
| } | ||
| assert!(edited.written_file().is_none()); | ||
| let mut replaced = written.clone(); | ||
| replaced.embedded = Some(Arc::new(bytes)); | ||
| assert!(replaced.written_file().is_none()); | ||
| } | ||
|
|
||
| /// A plain web address is read as it was before logins: never downloaded whole on reload. | ||
| #[test] | ||
| fn reload_downloads_only_s3_and_login_wrapped_addresses() { | ||
|
|
||
There was a problem hiding this comment.
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 ownedVec<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 sharedArc<Vec<u8>>so the copy goes away.Category: performance
There was a problem hiding this comment.
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
Arcwould change theServices::downloadinterface, which is outside this PR.