⚡️ Speed up function str_to_color by 6%
#781
Open
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.
📄 6% (0.06x) speedup for
str_to_colorininference/core/workflows/core_steps/visualizations/common/utils.py⏱️ Runtime :
4.52 milliseconds→4.26 milliseconds(best of34runs)📝 Explanation and details
The optimized code achieves a 6% speedup through three key optimizations:
1. Pre-computed Color Name Lookup
The biggest performance gain comes from eliminating repeated
hasattr()andgetattr()calls for color names. The original code performed expensive attribute reflection for each color name lookup:hasattr(sv.Color, color.upper())- checks if attribute existsgetattr(sv.Color, color.upper())- retrieves the attributecolor.upper()- converts string to uppercaseThe optimized version pre-computes all valid color attributes once at module load time into
_COLOR_NAME_MAP, then uses a simple dictionary lookup withcolor.lower(). This provides 790-846% faster performance for named colors according to the test results.2. Optimized RGB/BGR Parsing
For RGB and BGR parsing, the optimization replaces
map(int, color[4:-1].split(","))with direct integer conversion of split values when exactly 3 values are present. This avoids creating a temporarymapobject and provides 2-3% faster performance for RGB/BGR colors.3. Single String Operation
The color name lookup now calls
.lower()only once and stores the result, rather than calling.upper()twice in the original code'shasattr/getattrpattern.Impact on Hot Path Usage
Based on the function references,
str_to_coloris called in QR code generation for bothfill_colorandback_colorparameters. While QR generation may not be in the critical rendering path, the optimization particularly benefits workloads with:The optimization maintains full backward compatibility while providing consistent performance improvements across all color format types.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
workflows/unit_tests/core_steps/visualizations/test_str_to_color.py::test_str_to_color_with_bgr_colorworkflows/unit_tests/core_steps/visualizations/test_str_to_color.py::test_str_to_color_with_color_nameworkflows/unit_tests/core_steps/visualizations/test_str_to_color.py::test_str_to_color_with_hex_colorworkflows/unit_tests/core_steps/visualizations/test_str_to_color.py::test_str_to_color_with_invalid_colorworkflows/unit_tests/core_steps/visualizations/test_str_to_color.py::test_str_to_color_with_rgb_color🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
To edit these changes
git checkout codeflash/optimize-str_to_color-miqjntrkand push.