Skip to content

perf(data): place a decoded GeoTIFF strip or tile a row at a time - #3011

Merged
Mavrick-F merged 1 commit into
opengeos:mainfrom
Mavrick-F:perf/geotiff-chunk-placement
Nov 26, 2026
Merged

Mavrick-F merged 1 commit into
opengeos:mainfrom
Mavrick-F:perf/geotiff-chunk-placement

Conversation

@Mavrick-F

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

Copy link
Copy Markdown
Member

Summary

A GeoTIFF read from a file is decoded on several threads, which take turns, behind one lock, copying each decoded strip or tile into the bands. That copy went a sample at a time, so it held the other threads up.

When every pixel is kept and there are no rounded values to count, Chunks::place now copies a run of a row at a time: copy_from_slice for one sample per pixel, one strided pass per band for interleaved samples. Reads that keep every k-th pixel, and reads that count rounded values, take the old loop.

In a profile of drawing the benchmark's 8.9-million-cell DEM, placing took about 45 ms of CPU, all of it under the lock. It now takes under 1 ms.

Timing

Whole rustgis-cli run, release builds of main (336a178) and this change, 25 runs each with the builds taking turns, through the job queue on a shared 8-thread machine (load average about 9). Median wall and CPU time:

Case main This PR
Load a 115-million-cell DEM (layer.addData) 1421 ms, CPU 5.96 s 1105 ms, CPU 5.58 s (1.29×)
Draw the 8.9-million-cell DEM, tiled GeoTIFF 267 ms, CPU 0.70 s 250 ms, CPU 0.66 s
Draw the same DEM from a COG 273 ms, CPU 0.70 s 258 ms, CPU 0.67 s

An earlier set of 11 runs, at 10 ms resolution, put the large load at 1.68 s and 1.48 s (1.14×), so read the factor as that range. The rest of the load is mainly decompression and the predictor, band statistics, and setting up the coordinate system.

Output

share.exportRaster (plain GeoTIFF) writes byte-identical files from main and this change for the DEM, its COG, and copies of it that are striped, LZW, Int32, Float64, three bands interleaved by pixel or stored apart (tiled), and three Int16 bands in strips.

Surfaces and memory

Every GeoTIFF read a strip or tile at a time takes this path: desktop, command line, Python, browser and phone (one thread in the browser, with the same cheaper copy). No new allocation and no copy of layer data. Nothing saved changes.

Testing

  • decimation_keeps_the_same_pixels_whatever_the_chunk_shape already covers the new path at k = 1: several chunk shapes, padded or not, two samples a pixel, whole grids and parts of them.
  • The output comparison above.
  • The full CI gate passed on a fork: https://github.com/Mavrick-F/RustGIS/actions/runs/45760686426 (cd42a90)
  • cargo fmt --all --check passed.

UI evidence

No visible UI change.

Related issue

Part of #1227.

Summary by CodeRabbit

  • Performance
    • Full-resolution raster reads may complete faster in cases where rounded-sample tracking is not needed.

)

Threads reading a GeoTIFF take turns placing the chunks they decode, and
each chunk was placed a sample at a time. With every pixel kept and no
rounded values to count, copy whole runs of a row instead.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Chunks.place now copies contiguous row segments for full-resolution reads when rounded-sample tracking is absent. It handles planar and interleaved samples separately. Decimated reads and reads with rounded-sample tracking continue through the existing per-pixel path.

Changes: GeoTIFF chunk placement

Layer / File(s) Summary
Full-resolution row copy
crates/data/src/raster/geotiff.rs
When k == 1 and changed is absent, Chunks.place copies contiguous row segments into planar or interleaved output bands, then returns. Other cases use the existing per-pixel path.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related issues

  • Issue 1227 tracks raster performance gaps, including DEM loading and COG drawing. This change targets GeoTIFF chunk placement, which is related to those performance objectives.

Suggested reviewers: giswqs, pick2510

Merge Risk: Minimal · up to cd42a

This change speeds up placing decoded GeoTIFF strips and tiles by copying whole rows when every pixel is kept. The code trace shows the same pixels land in the same output slots as before, so no merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: placing decoded GeoTIFF strips or tiles a row at a time, which is the performance fast path in Chunks::place. It follows the conventional type(scope) prefix and is spe…
Description check Passed The description follows the template. It has Summary, Testing, UI evidence and Related issue sections. The UI section correctly states no visible UI change. It explains the change, the benchmark timin…
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.
Full details: Title check

Explanation

The title names the main change: placing decoded GeoTIFF strips or tiles a row at a time, which is the performance fast path in Chunks::place. It follows the conventional type(scope) prefix and is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description follows the template. It has Summary, Testing, UI evidence and Related issue sections. The UI section correctly states no visible UI change. It explains the change, the benchmark timings, the byte-identical export check, the existing decimation test, and the CI and fmt results. The Related issue section names Part of #1227.

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 hops beside the rows,
Where full-speed GeoTIFF data flows.
Planar bands and woven bytes,
Copy swiftly through the pixel sites.
The old path waits for other reads.

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: 04983013-9074-484e-816d-b7c0066b2c36
Commits

