perf(shapes): vectorize datashader polygon scaling#738
Merged
Conversation
The datashader path scaled polygons with a per-geometry affinity.scale loop (_geometry[is_polygon].apply(lambda g: affinity.scale(...))) — a pure-Python loop that dominates large polygon renders (measured ~60% of a 100k-polygon render; the prototype's 33s at 1M is almost entirely this loop). Replace with _scale_geometries: scale every coordinate about each geometry's bounding-box centre (affinity.scale's default origin) in one vectorized pass via shapely.get_coordinates/set_coordinates. Byte-identical to affinity.scale including asymmetric shapes, multipolygons and holes (verified main-vs-branch, diff_px=0); ~12x polygons / ~6.5x multipolygons -> ~2x+ end-to-end at 100k, growing with n. Only fires for scale != 1.0. The affine transform loop is left as-is (vectorizing it is ~0.9x: shapely coordinate (de)serialization dominates, not the Python callback).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #738 +/- ##
==========================================
+ Coverage 79.26% 79.32% +0.05%
==========================================
Files 17 17
Lines 4596 4604 +8
Branches 1028 1030 +2
==========================================
+ Hits 3643 3652 +9
+ Misses 603 602 -1
Partials 350 350
🚀 New features to boost your workflow:
|
This was referenced Jun 22, 2026
timtreis
added a commit
that referenced
this pull request
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On the datashader shapes path, polygon/multipolygon scaling used a per-geometry Python loop:
affinity.scalebuilds a new geometry per shape — a pure-Python O(n) loop that dominates large polygon renders: measured ~60% of a 100k-polygon datashader render (and ~33 s at 1M is almost entirely this loop).Fix
_scale_geometries: scale every coordinate about each geometry's bounding-box centre (affinity.scale's default origin) in one vectorized pass viashapely.get_coordinates/set_coordinates.Byte-identical
Verified main-vs-branch on a 2000-polygon datashader render at
scale=0.6andscale=2.0→ diff_px=0, max|d|=0. Unit testtest_scale_geometries_matches_affinity_scalelocks the helper againstshapely.affinity.scalefor asymmetric polygons (bbox-centre ≠ centroid), multipolygons, and holes (tolerance 1e-9).Impact
scale != 1.0(default skips it; circles use the fast-path, not this branch).transformloop is left as-is: vectorizing it is ~0.9× (shapely coordinate (de)serialization dominates, not the Python callback).