⚡️ Speed up function maybe_convert_css_to_tuples by 47%
#385
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.
📄 47% (0.47x) speedup for
maybe_convert_css_to_tuplesinpandas/io/formats/style_render.py⏱️ Runtime :
1.41 milliseconds→959 microseconds(best of57runs)📝 Explanation and details
The optimization replaces the inefficient list comprehension with a more streamlined loop that eliminates redundant string operations. Key changes:
Single split per property: Uses
str.partition(":")instead ofstr.split(":")multiple times. The original code calledx.split(":")twice - once for the key[0]and again for reconstructing the value with":".join(x.split(":")[1:]). The optimized version usespartitionwhich splits only once at the first colon and returns the separator, avoiding duplicate work.Reduced strip operations: The original code stripped
xin the list comprehension condition and then stripped the split results. The optimized version stripsxonce upfront (x_stripped) and reuses this result.Eliminated unnecessary string reconstruction: The original
":".join(x.split(":")[1:])pattern is expensive for values containing multiple colons. The optimizedpartitionmethod directly provides the key and remaining value without reconstruction.Simpler control flow: Replaced the complex list comprehension with a straightforward loop that's easier for Python to optimize.
The optimization shows 46% speedup with particularly strong gains (30-60%) on CSS strings with multiple rules, colons in values, and large-scale inputs. Based on the function references, this function is called in hot paths within pandas DataFrame styling operations (
_update_ctx,_update_ctx_header,set_table_styles), where CSS strings are processed for every styled cell. The optimization reduces CPU overhead when styling large DataFrames or applying complex CSS rules, making pandas styling operations more responsive.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
io/formats/style/test_style.py::TestStyler.test_maybe_convert_css_to_tuplesio/formats/style/test_style.py::TestStyler.test_maybe_convert_css_to_tuples_err🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-maybe_convert_css_to_tuples-mio746ghand push.