Skip to content

⚡️ Speed up function to_camel_case by 128%#5239

Merged
adhami3310 merged 2 commits intoreflex-dev:mainfrom
misrasaurabh1:codeflash/optimize-to_camel_case-ma50cmoo
May 1, 2025
Merged

⚡️ Speed up function to_camel_case by 128%#5239
adhami3310 merged 2 commits intoreflex-dev:mainfrom
misrasaurabh1:codeflash/optimize-to_camel_case-ma50cmoo

Conversation

@misrasaurabh1
Copy link
Copy Markdown
Contributor

📄 128% (1.28x) speedup for to_camel_case in reflex/utils/format.py

⏱️ Runtime : 996 microseconds 436 microseconds (best of 149 runs)

📝 Explanation and details

Here's an optimized version of your code. The time-consuming part is the re.split operation, used only for splitting on - and _, which can be replaced with a much faster approach since there are only two possible delimiters. Using str.replace is significantly faster for this case, and the generator expression for capitalization is fine, but can be made a tiny bit faster with a list comprehension. All existing comments are preserved.

Optimizations made:

  • Removed re.split, replacing with a much faster combination of str.replace and str.split.
  • Used a list comprehension for joining capitalized words (marginally faster than a generator for str.join).
  • Preserves exact same return values for all inputs and all original comments.

This should be much faster, especially for large numbers of short strings.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 38 Passed
🌀 Generated Regression Tests 44 Passed
⏪ Replay Tests 256 Passed
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests Details
- codeflash_concolic_edqok43p/tmpe5npqznc/test_concolic_coverage.py
- test_tracer_py__replay_test_0.py
- utils/test_format.py
🌀 Generated Regression Tests Details
from __future__ import annotations

import re

# imports
import pytest  # used for our unit tests
from reflex.utils.format import to_camel_case

# unit tests

def test_basic_functionality():
    # Test basic underscore conversion
    codeflash_output = to_camel_case("hello_world")
    codeflash_output = to_camel_case("my_variable_name")

def test_hyphen_handling():
    # Test hyphen conversion when treat_hyphens_as_underscores is True
    codeflash_output = to_camel_case("hello-world", True)
    codeflash_output = to_camel_case("my-variable-name", True)
    # Test hyphen ignored when treat_hyphens_as_underscores is False
    codeflash_output = to_camel_case("hello-world", False)
    codeflash_output = to_camel_case("my-variable-name", False)

def test_mixed_delimiters():
    # Test mixed hyphen and underscore conversion
    codeflash_output = to_camel_case("hello-world_test", True)
    codeflash_output = to_camel_case("my-variable_name", True)

def test_edge_cases():
    # Test empty string
    codeflash_output = to_camel_case("")
    # Test single word
    codeflash_output = to_camel_case("word")
    # Test leading and trailing delimiters
    codeflash_output = to_camel_case("_leading")
    codeflash_output = to_camel_case("trailing_")
    codeflash_output = to_camel_case("-leading", True)
    codeflash_output = to_camel_case("trailing-", True)
    # Test multiple consecutive delimiters
    codeflash_output = to_camel_case("hello__world")
    codeflash_output = to_camel_case("hello--world", True)

def test_case_sensitivity():
    # Test mixed case input
    codeflash_output = to_camel_case("HeLLo_WoRLd")
    codeflash_output = to_camel_case("MY_Variable")

def test_large_scale():
    # Test long strings
    long_string_underscores = "_".join(["word"] * 1000)
    expected_result_underscores = "word" + "".join(["Word"] * 999)
    codeflash_output = to_camel_case(long_string_underscores)

    long_string_hyphens = "-".join(["word"] * 1000)
    expected_result_hyphens = "word" + "".join(["Word"] * 999)
    codeflash_output = to_camel_case(long_string_hyphens, True)

def test_special_characters():
    # Test non-alphabetic characters
    codeflash_output = to_camel_case("hello_world123")
    codeflash_output = to_camel_case("123_hello_world")

def test_unicode_characters():
    # Test Unicode characters
    codeflash_output = to_camel_case("héllo_wörld")
    codeflash_output = to_camel_case("你好_世界")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from __future__ import annotations

