Fix/annotation quadratic performance by mosh3eb · Pull Request #5520 · plotly/plotly.py
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
BaseFigureto 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
base64encoding option toplotly.io.to_jsonto 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.pythat 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.