⚡️ Speed up function get_default_args by 313%
#7
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.
📄 313% (3.13x) speedup for
get_default_argsinultralytics/utils/__init__.py⏱️ Runtime :
883 microseconds→214 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 313% speedup by bypassing Python's expensive
inspect.signature()API in favor of direct attribute access for common function types.Key Optimization:
func.__defaults__(positional defaults) andfunc.__kwdefaults__(keyword-only defaults) instead of usinginspect.signature().inspect.isfunction()orinspect.ismethod()to identify when the fast path can be used.inspect.signature()approach for edge cases like callable objects.Why This Is Faster:
inspect.signature()performs extensive introspection, parameter validation, and object creation overhead__defaults__,__kwdefaults__) is a simple dictionary/tuple lookupinspect.signature()takes 87.4% of execution time in the original vs only 1.1% in the optimized version when the fast path is usedPerformance Impact on Workloads:
Based on the function reference,
get_default_args()is called in the YOLO export decorator (@try_export) which processes model exports. Since model export operations can involve multiple function calls and the decorator pattern suggests this could be called frequently, the 5-20x speedup per call (as shown in annotated tests) will meaningfully reduce export processing overhead.Test Case Benefits:
The optimization shows consistent 5-25x speedups across all test scenarios, with particularly strong performance on:
The optimization maintains identical behavior while dramatically reducing function introspection overhead.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_default_args-mi8b5qw7and push.