Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function normalize_string_prefix by 9% in src/black/strings.py #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Dec 16, 2024

📄 normalize_string_prefix in src/black/strings.py

✨ Performance Summary:

  • Speed Increase: 📈 9% (0.09x faster)
  • Runtime Reduction: ⏱️ From 318 microseconds down to 291 microseconds (best of 340 runs)

📝 Explanation and details

To optimize the provided Python program, we can avoid the regular expression matching and use direct string operations which are generally faster for this use case. Here’s an optimized version of the program.


Correctness verification

The new optimized code was tested for correctness. The results are listed below:

Test Status Details
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 59 Passed See below
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 1 Passed
📊 Coverage 100.0%

🌀 Generated Regression Tests Details

Click to view details
import re
from typing import Final

# imports
import pytest  # used for our unit tests
from black.strings import normalize_string_prefix

# function to test
STRING_PREFIX_CHARS = "FfBbUuRr"

STRING_PREFIX_RE: Final = re.compile(
    r"^([" + STRING_PREFIX_CHARS + r"]*)(.*)$", re.DOTALL
)
from black.strings import normalize_string_prefix


# unit tests
def test_basic_prefix_normalization():
    codeflash_output = normalize_string_prefix("F")
    codeflash_output = normalize_string_prefix("B")
    codeflash_output = normalize_string_prefix("UF")
    codeflash_output = normalize_string_prefix("uB")

def test_strings_without_prefixes():
    codeflash_output = normalize_string_prefix("hello")
    codeflash_output = normalize_string_prefix("world")
    codeflash_output = normalize_string_prefix("")

def test_mixed_case_prefixes():
    codeflash_output = normalize_string_prefix("fB")
    codeflash_output = normalize_string_prefix("Fb")
    codeflash_output = normalize_string_prefix("uF")
    codeflash_output = normalize_string_prefix("Ub")

def test_prefixes_with_r():
    codeflash_output = normalize_string_prefix("r")
    codeflash_output = normalize_string_prefix("Fr")
    codeflash_output = normalize_string_prefix("Br")
    codeflash_output = normalize_string_prefix("Ur")
    codeflash_output = normalize_string_prefix("uR")
    codeflash_output = normalize_string_prefix("rf")
    codeflash_output = normalize_string_prefix("rb")

def test_no_prefix_characters():
    codeflash_output = normalize_string_prefix("12345")
    codeflash_output = normalize_string_prefix("!@#$%")

def test_multiple_prefix_characters():
    codeflash_output = normalize_string_prefix("FU")
    codeflash_output = normalize_string_prefix("UB")
    codeflash_output = normalize_string_prefix("UFr")
    codeflash_output = normalize_string_prefix("uBr")

def test_edge_cases():
    codeflash_output = normalize_string_prefix("rF")
    codeflash_output = normalize_string_prefix("rB")
    codeflash_output = normalize_string_prefix("rU")
    codeflash_output = normalize_string_prefix("ru")
    codeflash_output = normalize_string_prefix("rFU")
    codeflash_output = normalize_string_prefix("rUB")

def test_large_scale():
    codeflash_output = normalize_string_prefix("F" + "a" * 10000)
    codeflash_output = normalize_string_prefix("a" * 10000)


def test_strings_with_newlines():
    codeflash_output = normalize_string_prefix("Fhello\nworld")
    codeflash_output = normalize_string_prefix("Bhello\nworld")

def test_strings_with_special_characters():
    codeflash_output = normalize_string_prefix("F!@#$%^&*()")
    codeflash_output = normalize_string_prefix("B12345")

def test_strings_with_escape_characters():
    codeflash_output = normalize_string_prefix("F\thello")
    codeflash_output = normalize_string_prefix("B\nworld")

def test_strings_with_mixed_content():
    codeflash_output = normalize_string_prefix("Fabc123!@#")
    codeflash_output = normalize_string_prefix("Bxyz789$%^")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import re
from typing import Final

# imports
import pytest  # used for our unit tests
from black.strings import normalize_string_prefix

# function to test
STRING_PREFIX_CHARS = "fFrRbBuU"

STRING_PREFIX_RE: Final = re.compile(
    r"^([" + STRING_PREFIX_CHARS + r"]*)(.*)$", re.DOTALL
)
from black.strings import normalize_string_prefix

# unit tests

def test_basic_prefix_normalization():
    # Single prefix normalization
    codeflash_output = normalize_string_prefix("F'hello'")
    codeflash_output = normalize_string_prefix("B'hello'")

def test_multiple_prefixes():
    # Multiple prefixes normalization and ordering
    codeflash_output = normalize_string_prefix("Fr'hello'")
    codeflash_output = normalize_string_prefix("RF'hello'")
    codeflash_output = normalize_string_prefix("uR'hello'")
    codeflash_output = normalize_string_prefix("Ur'hello'")

def test_prefix_removal():
    # Prefix removal
    codeflash_output = normalize_string_prefix("u'hello'")
    codeflash_output = normalize_string_prefix("U'hello'")

def test_mixed_case_prefixes():
    # Mixed case prefixes normalization
    codeflash_output = normalize_string_prefix("Fr'hello'")
    codeflash_output = normalize_string_prefix("fR'hello'")

def test_no_prefix():
    # No prefix
    codeflash_output = normalize_string_prefix("'hello'")
    codeflash_output = normalize_string_prefix('"hello"')


def test_empty_string():
    # Empty string
    codeflash_output = normalize_string_prefix("")

def test_strings_with_newlines():
    # Strings containing newlines
    codeflash_output = normalize_string_prefix("F'hello\nworld'")
    codeflash_output = normalize_string_prefix("r'hello\nworld'")

def test_strings_with_special_characters():
    # Strings containing special characters
    codeflash_output = normalize_string_prefix("F'hello\\nworld'")
    codeflash_output = normalize_string_prefix("r'hello\\tworld'")

def test_large_scale():
    # Large scale test cases to assess performance and scalability
    large_string = "a" * 1000000
    codeflash_output = normalize_string_prefix(f"F'{large_string}'")
    codeflash_output = normalize_string_prefix(f"u'{large_string}'")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

📣 **Feedback**

If you have any feedback or need assistance, feel free to join our Discord community:

Discord

To optimize the provided Python program, we can avoid the regular expression matching and use direct string operations which are generally faster for this use case. Here’s an optimized version of the program.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 16, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 December 16, 2024 06:07
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants