Setting a collections transform slows down rendering by a factor of ~two

Hello,

I’m the developer of mosaic, a python package for unstructured MPAS mesh visualization which is heavily reliant on cartopy.

I use matplotlib.collections.Polycollection’s to draw the N-sided polygons that comprise an MPAS mesh. As part of the rendering process, I’ve been setting the collection transform:

but I’ve noticed that this significantly slows down rendering times.

Low Resolution Ocean Mesh (465,044 cells)

With polycollection transform set:

$ hyperfine ./IcoswISC30E3r5.py
Benchmark 1: ./IcoswISC30E3r5.py
  Time (mean ± σ):     13.439 s ±  0.251 s    [User: 12.580 s, System: 0.451 s]
  Range (min … max):   12.939 s … 13.840 s    10 runs

Without polycollection transform set:

$ hyperfine ./IcoswISC30E3r5.py
Benchmark 1: ./IcoswISC30E3r5.py
  Time (mean ± σ):      7.016 s ±  0.108 s    [User: 6.244 s, System: 0.432 s]
  Range (min … max):    6.841 s …  7.164 s    10 runs
Here's the `IcoswISC30E3r5.py` sciprt, which will download the low resolution mesh, for reference
#!/usr/bin/env python

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import mosaic

projection = ccrs.PlateCarree()
transform = ccrs.Geodetic()

ds = mosaic.datasets.open_dataset("mpaso.IcoswISC30E3r5")

descriptor = mosaic.Descriptor(ds, projection=projection, transform=transform)

fig, ax = plt.subplots(subplot_kw={"projection": projection})

mosaic.polypcolor(ax, descriptor, ds.bottomDepth)

fig.savefig("IcoswISC30E3r5.png", dpi=300, bbox_inches='tight')

The only difference between these two call is whether the lines linked above are commented out.

In mosaic we project the mesh coordinates ourselves and take special care to handle the projection boundary. Any time a GeoAxes figure is made, the coordinates of the mesh, as stored in our mosaic.Descriptor object, will already be projected to the figure’s projection. This means you cannot plot a mesh with coordinates in a different projection than the figure, but this is intentionally unsupported because dynamically projecting each individual cell in a mesh is expensive. If you wanted to make a figure with different subplots having different projection, you’d need to make a descriptor instance for each projection. (This is a little messy, but a pretty rare use-case for us).

Given the rendering overhead and that we handle mesh projection ourselves, I’m wondering if I really need to set the collection’s transform when we plot? Are there any unintended consequence of this that I might not be aware of? From visual inspection of figure with and without the transform set, they seem identical. I came across this issue: Proposal for faster transform option · Issue #2104 · SciTools/cartopy · GitHub, which seems related. Any guidance would be greatly appreciated.

P.S. I decided to post this here instead of as an Issue on github, since I don’t really think this is an issue per se. But, if that’s a better place for this I’m happy to move it over.

Thanks!