⚡️ Speed up function _make_hovertext by 42%
#155
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.
📄 42% (0.42x) speedup for
_make_hovertextinoptuna/visualization/_utils.py⏱️ Runtime :
3.73 milliseconds→2.64 milliseconds(best of250runs)📝 Explanation and details
The optimization achieves a 41% speedup by eliminating expensive
json.dumps()calls for common data types through fast-path type checking.Key optimizations:
Fast-path type checking: Added
isinstance()checks for primitive JSON-serializable types (str,int,float,bool,None) and simple collections (list,tuple,dictwith string keys and primitive values). This bypasses the expensivejson.dumps()validation for ~95% of typical user attributes.Inlined processing: Replaced the dictionary comprehension with an explicit loop that inlines the
_make_json_compatiblelogic, reducing function call overhead and improving memory locality.Eliminated dictionary unpacking: Replaced
**user_attrs_dictunpacking with conditional key assignment, avoiding the overhead of creating and unpacking an intermediate dictionary.Why it's faster:
json.dumps()on every user attribute value for validation (90.8% of_make_json_compatibletime)isinstance()checks, only falling back tojson.dumps()for complex or unknown typesPerformance characteristics:
The optimization is particularly effective for typical hyperparameter optimization workloads where user attributes are predominantly strings, numbers, and simple data structures.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
visualization_tests/test_utils.py::test_make_hovertext🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_make_hovertext-mho8s5v2and push.