⚡️ Speed up function from_json_plotly by 126%
#77
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.
📄 126% (1.26x) speedup for
from_json_plotlyinplotly/io/_json.py⏱️ Runtime :
1.43 milliseconds→632 microseconds(best of217runs)📝 Explanation and details
The optimized code achieves a 126% speedup through two key optimizations that eliminate expensive repeated function calls:
1. Cache orjson module lookup at module load time
get_module("orjson", should_load=True)from inside the function to module-level_ORJSON_MODULE2. Memoize JsonConfig.validate_orjson() calls
_VALIDATE_ORJSON_CALLEDflag to callvalidate_orjson()only once per process3. Static config import
configstatically rather than relying on global scope resolution, providing minor but consistent performance gainsThese optimizations are especially effective for small to medium JSON payloads where the function call overhead dominates (test cases show 200-1200% speedups). For large payloads, the improvements are more modest (20-50% speedups) since JSON parsing time becomes the dominant factor, but the optimizations still provide consistent gains across all use cases.
The changes maintain full behavioral compatibility - all error handling, type validation, and engine selection logic remain identical.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-from_json_plotly-mhg57vmuand push.