Skip to content

perf(labels): compute np.unique(label) once per render#693

Merged
timtreis merged 1 commit into
mainfrom
perf/labels-unique-once
Jun 6, 2026
Merged

perf(labels): compute np.unique(label) once per render#693
timtreis merged 1 commit into
mainfrom
perf/labels-unique-once

Conversation

@timtreis
Copy link
Copy Markdown
Member

@timtreis timtreis commented Jun 6, 2026

What & why

Third item from the profiling write-up in #690 (after #691 shapes and #692 points).

_render_labels uniquified the (already-rasterized) label raster up to twice per render:

  • once for the instance ids (np.unique(label)) or, when a table annotates the element,
    the overlap check (np.unique(label.values));
  • again for the rasterize mask (labels_in_rasterized_image = np.unique(label.values)).

Both run on the same array, so the second pass is redundant.

Change

Compute the unique label values once after the (optional) rasterization, and reuse them
for the instance ids, the overlap check, and the rasterize mask.

Correctness — byte-identical output

Rendered 8 label scenarios on this branch and on main, comparing the Agg RGBA buffers
exactly (np.array_equal): all identical.

plain · contour · continuous · continuous fill+outline · categorical · groups ·
groups+na_color · outline-only

This covers both the no-table path (instance ids from the raster) and the
table-annotated path (overlap check + rasterize mask).

Performance

Removes one full-raster np.unique pass per render. The saving scales with the
displayed/rasterized label size (the label is downsampled to the canvas by default):

render main this PR
2000-px label, figsize 4×4, dpi 100 ~289 ms ~284 ms (~5 ms)
2000-px label, figsize 12×12, dpi 300 ~2336 ms ~2296 ms (~40 ms)

Small at default settings, larger for high-resolution / large-figure renders. It's a
correctness-neutral cleanup that also removes a redundant local variable.

_render_labels uniquified the (already-rasterized) label raster up to twice:
once for the instance ids / table overlap check and again for the rasterize
mask. Compute the unique label values once and reuse them at all three sites.

Output is unchanged (RGBA buffers byte-identical to main across 8 label
scenarios: plain, contour, continuous, continuous fill+outline, categorical,
groups, groups+na_color, outline-only). Removes one full-raster np.unique pass
per render; the saving scales with the displayed raster size (~5 ms at default
figsize/dpi, ~40 ms at 12x12/dpi=300 on a 2000-px label).
@timtreis timtreis merged commit 47a0374 into main Jun 6, 2026
5 of 8 checks passed
@timtreis timtreis deleted the perf/labels-unique-once branch June 6, 2026 18:49
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