⚡️ Speed up function aggregate_metricrecords by 13%#32
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function aggregate_metricrecords by 13%#32codeflash-ai[bot] wants to merge 1 commit intomainfrom
aggregate_metricrecords by 13%#32codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves a 12% speedup through two key changes in the `aggregate_metricrecords` function: **1. List Comprehension for Weight Extraction** - Replaced the explicit loop with a list comprehension to extract weights: `weights: list[float] = [cast(float, next(iter(record.metric_records.values()))[weighting_metric_name]) for record in records]` - This eliminates the overhead of multiple `append()` calls and reduces the number of intermediate variable assignments **2. In-Place List Updates** - For list-valued metrics, replaced the expensive list comprehension `[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 * weight` - This avoids creating new list objects for each aggregation step, which is particularly beneficial when dealing with large lists or many records The 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.