⚡️ Speed up function _aggregate_stats by 9%
#44
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.
📄 9% (0.09x) speedup for
_aggregate_statsindatacompy/fugue.py⏱️ Runtime :
187 milliseconds→172 milliseconds(best of7runs)📝 Explanation and details
The optimized code achieves a 9% speedup through several targeted micro-optimizations that reduce function call overhead and improve data structure operations:
Key Optimizations Applied:
Method Localization: The code caches
samples.__getitem__assamples_appendto avoid repeated attribute lookups in the inner loop, reducing overhead when accessing dictionary keys.Efficient DataFrame Concatenation: Instead of using a list comprehension with
pd.concat(v)inside_aggregate_stats, the optimization pre-processes concatenation logic to avoid redundant operations. It usesignore_index=Truewhen concatenating multiple DataFrames and skips concatenation entirely for single DataFrames.Index Reset Optimization in
_sample: The function now checks if the DataFrame already has a proper RangeIndex before callingreset_index(), avoiding unnecessary index operations when the DataFrame is already in the desired state.Variable Renaming for Clarity: Changed
statstostats_appendto better reflect its purpose and cachedconcat_results.appendasconcat_appendfor faster method access.Performance Impact by Test Case:
Why This Matters in Practice:
Based on the function references,
_aggregate_statsis called from the mainreport()function in datacompy's comparison workflow. Since this function processes potentially large numbers of column statistics and mismatch samples from distributed comparisons, the optimizations become more valuable as dataset size and column count increase. The improvements in large-scale test cases indicate this optimization will be most beneficial for real-world data comparison scenarios involving many columns or distributed processing contexts.The optimizations are particularly effective for workloads with many columns and multiple comparison operations, where the reduced function call overhead and efficient DataFrame operations compound to provide meaningful performance gains.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_teststest_sparktest_helper_py_teststest_fuguetest_fugue_polars_py_teststest_fuguetest_fugue_p__replay_test_0.py::test_datacompy_fugue__aggregate_statsTo edit these changes
git checkout codeflash/optimize-_aggregate_stats-mi6jgv60and push.