Repository navigation
perf: fewer pixel copies when translating a GeoTIFF read by window - #597
Conversation
A lazy translate to a GeoTIFF copied each strip of pixels several more times than it needed, each copy into freshly mapped memory: - translate builds the nodata mask only when rescaling or resampling uses it; a nearest-neighbour copy never read it. - A resampled read made in one part (a window copy) keeps that part's buffer instead of placing it into a second zeroed one (resample_bands, read_resampled_by_band). - translate converts the pixels of a dataset it owns where they are when the output copies all of it, every band in order, in its type, by nearest neighbour and not rescaled (translate_owned, used by translate_lazy and translate_lazy_to_file), with the conversion the copy applies: a float32 signaling NaN is still quieted and UInt64 values still go through f64. - GeoTiffWriter::write_rows encodes the rows given where they are when none are pending, and all of the pending rows without a further copy into a slab, as the COG writer does; it never copies more than before, and its peak memory stays at or below main's. Two error paths of GeoTiffWriter::write_rows (GeoTIFFs other than COGs) change: rows of another type, even none, are refused before their mask rows are queued (a refused call left them in front of the next call's), and once a block row fails to be written the writer refuses more rows and finish, which could write a file missing it. Outputs are byte-identical to main's: 87 translate, dem and warp cases compared with cmp, over every sample type, masks, overviews, VRT, PNG and COG. Translating a 14412 x 14412 float32 Deflate GeoTIFF to an uncompressed GeoTIFF on one CPU takes 67% fewer page faults.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 4 minutes. View limit details
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Code reviewBugs
Security
Performance
Quality
CLAUDE.md (AGENTS.md)
Change sizeCompared: the whole PR against main (merge base 2518f3a to PR head 917a244).
|
Code reviewI found no correctness, security or performance problems in the changed code. I read the full diff and the surrounding code in Checked and found correct:
Quality (nits):
Performance: no regression found. On the failure path, Security: no issues found. The change adds no unsafe code and no input-handling surface. CLAUDE.md (AGENTS.md): I checked identifier naming, the regression test, CHANGELOG placement, and that no ported code needs SPDX or NOTICE entries. No public API changed, so Python and JS bindings are unaffected. Not verified here: Change sizeCompared: the whole PR against main (merge base 6628390 to PR head 781a6c1).
|
Code reviewI read the full diff and the surrounding code for the translate in-place path, the resampled read buffers, and the GeoTIFF Bugs
Security
Performance
Quality
CLAUDE.md / AGENTS.md
One inline comment I tried to place on Change sizeCompared: the whole PR against main (merge base 6cf73c7 to PR head 504c04b).
|
Summary
terrio translateof a large tiled GeoTIFF to a GeoTIFF copied each strip of pixels four more times than it needed, and wrote each copy into freshly mapped memory. This PR removes those copies in the shared read, translate and GeoTIFF row-writing code. Outputs are byte-for-byte the same as main's. It also fixes two error paths ofGeoTiffWriter::write_rows: a refused call's mask rows were queued, and after a block row failed to be written,finishcould still write a file missing it.On one CPU, copying a 14412 x 14412 float32 Deflate GeoTIFF (256 x 256 tiles) to an uncompressed GeoTIFF takes 2.82 s instead of 4.63 s, with 438,000 minor page faults instead of 1.31 million. Peak memory goes down from 179 MB to 126 MB.
This PR is one squashed commit. The commit hashes cited below are from the reviewed history on
nkarasiak/terrio:perf/translate-read-copies.What changed
Each strip of a lazy
translatewent through these buffers (strips of 512 rows, 29.5 MB here):LazyRaster::read_resampled)translateBandView::getGeoTiffWriter::write_rows(GeoTIFFs other than COGs)The commits:
perf(translate): build the nodata mask only when rescaling or resampling uses it.perf(read): keep a read made in one part instead of copying it (resample_bands,read_resampled_by_band).perf(translate): convert the pixels of a dataset it owns where they are (translate_owned, used bytranslate_lazyandtranslate_lazy_to_file). The in-place pass applies the same conversion as the copy (convert_value(x as f64, type)), so a float32 signaling NaN is still quieted and UInt64 values still go throughf64, as on main.perf(gtiff): write the rows given to a GeoTIFF where they are, as the COG writer already does (append_rows,slice_rows).fix(gtiff): refuse rows of another type before queueing their mask rows. On main, a refused call left its mask rows in front of the next call's, so the next rows were written with the wrong mask. The same commit keeps the writer's peak memory at main's when rows are pending and only some of them are written.fix(gtiff): an intermediate fix of commit 5's failure path, superseded by 7; it also documents that only a COG refuses a mask it has no room for.fix(gtiff): after a failed write, every row given so far stays pending with its mask rows, as on main (commit 5 had leftpending_rowscounting rows the emptied buffer no longer held, so the next call panicked). An empty write of another type is refused, as the COG writer refuses it.fix(gtiff)(899842c, with the test b2d29aa): once a block row of a GeoTIFF other than a COG fails to be written, the writer refuses more rows andfinish. On main,finishcounted the failed row's pending rows as written and wrote the directories, so the file could miss that row, and a retry could add the row's chunks after part of it. When rows of an earlier call are left pending (strips not aligned on the output's block rows),write_rowsmakes at most as many copies as main.Other commits add the CHANGELOG entry and say which checks
write_rowsmakes whenrowsis zero (# Errors). Two merges bring in main at e058043 (35f8271: fdeflate decoding, #577) and at 2518f3a (cf9e045: libdeflate writes and in-place raster writes, #589), and two update the CHANGELOG's fault figure measured after each.The public
translate(&RasterDataset, …)is unchanged; only callers that own the dataset take the in-place path. No public API changes. COGs written throughGeoTiffWriterstill copy their rows (RowWriter::Cogclones them); the COG outputstranslatestreams useCogWriterdirectly, which already took its rows as they are.Measurements
terrio translate dem16.tif out.tif: 14412 x 14412, Float32, Deflate, 256 x 256 tiles, nodata -999999; output uncompressed, striped. One warm-up per tool, then 7 (11 on all cores) rounds in rotated order. Wall time is the median and range; user, sys, minor faults and peak RSS are medians. main is 2518f3a, the main this branch merges, for the one-CPU table; the all-core and per-case tables were measured against e058043, before #589, and were not rerun. Both builds use the default release profile. GDAL isgdal_translate3.13.3.One CPU (
taskset -c 11,RAYON_NUM_THREADS=1,GDAL_NUM_THREADS=1), load average 0.99 (0.50–1.62, from background services; 32 cores):All 32 cores (
GDAL_NUM_THREADS=ALL_CPUS), against e058043, load average 2.88 (1.51–3.23):All-core wall times spread widely for both terrio builds (the 830 MB output is written through the page cache).
Peak RSS (MB) and minor faults of other cases against main@e058043, all cores, median of 3 runs (10 alternating runs for the two Deflate outputs, whose peak varies most from run to run):
translate dem16translate --ot Float64 dem16translate --srcwin 1000 2000 5000 3000 dem16translate --outsize 50% 50% dem16translate --outsize 3000 3000 -r bilinear dem16translate --scale 0 4000 0 255 --ot Byte dem16translate --compress DEFLATE dem16translate --co TILED=YES --compress DEFLATE dem16translate -f COG dem16dem slope dem16dem hillshade dem16warp --t-srs EPSG:3857 USGS_1_n39w105.tifThe
--scalecase reads the whole raster and goes through the unchangedtranslate; its 3 MB is within the 0–3 MB more that the branch's binary uses forterrio info, which reaches none of the changed code, and its fault counts vary by about as much between runs (428,000–466,000).Before the merge, with zlib-rs decoding, Deflate went from about 40% to about 61% of the single-CPU cycles;
BandView::valid,BandView::get,SourceRead::readandfallible::filledzero-fills no longer appear in the profile's top entries.Validation
cmp, with.aux.xmlsidecars): 87 cases, also run against e058043 and 55963cc before the earlier merges; 5 more cases fail identically on both builds ("band 3 does not exist"). They cover translate of dem16 (default,--ot Float64,-b 1, integer and fractional--srcwin,--a-nodata,--a-ullr,--outsize 50%, bilinear--outsize,--scale, Deflate, tiled Deflate, COG),dem slope,dem hillshadeand a warp to EPSG:3857, which use the same GeoTIFF row writer. They also cover the float-read test files in every sample type (UInt64, Int64 and NaN included, whole and windowed), the resampled-read test files (RGB with mask, RGBA, paletted, external overviews, sidecar mask; whole,--overview 0, average to half size, reordered bands), a VRT of mixed types, PNG output and input, and a COG.translate_in_place_converts_as_a_copy: the in-place path gives the bits the copy gives, for float32 with signaling and negative NaNs and a nodata pixel, and UInt64 past 2^53, with new nodata, bounds, explicit bands, type and window; it also checks which options keep the pixels' buffer. Red-checked: with the in-place conversion removed the signaling NaN differs, and with the in-place path disabled the buffer check fails.failed_row_writes_stop_the_writer(gtiff_write tests): after a flush refused by the 4 GiB check, every row given so far is pending, and a later write andfinishare refused even once the file is back within the limit. Red-checked: without the refusal, the later write succeeds. A failed last block row, whose rows are all given, makesfinishfail (b2d29aa).internal_masks_and_rows_written_in_order(georeferencing tests) now also checks that rows of another type are refused and that a refused call's mask rows are not kept: a refused call is made before every row write, with and without rows pending, and the files' masks are read back. Red-checked: it fails with main's writer (wrong mask).cargo test -p terrio -p terrio-cliat the merge of 2518f3a: 2,953 passed, 0 failed, 54 ignored (146 test binaries).cargo fmt --check, workspace Clippy with-D warnings, the unsafe allowlist check, NOTICE andcargo check --target wasm32-unknown-unknownpass.maturin develop, imported from this worktree): 5,892 passed, 736 skipped, 1 failed. The failure istest_compat_nodata_warnings_match_rasterios, a known false positive in this environment.Limitations
translatestill converts UInt64 and Int64 values throughf64, as main does, so values past 2^53 are rounded even by an identity translate; the in-place path keeps that, to stay byte-identical with main.🤖 Generated with Claude Code
Summary by CodeRabbit
Performance
Bug Fixes