From cdb8f8a872047dd76107c85b60488bbcdc009476 Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 26 Nov 2026 15:21:27 -0600 Subject: [PATCH 1/3] perf(data): a byte GeoTIFF's samples are checked and made on the strip threads An 8-bit band written as bytes was looked over and converted on one thread before its strips were compressed: about 0.65 s of a 115-million- cell hillshade's encode. The look runs on several threads with an exact conversion test instead of fract(), and each strip converts its own cells on the thread that compresses it, so no byte copy of the band is made first. The file is the same, byte for byte. --- crates/data/src/raster/geotiff.rs | 45 +++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index a9a1ef67d5..678113e4b1 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -2298,11 +2298,22 @@ fn put( data: &[T], le: fn(T) -> [u8; N], out: &mut std::io::Cursor>, +) -> Result<()> { + put_cells(img, data, |v| le(*v), out) +} + +/// [`put`], each cell of `data` written as `cell` gives it: on the threads that make the strips, +/// with no copy of the samples made first. +fn put_cells( + img: super::cogwrite::Strips, + data: &[T], + cell: impl Fn(&T) -> [u8; N] + Sync, + out: &mut std::io::Cursor>, ) -> Result<()> { let per_row = img.w as usize * usize::from(img.spp); super::cogwrite::write_strips(img, out, |rows, buf| { let s = data.get(rows.start * per_row..rows.end * per_row).ok_or_else(|| err("the samples don't fill the image"))?; - buf.extend(s.iter().flat_map(|v| le(*v))); + buf.extend(s.iter().flat_map(&cell)); Ok(()) }) } @@ -2471,17 +2482,17 @@ fn encode(r: &Raster, band: usize, every: bool) -> Result> { let b = r.bands.get(band).ok_or_else(|| err(format!("the raster has no band {}", band + 1)))?; // An 8-bit band is written as bytes when every value is a whole byte and an empty cell is // only where no-data can mark it (as the multiband samples above); otherwise as floats. - let byte = |v: &f32| if r.is_valid(*v) { v.fract() == 0.0 && (0.0..=255.0).contains(v) } else { r.nodata.is_some() }; - if r.data_type == "UInt8" && fits_bytes(r, band) && b.data.iter().all(byte) { + // A whole number from 0 to 255 is the one value a byte holds that converts back to itself. + // The look over the band is one pass on several threads with no look at Cancel inside it: + // looked at after it. + let byte = |v: &f32| if r.is_valid(*v) { f32::from(*v as u8) == *v } else { r.nodata.is_some() }; + let all_bytes = + || super::on_runs(&b.data, super::threads_for(b.data.len()), |_, run| run.iter().all(byte)).into_iter().all(|fits| fits); + if r.data_type == "UInt8" && fits_bytes(r, band) && all_bytes() { + crate::progress::check()?; let nodata = r.nodata.map_or(0, |d| d as u8); - let mut data = super::room(b.data.len(), w as usize, h as usize)?; - for (i, v) in b.data.iter().enumerate() { - if i % (1 << 20) == 0 { - crate::progress::check()?; - } - data.push(if r.is_valid(*v) { *v as u8 } else { nodata }); - } - put(image(&[band], Sample::Byte, Vec::new())?, &data, u8::to_le_bytes, &mut out)?; + let cell = |v: &f32| [if r.is_valid(*v) { *v as u8 } else { nodata }]; + put_cells(image(&[band], Sample::Byte, Vec::new())?, &b.data, cell, &mut out)?; } else { put(image(&[band], Sample::Float, Vec::new())?, &b.data, f32::to_le_bytes, &mut out)?; } @@ -2919,6 +2930,18 @@ mod tests { assert_eq!((back.value(0, 0, 0), back.value(0, 1, 0), back.value(0, 2, 0)), (Some(1.0), None, Some(255.0))); assert_eq!(byte(vec![1.0, f32::NAN, 2.0], None).data_type, "Float32"); assert_eq!(byte(vec![1.0, 2.5, 2.0], Some(0.0)).data_type, "Float32"); + // A band long enough to be looked at on several threads: one fraction in its last run + // keeps it floats. + let n = 4 * super::super::PARALLEL_VALUES; + let long = |last: f32| { + let mut cells: Vec = (0..n).map(|i| (i % 256) as f32).collect(); + cells[n - 2] = last; + let r = Raster::new("g", n as u32, 1, vec![cells], gt, Crs::LonLat, Some(0.0), ColorHint::Gray, "UInt8").unwrap(); + read(std::io::Cursor::new(write(&r, 0).unwrap()), "back").unwrap() + }; + let back = long(7.0); + assert_eq!((back.data_type.as_str(), back.value(0, n as i64 - 2, 0)), ("UInt8", Some(7.0))); + assert_eq!(long(7.5).data_type, "Float32"); } #[test] From c981b5ed59a7b524eccd6621996c81558c6bc45e Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 26 Nov 2026 15:55:57 -0600 Subject: [PATCH 2/3] perf(data): encode says its byte look has no Cancel inside; the test reads every cell back The note on the look over a single 8-bit band moves into encode's doc comment. The long band's test checks every cell reads back where it was written, so strips made on several threads out of order would fail it. --- crates/data/src/raster/geotiff.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 678113e4b1..1e79afdeb0 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -2318,7 +2318,9 @@ fn put_cells( }) } -/// [`write`] (or with `every`, [`write_all`]). +/// [`write`] (or with `every`, [`write_all`]). Whether a single 8-bit band's cells are all bytes +/// is one look over the band, on several threads, with no look at Cancel inside it: Cancel is +/// looked at after it, and between strips as they are written. fn encode(r: &Raster, band: usize, every: bool) -> Result> { use super::cogwrite::{Sample, Strips, few_classes, geo_entries}; // A scan's place is only where it was laid on the view: not coordinates to write into a file. @@ -2483,8 +2485,6 @@ fn encode(r: &Raster, band: usize, every: bool) -> Result> { // An 8-bit band is written as bytes when every value is a whole byte and an empty cell is // only where no-data can mark it (as the multiband samples above); otherwise as floats. // A whole number from 0 to 255 is the one value a byte holds that converts back to itself. - // The look over the band is one pass on several threads with no look at Cancel inside it: - // looked at after it. let byte = |v: &f32| if r.is_valid(*v) { f32::from(*v as u8) == *v } else { r.nodata.is_some() }; let all_bytes = || super::on_runs(&b.data, super::threads_for(b.data.len()), |_, run| run.iter().all(byte)).into_iter().all(|fits| fits); @@ -2933,14 +2933,21 @@ mod tests { // A band long enough to be looked at on several threads: one fraction in its last run // keeps it floats. let n = 4 * super::super::PARALLEL_VALUES; - let long = |last: f32| { + let cells = |last: f32| { let mut cells: Vec = (0..n).map(|i| (i % 256) as f32).collect(); cells[n - 2] = last; - let r = Raster::new("g", n as u32, 1, vec![cells], gt, Crs::LonLat, Some(0.0), ColorHint::Gray, "UInt8").unwrap(); + cells + }; + let long = |last: f32| { + let r = Raster::new("g", n as u32, 1, vec![cells(last)], gt, Crs::LonLat, Some(0.0), ColorHint::Gray, "UInt8").unwrap(); read(std::io::Cursor::new(write(&r, 0).unwrap()), "back").unwrap() }; + // Every cell reads back where it was, so the strips made on several threads are in order. let back = long(7.0); - assert_eq!((back.data_type.as_str(), back.value(0, n as i64 - 2, 0)), ("UInt8", Some(7.0))); + assert_eq!(back.data_type, "UInt8"); + let written = cells(7.0); + let wrong = written.iter().enumerate().find(|&(i, &v)| back.value(0, i as i64, 0) != Some(v).filter(|v| *v != 0.0)); + assert!(wrong.is_none(), "{wrong:?}"); assert_eq!(long(7.5).data_type, "Float32"); } From 527e113b20121e43601d89c1f3b206d9087d324d Mon Sep 17 00:00:00 2001 From: mavrick Date: Thu, 26 Nov 2026 16:26:49 -0600 Subject: [PATCH 3/3] test(data): the long byte band spans several strips, each with other values A 2048 x 2048 band is written as four strips whose values differ (they repeat every 251 cells), so strips out of order fail the cell check. The looks over its runs are read with iter(). --- crates/data/src/raster/geotiff.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/data/src/raster/geotiff.rs b/crates/data/src/raster/geotiff.rs index 1e79afdeb0..c63f1b7fb8 100644 --- a/crates/data/src/raster/geotiff.rs +++ b/crates/data/src/raster/geotiff.rs @@ -2487,7 +2487,7 @@ fn encode(r: &Raster, band: usize, every: bool) -> Result> { // A whole number from 0 to 255 is the one value a byte holds that converts back to itself. let byte = |v: &f32| if r.is_valid(*v) { f32::from(*v as u8) == *v } else { r.nodata.is_some() }; let all_bytes = - || super::on_runs(&b.data, super::threads_for(b.data.len()), |_, run| run.iter().all(byte)).into_iter().all(|fits| fits); + || super::on_runs(&b.data, super::threads_for(b.data.len()), |_, run| run.iter().all(byte)).iter().all(|&fits| fits); if r.data_type == "UInt8" && fits_bytes(r, band) && all_bytes() { crate::progress::check()?; let nodata = r.nodata.map_or(0, |d| d as u8); @@ -2930,23 +2930,30 @@ mod tests { assert_eq!((back.value(0, 0, 0), back.value(0, 1, 0), back.value(0, 2, 0)), (Some(1.0), None, Some(255.0))); assert_eq!(byte(vec![1.0, f32::NAN, 2.0], None).data_type, "Float32"); assert_eq!(byte(vec![1.0, 2.5, 2.0], Some(0.0)).data_type, "Float32"); - // A band long enough to be looked at on several threads: one fraction in its last run + // A band long enough to be looked at on several threads, and written as several strips + // that each hold other values (they repeat every 251 cells): one fraction in its last run // keeps it floats. - let n = 4 * super::super::PARALLEL_VALUES; + let side = 2048; + let n = side * side; + assert!(n >= 4 * super::super::PARALLEL_VALUES); let cells = |last: f32| { - let mut cells: Vec = (0..n).map(|i| (i % 256) as f32).collect(); + let mut cells: Vec = (0..n).map(|i| (i % 251) as f32).collect(); cells[n - 2] = last; cells }; let long = |last: f32| { - let r = Raster::new("g", n as u32, 1, vec![cells(last)], gt, Crs::LonLat, Some(0.0), ColorHint::Gray, "UInt8").unwrap(); + let r = Raster::new("g", side as u32, side as u32, vec![cells(last)], gt, Crs::LonLat, Some(0.0), ColorHint::Gray, "UInt8") + .unwrap(); read(std::io::Cursor::new(write(&r, 0).unwrap()), "back").unwrap() }; // Every cell reads back where it was, so the strips made on several threads are in order. let back = long(7.0); assert_eq!(back.data_type, "UInt8"); let written = cells(7.0); - let wrong = written.iter().enumerate().find(|&(i, &v)| back.value(0, i as i64, 0) != Some(v).filter(|v| *v != 0.0)); + let wrong = written + .iter() + .enumerate() + .find(|&(i, &v)| back.value(0, (i % side) as i64, (i / side) as i64) != Some(v).filter(|v| *v != 0.0)); assert!(wrong.is_none(), "{wrong:?}"); assert_eq!(long(7.5).data_type, "Float32"); }