⚡️ Speed up method SimpleHealthServicer.Check by 165%
#39
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.
📄 165% (1.65x) speedup for
SimpleHealthServicer.Checkinframework/py/flwr/supercore/grpc_health/simple_health_servicer.py⏱️ Runtime :
1.97 milliseconds→745 microseconds(best of164runs)📝 Explanation and details
The optimization introduces object caching to eliminate repeated object creation overhead. The key change is creating a single cached
HealthCheckResponseinstance (_SERVING_RESPONSE) at module level, rather than creating a new response object on every method call.What changed:
_SERVING_RESPONSE = HealthCheckResponse(status=HealthCheckResponse.SERVING)Checkmethod to return the cached instance instead of creating new objectsWhy this is faster:
HealthCheckResponseobject for every call (548ns per hit), while the optimized version simply returns a pre-existing object (203ns per hit)Performance gains:
This optimization is most beneficial for scenarios with frequent health check requests, as demonstrated by the test cases that make hundreds or thousands of consecutive calls - exactly the use case where gRPC health services typically operate.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-SimpleHealthServicer.Check-mhcbi59gand push.