Skip to content

Comments

Fix/annotation quadratic performance#5520

Open
mosh3eb wants to merge 3 commits intoplotly:mainfrom
mosh3eb:fix/annotation-quadratic-performance
Open

Fix/annotation quadratic performance#5520
mosh3eb wants to merge 3 commits intoplotly:mainfrom
mosh3eb:fix/annotation-quadratic-performance

Conversation

@mosh3eb
Copy link

@mosh3eb mosh3eb commented Feb 20, 2026

Description

Optimization: Annotation and shape append performance (O(N) -> O(1)) and Base64 JSON support.

This PR significantly improves the performance of adding annotations and shapes to a figure, particularly when dealing with large numbers of elements. It also introduces a requested feature for Base64 encoding in JSON serialization.

Technical Summary:

  • Performance Optimization: Refactored the list-handling logic in BaseFigure to avoid recursive, full-list validations when appending or extending annotations and layout shapes. This shifts the complexity from $O(N^2)$ for building a figure with $N$ elements to $O(N)$ (effectively $O(1)$ per operation). Fixes Adding N annotations takes O(N^2) time #5316 and Performance issue - add_vlines #4965.
  • New JSON Feature: Added a base64 encoding option to plotly.io.to_json to support embedding binary data more efficiently in JSON outputs. Fixes plotly.io.to_json method does not convert large numbers correctly #5124.
  • Code Integrity: Decoupled internal validation from public list mutations to ensure performance gains do not sacrifice data validation quality.
  • Verification: Included a comprehensive test suite in test_annotated_shapes.py that verifies both the correctness of the operations and the handling of edge cases for the new performance path.

Code PR

  • I have read through the contributing notes and understand the structure of the package.
  • I have added tests or modified existing tests.
  • For a new feature, I have added documentation examples.
  • I have added a CHANGELOG entry if changing anything substantial.
  • For a new feature or a change in behavior, I have updated the relevant docstrings in the code.

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.

Adding N annotations takes O(N^2) time plotly.io.to_json method does not convert large numbers correctly

1 participant