⚡️ Speed up method ClientApp.query by 57%
#44
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.
📄 57% (0.57x) speedup for
ClientApp.queryinframework/py/flwr/clientapp/client_app.py⏱️ Runtime :
21.7 microseconds→13.8 microseconds(best of63runs)📝 Explanation and details
The optimization achieves a 57% speedup by inlining the
_get_decoratorfunction directly into thequerymethod, eliminating function call overhead that was consuming significant time in the profiling results.Key optimizations:
Function call elimination: The original code called
_get_decorator(self, MessageType.QUERY, action, mods)on everyquery()call. The optimized version inlines this logic directly, removing the function call overhead that was taking 3,851ns per hit according to the line profiler.Reduced attribute lookups: Uses local variables like
registered_funcs = self._registered_funcsandact = actionto cache frequently accessed attributes and parameters, reducing repeated dictionary/object lookups within the inner decorator function.Optimized mods concatenation: The original code used
app._mods + (mods or [])which creates temporary list objects. The optimized version uses conditional logic to avoid unnecessary list concatenation when one of the lists is empty, reducing memory allocation overhead.Short-circuit identifier validation: Added a fast path that skips the expensive
isidentifier()method call when the action is the default "default" value, which is the most common case based on typical usage patterns.These optimizations are particularly effective for the common case of registering query functions with default actions and no mods, which represents the majority of real-world usage patterns. The line profiler shows the total time dropped from 2.84ms to 0.69ms, demonstrating the significant impact of eliminating the function call overhead in this hot path.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-ClientApp.query-mhd36yb3and push.