import re

# imports
import pytest  # used for our unit tests
from reflex.utils.format import to_camel_case

# unit tests

def test_simple_conversion():
    """Test basic conversion from snake_case to camelCase."""
    codeflash_output = to_camel_case("hello_world")
    codeflash_output = to_camel_case("my_variable_name")

def test_hyphen_and_underscore_handling():
    """Test conversion with hyphens treated as underscores."""
    codeflash_output = to_camel_case("my-variable-name", treat_hyphens_as_underscores=True)
    codeflash_output = to_camel_case("my-variable-name", treat_hyphens_as_underscores=False)

def test_empty_string():
    """Test conversion of an empty string."""
    codeflash_output = to_camel_case("")

def test_single_word():
    """Test conversion of a single word."""
    codeflash_output = to_camel_case("word")

def test_consecutive_delimiters():
    """Test conversion with consecutive delimiters."""
    codeflash_output = to_camel_case("hello__world")
    codeflash_output = to_camel_case("hello--world", treat_hyphens_as_underscores=True)

def test_leading_and_trailing_delimiters():
    """Test conversion with leading and trailing delimiters."""
    codeflash_output = to_camel_case("_leading")
    codeflash_output = to_camel_case("trailing_")

def test_mixed_characters():
    """Test conversion with mixed characters and numbers."""
    codeflash_output = to_camel_case("hello_world123")
    codeflash_output = to_camel_case("123_hello_world")

def test_non_alphanumeric_characters():
    """Test conversion with non-alphanumeric characters."""
    codeflash_output = to_camel_case("hello_world!")
    codeflash_output = to_camel_case("@hello_world")

def test_long_strings():
    """Test conversion of long strings for performance."""
    long_string = "_".join(["word"] * 1000)
    expected_result = "word" + "".join(["Word"] * 999)
    codeflash_output = to_camel_case(long_string)

def test_mixed_case_inputs():
    """Test conversion with mixed case inputs."""
    codeflash_output = to_camel_case("HeLLo_WOrLD")
    codeflash_output = to_camel_case("HELLO_WORLD")

def test_stress_test_large_input():
    """Stress test with very large input for performance."""
    large_input = "a" * 10000
    codeflash_output = to_camel_case(large_input)

def test_consistent_output():
    """Test consistent output for same input across runs."""
    input_text = "consistent_output_test"
    expected_output = "consistentOutputTest"
    codeflash_output = to_camel_case(input_text)
    codeflash_output = to_camel_case(input_text)  # Run again to ensure consistency
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from reflex.utils.format import to_camel_case

def test_to_camel_case():
    to_camel_case('', treat_hyphens_as_underscores=False)

def test_to_camel_case_2():
    to_camel_case('', treat_hyphens_as_underscores=True)

To edit these changes git checkout codeflash/optimize-to_camel_case-ma50cmoo and push.

Codeflash

Here's an optimized version of your code. The time-consuming part is the `re.split` operation, used only for splitting on `-` and `_`, which can be replaced with a much faster approach since there are only two possible delimiters. Using `str.replace` is significantly faster for this case, and the generator expression for capitalization is fine, but can be made a tiny bit faster with a list comprehension. All existing comments are preserved.



**Optimizations made:**
- Removed `re.split`, replacing with a much faster combination of `str.replace` and `str.split`.
- Used a list comprehension for joining capitalized words (marginally faster than a generator for `str.join`).
- Preserves exact same return values for all inputs and all original comments.

This should be much faster, especially for large numbers of short strings.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented May 1, 2025

CodSpeed Performance Report

Merging #5239 will improve performances by 3.39%

Comparing misrasaurabh1:codeflash/optimize-to_camel_case-ma50cmoo (2b26c26) with main (fd8e12a)

Summary

⚡ 1 improvements
✅ 7 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
test_evaluate_page[_complicated_page] 61.8 ms 59.8 ms +3.39%

Copy link
Copy Markdown
Member

@adhami3310 adhami3310 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks good!

@adhami3310 adhami3310 merged commit 54b3bf1 into reflex-dev:main May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants