⚡️ Speed up function build_invocation_context by 19%
#94
+9
−8
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.
📄 19% (0.19x) speedup for
build_invocation_contextininvokeai/app/services/shared/invocation_context.py⏱️ Runtime :
440 microseconds→369 microseconds(best of250runs)📝 Explanation and details
The optimization reduces function call overhead by eliminating repeated keyword argument creation when instantiating multiple interface objects.
Key Changes:
args = (services, data)containing the two most commonly used arguments*args) instead of explicit keyword arguments (services=services, data=data) for 6 out of 8 interface constructorsPerformance Impact:
The 19% speedup (440μs → 369μs) comes from Python's internal optimization of positional argument passing. When using
*args, Python avoids the overhead of:Test Case Performance:
The optimization shows consistent 12-27% improvements across all test scenarios, with particularly strong gains in:
This micro-optimization is especially valuable since
build_invocation_contextappears to be a factory function that creates complex object hierarchies, making the cumulative effect of reducing per-call overhead significant across the 8 interface instantiations.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-build_invocation_context-mhn7r4j1and push.