Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 28, 2025

📄 51% (0.51x) speedup for make_globally_unique_css_safe_id in src/bokeh/util/serialization.py

⏱️ Runtime : 542 milliseconds 358 milliseconds (best of 23 runs)

📝 Explanation and details

The optimization achieves a 51% speedup by eliminating two major performance bottlenecks:

1. Eliminated repeated import overhead: The original code performed from ..core.types import ID inside each function call, which was extremely expensive (18.2% of runtime in make_globally_unique_id). The optimized version moves this to a module-level import as _ID, avoiding the repeated import cost entirely.

2. Reduced unnecessary object creation in the retry loop: In make_globally_unique_css_safe_id(), the original code called make_globally_unique_id() for each retry attempt, creating an ID object each time only to discard it if the first character wasn't alphabetic. The optimized version generates str(uuid.uuid4()) directly in the loop and only creates the ID object when a valid string is found.

These changes are particularly effective for the common case where UUID strings start with a digit (requiring retries). The profiler shows the loop in make_globally_unique_css_safe_id() runs ~100x per call on average, making the per-iteration savings compound significantly. All test cases show consistent ~50% speedup, indicating the optimization benefits both single calls and batch operations equally.

The behavior remains identical - same return types, same CSS-safety guarantees, same fallback to "bk-" prefix when needed.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 22 Passed
🌀 Generated Regression Tests 2032 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 85.7%
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
import re  # used for regex checks
# function to test (copied from the prompt, with minimal necessary imports stubbed for test)
import uuid
from typing import Any

# imports
import pytest  # used for our unit tests
from bokeh.util.serialization import make_globally_unique_css_safe_id


class ID(str):
    pass
from bokeh.util.serialization import make_globally_unique_css_safe_id

# unit tests

# 1. Basic Test Cases

def test_id_is_string():
    """Test that the returned ID is a string and an instance of ID."""
    codeflash_output = make_globally_unique_css_safe_id(); id_val = codeflash_output # 282μs -> 189μs (48.8% faster)

def test_id_is_globally_unique():
    """Test that multiple calls produce unique IDs."""
    ids = {make_globally_unique_css_safe_id() for _ in range(10)} # 277μs -> 183μs (51.2% faster)

def test_id_is_css_safe():
    """Test that the returned ID starts with an alphabetic character or 'bk-'."""
    for _ in range(10):
        codeflash_output = make_globally_unique_css_safe_id(); id_val = codeflash_output # 2.66ms -> 1.77ms (50.5% faster)
        # If starts with 'bk-', next char should be alphabetic
        if id_val.startswith("bk-"):
            pass

def test_id_format_uuid():
    """Test that the ID matches UUID format if not prefixed with 'bk-'."""
    codeflash_output = make_globally_unique_css_safe_id(); id_val = codeflash_output # 272μs -> 182μs (49.0% faster)
    if not id_val.startswith("bk-"):
        # UUID format: 8-4-4-4-12 hex digits
        uuid_regex = r'^[A-Za-z][0-9a-f]{7}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}









def test_many_unique_ids():
    """Test that a large number of IDs are unique and CSS-safe."""
    ids = set()
    for _ in range(1000):
        codeflash_output = make_globally_unique_css_safe_id(); id_val = codeflash_output # 265ms -> 176ms (50.8% faster)
        ids.add(id_val)


def test_performance_large_scale():
    """Test that the function completes quickly for 1000 IDs."""
    import time
    start = time.time()
    for _ in range(1000):
        make_globally_unique_css_safe_id() # 267ms -> 176ms (51.6% faster)
    elapsed = time.time() - start
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import re
# function to test
import uuid
from typing import Any

# imports
import pytest  # used for our unit tests
from bokeh.util.serialization import make_globally_unique_css_safe_id

# unit tests

# -------------------------
# Basic Test Cases
# -------------------------

def test_id_is_string():
    """Test that the function returns a string."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 282μs -> 189μs (49.0% faster)

def test_id_is_nonempty():
    """Test that the returned ID is not empty."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 276μs -> 184μs (49.9% faster)

def test_id_is_globally_unique():
    """Test that multiple calls return unique values."""
    ids = {make_globally_unique_css_safe_id() for _ in range(100)} # 274μs -> 182μs (50.3% faster)

def test_id_starts_with_letter():
    """Test that the returned ID starts with a letter or 'bk-'."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 275μs -> 183μs (50.6% faster)

def test_id_is_css_safe():
    """Test that the ID is CSS selector safe (starts with a letter or 'bk-')."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 273μs -> 185μs (47.9% faster)

# -------------------------
# Edge Test Cases
# -------------------------

def test_id_never_empty_or_whitespace():
    """Returned ID should never be empty or only whitespace."""
    for _ in range(10):
        codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 2.66ms -> 1.77ms (49.9% faster)



def test_id_format_is_uuid_or_bk_uuid():
    """Test that the returned ID is either a UUID or 'bk-' followed by a UUID."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 280μs -> 189μs (48.1% faster)
    uuid_regex = r'^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}
    bk_uuid_regex = r'^bk-[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}

def test_id_is_ascii():
    """Test that the returned ID contains only ASCII characters."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 274μs -> 183μs (49.9% faster)
    try:
        id_.encode('ascii')
    except UnicodeEncodeError:
        pass

def test_id_length():
    """Test that the length of the ID is as expected (36 or 39 with 'bk-')."""
    codeflash_output = make_globally_unique_css_safe_id(); id_ = codeflash_output # 273μs -> 183μs (48.7% faster)

# -------------------------
# Large Scale Test Cases
# -------------------------

def test_many_ids_are_unique_and_css_safe():
    """Generate 1000 IDs and ensure all are unique and CSS-safe."""
    ids = [make_globally_unique_css_safe_id() for _ in range(1000)] # 273μs -> 182μs (49.7% faster)
    # All IDs should be CSS-safe
    for id_ in ids:
        pass

To edit these changes git checkout codeflash/optimize-make_globally_unique_css_safe_id-mhb3b336 and push.

Codeflash

The optimization achieves a **51% speedup** by eliminating two major performance bottlenecks:

**1. Eliminated repeated import overhead:** The original code performed `from ..core.types import ID` inside each function call, which was extremely expensive (18.2% of runtime in `make_globally_unique_id`). The optimized version moves this to a module-level import as `_ID`, avoiding the repeated import cost entirely.

**2. Reduced unnecessary object creation in the retry loop:** In `make_globally_unique_css_safe_id()`, the original code called `make_globally_unique_id()` for each retry attempt, creating an `ID` object each time only to discard it if the first character wasn't alphabetic. The optimized version generates `str(uuid.uuid4())` directly in the loop and only creates the `ID` object when a valid string is found.

These changes are particularly effective for the common case where UUID strings start with a digit (requiring retries). The profiler shows the loop in `make_globally_unique_css_safe_id()` runs ~100x per call on average, making the per-iteration savings compound significantly. All test cases show consistent ~50% speedup, indicating the optimization benefits both single calls and batch operations equally.

The behavior remains identical - same return types, same CSS-safety guarantees, same fallback to "bk-" prefix when needed.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 28, 2025 21:40
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant