⚡️ Speed up function get_dummy_client by 677%
#23
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.
📄 677% (6.77x) speedup for
get_dummy_clientinframework/py/flwr/simulation/ray_transport/ray_client_proxy_test.py⏱️ Runtime :
5.06 milliseconds→651 microseconds(best of360runs)📝 Explanation and details
The optimization introduces a wrapper class caching mechanism to avoid expensive dynamic class creation overhead in the
to_client()method.Key Changes:
_wrapper_cachedictionary to store dynamically created wrapper classes by client typeto_client()to check cache first before calling_wrap_numpy_client()Why This Provides a 677% Speedup:
The original code called
_wrap_numpy_client()on every invocation, which dynamically creates a new class usingtype()- an expensive operation involving reflection and dictionary construction. The optimization caches the wrapper class type after first creation, so subsequent calls only need a fast dictionary lookup and constructor call.Performance Analysis:
_wrap_numpy_client()call (28,492 ns) now only executes once instead of 1,034 timescls not in _wrapper_cache) is much faster at 241.6 ns per hit_wrapper_cache[cls](numpy_client=self)) at 704.2 ns is significantly cheaper than full wrapper creationIdeal Test Cases:
This optimization excels when the same client class types are converted repeatedly (as shown in the test results), which is typical in federated learning scenarios where multiple instances of the same client class need conversion to the Client interface.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_dummy_client-mh16sbswand push.