⚡️ Speed up method RegistryToolWrapper._is_class_method by 6%
#1084
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.
📄 6% (0.06x) speedup for
RegistryToolWrapper._is_class_methodinbackend/python/app/agents/tools/wrapper.py⏱️ Runtime :
824 microseconds→776 microseconds(best of169runs)📝 Explanation and details
The optimization replaces a double attribute access pattern with a single
getattrcall that avoids redundant__qualname__lookups.What changed:
hasattr(func, '__qualname__') and '.' in func.__qualname__performs two separate attribute lookups on the same attributegetattr(func, '__qualname__', None)followed byqualname is not None and '.' in qualnameperforms only one attribute lookupWhy it's faster:
In Python, attribute access via
hasattr()and direct attribute access (likefunc.__qualname__) both involve dictionary lookups in the object's__dict__. The original code does this twice for the same attribute, while the optimized version caches the result in a local variable and reuses it. Local variable access is significantly faster than attribute access.Performance impact:
The line profiler shows the optimization reduces per-hit time from 344.7ns to ~293ns average (considering both optimized lines), yielding a 6% overall speedup. The test results demonstrate consistent improvements across most test cases, particularly:
test_is_class_method_many_methods_and_functions: 19.7μs → 18.7μs)len)This optimization is especially valuable if
_is_class_methodis called frequently in tool registration or validation workflows, as the cumulative savings from avoiding redundant attribute lookups will be meaningful across many invocations.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-RegistryToolWrapper._is_class_method-mja140f7and push.