Skip to content

perf(render): encode an exported map's PNG at level 4 - #3012

Closed
Mavrick-F wants to merge 1 commit into
opengeos:mainfrom
Mavrick-F:perf/map-png-encoding
Closed

Mavrick-F wants to merge 1 commit into
opengeos:mainfrom
Mavrick-F:perf/map-png-encoding

Conversation

@Mavrick-F

Copy link
Copy Markdown
Member

Summary

When the benchmark draws a DEM to a file, PNG encoding is the longest single-threaded step. render_png used tiny-skia's encoder, which deflates at level 6. It now encodes through the image crate, already a dependency of rustgis-render, at level 4, with each row's filter still chosen by the encoder. The pixels are the same; files are a little larger. It also no longer clones the finished picture before encoding it.

The trade-off

Encoding alone, one thread, median of 7 to 9 encodes on a shared, loaded machine:

Picture Level 6 (before) Level 4 (this PR) File size
The benchmark's DEM, 1600×900 92 ms, 489,647 B 38 ms, 509,967 B +4.1%
An aerial photograph, 1600×900 157 ms, 3,408,489 B 140 ms, 3,411,466 B +0.1%
Sample map, 3840×2160 97 ms, 1,030,142 B 69 ms, 1,044,894 B +1.4%
Sample map, 1600×900 42 ms, 462,831 B 31 ms, 468,331 B +1.2%
100,000 buildings, 1600×900 54 ms, 801,070 B 42 ms, 803,917 B +0.4%

Times from different rows were taken at different loads, so compare within a row. Other settings tried: a fixed "up" filter was quicker on some pictures but made the photograph 8% larger; level 1 and the fast mode made the DEM picture more than twice as large. Encoding on several threads could go further, but needs a PNG writer of our own, so it isn't part of this.

Timing

Whole rustgis-cli run (add the DEM, zoom to it, export 1600×900), release builds, 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. This PR was measured together with #3011, so the middle column is its baseline:

Case main (336a178) With #3011 With #3011 and this PR
DEM, tiled GeoTIFF 267 ms, CPU 0.70 s 250 ms, CPU 0.66 s 204 ms, CPU 0.62 s (1.23×; 1.31× over main)
Same DEM, COG 273 ms, CPU 0.70 s 258 ms, CPU 0.67 s 211 ms, CPU 0.62 s (1.22×; 1.29× over main)

Roughly where the remaining time goes, from a profile: band statistics 50 ms, reading and decoding 40 ms, encoding 35 ms, setting up the coordinate system 30 ms, drawing 20 ms, and the rest in allocating the bands, starting the process and other work.

The COG still draws no faster than the plain GeoTIFF. A raster of this size is held whole, so both decode every cell and the overviews aren't read. Drawing from them would mean reading the file in place (as large tiled GeoTIFFs are since #2772), which also takes statistics and colours from an overview and draws no cast shadows. This PR doesn't change which rasters are read that way.

Output

Decoded pixels are identical between main and the two changes together for map exports of the DEM and its COG, six other GeoTIFF layouts of it, the buildings and the sample map.

Surfaces

Every map image render_png makes: share.exportMap on the desktop, in the browser, on the command line, from the Assistant and MCP, Python's render_png, and animation frames. The raster pictures in a self-contained web map page come from the same function and count against the page's picture limit, so a page within a few percent of that limit could now be refused. A map PDF's one-picture fallback decodes the PNG again, so its size doesn't depend on this. Layout exports and other PNG writers are unchanged. Nothing saved in a project changes.

Testing

UI evidence

No visible UI change: the exported pixels are identical.

Related issue

Part of #1227.

Encoding is the longest single-threaded part of drawing a raster to a
file. The encoder's default, level 6, takes more than twice as long as
level 4 on an elevation model's shades, for a file about 4% smaller. The
pixels are the same.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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 13 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 8 included reviews currently available.

Learn how review limits work.

Review configuration:

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: cbd79e05-2385-483d-bab3-1e7070c231ea
Commits

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

Files selected for processing: 1

  • crates/render/src/lib.rs
  • 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

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, security issues, or AGENTS.md breaks in this change. I left no inline comments.

What I checked

  • encode_map_png is the only new encoder. It is called only from render_png (crates/render/src/lib.rs:8989), which is the path for share.exportMap and the other exports listed in the PR.
  • The image dependency already enables the png feature at the workspace level, so no new dependency is added. The png codec is pure Rust, so the wasm build should be unaffected.
  • The Pixmap import and take_demultiplied call type-check by reading. The encoder settings (CompressionType::Level(4), FilterType::Adaptive, ExtendedColorType::Rgba8) match the image 0.25 API.
  • The new test's premultiplied pixel values satisfy channel <= alpha. It compares decoded pixels from tiny-skia's encode_png with the new encoder, which is the right check for "same pixels".
  • Twins: the other PNG writers (render_placed GeoTIFF path, layout, tiles, raster export, SVG, and the engine's encode_png calls) are not the same export path. The PR says they are unchanged on purpose, and that is consistent with the diff.

Limits of this review

  • I could not read the tiny-skia 0.12 source, because no cargo registry is on disk. I did not confirm whether take_demultiplied works in place or allocates a second buffer. If it allocates, the picture is briefly held twice. The old encode_png also built a full RGBA copy, so this is probably not a regression. I did not check it.
  • I could not run the tests or the wasm build (no shell).

Low-confidence notes (not posted)

  • AGENTS.md "Size for real data" asks for a memory multiple in the PR description when stored or copied data changes. This is a rendered picture, not layer data, so it probably doesn't apply. The PR does not state a multiple.
  • The PR says a self-contained web map page near its picture limit could now be refused, because PNGs are about 0.1% to 4% larger. This is a refusal with an error, not silent data loss, and the author has disclosed it.

Prior reviews: none exist on this PR, so no earlier concerns were left alone.

Change size

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

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 874.5 KB
Size after 875.9 KB
Total size change +1.4 KB
Path Size before Size after Size change
crates/render/src/lib.rs 874.5 KB 875.9 KB +1.4 KB

@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3012/
Web app https://preview.rustgis.app/pr-3012/app/
Commit 9e6b3de

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

Closing: this only speeds up map export, not drawing in the app, and the exported files grow by up to 4.8%. Not worth the trade.

@Mavrick-F Mavrick-F closed this Nov 26, 2026
@Mavrick-F
Mavrick-F deleted the perf/map-png-encoding branch November 26, 2026 13:14
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