⚡️ Speed up function run_clientappio_api_grpc by 5%
#28
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.
📄 5% (0.05x) speedup for
run_clientappio_api_grpcinframework/py/flwr/supernode/start_client_internal.py⏱️ Runtime :
12.7 milliseconds→12.0 milliseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 5% performance improvement through three main optimizations focused on reducing repeated object creation and attribute lookups:
Key Optimizations:
1. Pre-constructed gRPC Handler Objects (proto/clientappio_pb2_grpc.py)
grpc.unary_unary_rpc_method_handlerconstructions to module-level constants (_LIST_APPS_HANDLER,_REQUEST_TOKEN_HANDLER, etc.)_RPC_METHOD_HANDLERSdictionary that's reused across callsadd_ClientAppIoServicer_to_servercall2. Constant gRPC Options Optimization (common/grpc.py)
_GRPC_BASE_OPTIONStuple3. Direct Function Reference (supernode/start_client_internal.py)
add_ClientAppIoServicer_to_serverPerformance Impact:
The line profiler shows the most significant improvement in
add_ClientAppIoServicer_to_server, where handler construction time decreased from ~56ms to ~54ms. While individual savings are small, they compound when creating multiple gRPC servers or in high-frequency scenarios.Test Case Benefits:
These optimizations are particularly effective for test scenarios involving multiple server instantiations (like the port-in-use and certificate validation tests), where the same handler construction overhead would otherwise repeat many times.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-run_clientappio_api_grpc-mh5dcrphand push.