Reviewing files that changed from the base of the PR and between a757b09 and cd42a90.

Files selected for processing: 1

  • crates/data/src/raster/geotiff.rs

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

for y in ys..ye {
let src = ((y - y0) as usize * stride + left) * per_pixel;
let dst = (y - sr) as usize * ow + (xs - sc) as usize;
let Some(src) = values.get(src..src + width * per_pixel) else { continue };

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.

Hardening nit on the new run-of-a-row path. The arithmetic here is unchecked (src + width * per_pixel, and the (y - y0) as usize * stride on line 1205), while the loop below this block keeps the same kind of position math checked and says so in a comment. The values are bounded by values.len() after the shape match, so this can't overflow in practice. Using saturating arithmetic here keeps the two paths consistent and avoids a wrap in release builds if the shape logic changes later:

Suggested change
let Some(src) = values.get(src..src + width * per_pixel) else { continue };
let Some(src) = values.get(src..src.saturating_add(width.saturating_mul(per_pixel))) else { continue };

The continue also drops a whole row without a notice if it is ever reached. The shape check above makes that unreachable, and the old per-pixel loop had the same silent skip, so this is not a regression. Returning an error here would match the "never silent" rule if you want the path to fail loudly instead.

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. The shape check above the new path ties these values to the decoded chunk's own length, so the products stay within it and the row is never skipped.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs: None found. I checked the new run-of-a-row path in Chunks::place (crates/data/src/raster/geotiff.rs:1202-1221) against the loop it skips, for k = 1 and changed = None:

  • Bounds: Span::kept never returns a start below the chunk's corner or the span's start, so xs - x0, xs - sc and ys - y0 can't underflow.
  • Layout: the source and destination indices match the old per-pixel formulas at k = 1.
  • Slices: the copy_from_slice lengths are equal by construction, so it can't panic.
  • Interleaved samples: the skip(b).step_by(per_pixel) zip writes the same values as the old loop.
  • Planar: samples is 1 when planar, so the single-sample copy is used and each band gets the same values.
  • Callers: both the serial and parallel reads (decimate and decimate_parallel) go through place with changed = None on the new path, so they behave the same.

Confidence: high.

Security: None found. The change is pure index arithmetic on data already bounded by the shape check.

Performance: No new allocation or copy. The speed-up is plausible (a copy_from_slice under the lock instead of a per-sample loop). I couldn't measure the timings here.

Quality:

  • geotiff.rs:1207 (inline comment posted): the new block uses unchecked src + width * per_pixel and (y - y0) as usize * stride, while the loop below keeps checked arithmetic. Values are bounded by values.len(), so this is hardening, not a bug. The row-level continue would drop a row silently if it were ever reached, though the shape check makes that unreachable and the old per-pixel loop did the same. Confidence: high that it is harmless, medium that it is worth tidying.

AGENTS.md: No violations found.

  • No unwrap, expect, panic! or unsafe added.
  • No new cfg or platform-specific code, so the wasm build is unaffected.
  • No new layer, command, or saved-file change, so the schema and command-surface rules don't apply.
  • Span::kept and Chunks::place are existing helpers. The change is a fast path inside place, so the rule about sizing for real data doesn't add a copy.

Twins checked: The change sits in Chunks::place, the shared placer. Its two callers (decimate and decimate_parallel) both reach the new path, and the browser build uses the same code with one thread. The overlay tools, the layer.add* and project.* commands, and the other raster readers don't call place, so they don't need the change. The COG and LZW read routes go through decimate as well.

Tests: decimation_keeps_the_same_pixels_whatever_the_chunk_shape covers k = 1 across several chunk shapes, padded and unpadded, with two samples per pixel. The planar k = 1 path is covered by the file-level Example { bands: 2, planar: true } case. I didn't run the tests, since I have no shell here.

Prior reviews: The review-thread file is empty, so there are no earlier concerns to skip.

Change size

Compared with the base branch: the whole PR against main (merge base 336a178 to PR head cd42a90).

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 292.6 KB
Size after 292.2 KB
Total size change +1.2 KB

Hmm, that table is wrong. Corrected values follow.

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 292.6 KB (299,593 B)
Size after 293.8 KB (300,783 B)
Total size change +1.2 KB (+1,190 B)
Path Before After Change
crates/data/src/raster/geotiff.rs 292.6 KB 293.8 KB +1.2 KB

@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3011/
Web app https://preview.rustgis.app/pr-3011/app/
Commit cd42a90

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

@Mavrick-F

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Full review finished.

@Mavrick-F
Mavrick-F added this pull request to the merge queue Nov 26, 2026
Merged via the queue into opengeos:main with commit 1982875 Nov 26, 2026
28 checks passed
@Mavrick-F
Mavrick-F deleted the perf/geotiff-chunk-placement branch November 26, 2026 06:36
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