⚡️ Speed up function aggregate_metricrecords by 13%
#32
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.
📄 13% (0.13x) speedup for
aggregate_metricrecordsinframework/py/flwr/serverapp/strategy/strategy_utils.py⏱️ Runtime :
17.3 milliseconds→15.3 milliseconds(best of139runs)📝 Explanation and details
The optimized code achieves a 12% speedup through two key changes in the
aggregate_metricrecordsfunction:1. List Comprehension for Weight Extraction
weights: list[float] = [cast(float, next(iter(record.metric_records.values()))[weighting_metric_name]) for record in records]append()calls and reduces the number of intermediate variable assignments2. In-Place List Updates
[curr + val * weight for curr, val in zip(current_list, value)]with an in-place update loop:for i, val in enumerate(value): curr_list[i] += val * weightThe line profiler shows the most significant improvement in the list aggregation section - the original code spent 18.4% of total time creating new lists via comprehension, while the optimized version spends only 4.7% on in-place updates. The optimization is most effective for test cases with large numbers of records containing list-valued metrics, as evidenced by the performance improvements in large-scale tests with vector data.
These changes maintain the same algorithmic complexity while reducing memory allocations and function call overhead, resulting in the observed 12% performance gain.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-aggregate_metricrecords-mh9hz1xxand push.