⚡️ Speed up method Tool.from_string by 159%#170
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization achieves a **159% speedup** by eliminating redundant computations in the error handling path of `Tool.from_string()`, which is frequently called when processing tool configurations in Bokeh plots. **Key Optimizations:** 1. **Class-level caching of tool names**: The original code repeatedly called `cls._known_aliases.keys()` and computed `.lower()` for each key on every error. The optimized version caches both the original tool names tuple (`_known_names_tuple`) and their lowercased variants (`_known_names_lower`) as class attributes, computed only once per class. 2. **Efficient case-insensitive matching**: Instead of passing `known_names` (which are mixed case) to `difflib.get_close_matches()` with `name.lower()`, the optimization passes the pre-computed `known_names_lower` list, eliminating redundant string lowering operations during fuzzy matching. 3. **Import reorganization**: Moved imports to standard locations for better performance. **Performance Impact by Test Case:** - **Large-scale scenarios show dramatic improvements**: Tests with 1000+ tools see speedups of **2900-5900%** because the caching eliminates O(n) string operations on every error - **Basic error cases**: 1-7% improvements due to reduced overhead - **Success cases**: Minimal impact (±3%) since caching only helps error paths **Real-world Impact:** Based on the function references, `Tool.from_string()` is called from `add_tools()` in plot creation and `_resolve_tools()` during tool resolution. When users provide invalid tool names (common during development/configuration), this optimization prevents performance degradation that scales with the number of registered tools. The caching is particularly valuable in applications with many custom tools or when processing tool lists programmatically.
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.
📄 159% (1.59x) speedup for
Tool.from_stringinsrc/bokeh/models/tools.py⏱️ Runtime :
7.82 milliseconds→3.01 milliseconds(best of44runs)📝 Explanation and details
The optimization achieves a 159% speedup by eliminating redundant computations in the error handling path of
Tool.from_string(), which is frequently called when processing tool configurations in Bokeh plots.Key Optimizations:
Class-level caching of tool names: The original code repeatedly called
cls._known_aliases.keys()and computed.lower()for each key on every error. The optimized version caches both the original tool names tuple (_known_names_tuple) and their lowercased variants (_known_names_lower) as class attributes, computed only once per class.Efficient case-insensitive matching: Instead of passing
known_names(which are mixed case) todifflib.get_close_matches()withname.lower(), the optimization passes the pre-computedknown_names_lowerlist, eliminating redundant string lowering operations during fuzzy matching.Import reorganization: Moved imports to standard locations for better performance.
Performance Impact by Test Case:
Real-world Impact:
Based on the function references,
Tool.from_string()is called fromadd_tools()in plot creation and_resolve_tools()during tool resolution. When users provide invalid tool names (common during development/configuration), this optimization prevents performance degradation that scales with the number of registered tools. The caching is particularly valuable in applications with many custom tools or when processing tool lists programmatically.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Tool.from_string-mhx0t8k6and push.