⚡️ Speed up method CSSToExcelConverter.build_xlstyle by 27%
#404
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.
📄 27% (0.27x) speedup for
CSSToExcelConverter.build_xlstyleinpandas/io/formats/excel.py⏱️ Runtime :
1.39 milliseconds→1.09 milliseconds(best of23runs)📝 Explanation and details
The optimized code achieves a 27% speedup by eliminating unnecessary dictionary creation and subsequent cleanup operations. Here are the key optimizations:
1. Avoid None-value dictionary entries: Instead of creating dictionaries with None values and then removing them with
remove_none(), the optimized code only adds entries when they have meaningful values. This is most impactful inbuild_font()where the original code created 9 dictionary entries unconditionally, but the optimized version conditionally adds only non-None entries.2. Reduced
remove_none()overhead: The profiler showsremove_none()took 22.7% of total time in the original code but only 10.2% in the optimized version. This dramatic reduction comes from having fewer None values to remove in the first place.3. Skip empty border generation: In
build_border(), the optimized code checks if a border side has any meaningful properties (style_xlorcolor_xl) before adding it to the output dictionary, avoiding creation of empty border entries.4. Early returns for empty cases: Methods like
build_number_format()andbuild_alignment()now return empty dictionaries immediately when no meaningful properties are found, avoiding unnecessary dictionary construction.5. Local variable caching: Minor optimization in
build_border()caches method references (color_to_excel,_border_style) as local variables to reduce attribute lookup overhead in the loop.The test results show consistent improvements across all scenarios, with the largest gains (20-50%) appearing in cases with sparse property sets where many values would be None. This optimization is particularly valuable for Excel formatting operations that process many CSS declarations with varying completeness, as it reduces both memory allocation and cleanup overhead.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-CSSToExcelConverter.build_xlstyle-miy5upl6and push.