perf(labels): compute np.unique(label) once per render#693
Merged
Conversation
_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).
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.
What & why
Third item from the profiling write-up in #690 (after #691 shapes and #692 points).
_render_labelsuniquified the (already-rasterized) label raster up to twice per render:np.unique(label)) or, when a table annotates the element,the overlap check (
np.unique(label.values));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 buffersexactly (
np.array_equal): all identical.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.uniquepass per render. The saving scales with thedisplayed/rasterized label size (the label is downsampled to the canvas by default):
Small at default settings, larger for high-resolution / large-figure renders. It's a
correctness-neutral cleanup that also removes a redundant local variable.