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 method CustomSplitMapMixin.has_custom_splits by 33% in src/black/trans.py #57

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

📄 CustomSplitMapMixin.has_custom_splits in src/black/trans.py

✨ Performance Summary:

  • Speed Increase: 📈 33% (0.33x faster)
  • Runtime Reduction: ⏱️ From 50.1 milliseconds down to 37.7 milliseconds (best of 5 runs)

📝 Explanation and details

Here's an optimized version of the CustomSplitMapMixin class. The optimization focuses on reducing redundant function calls and enhancing readability without changing the function signatures or functionality.


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 101009 Passed See below
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 1 Passed
📊 Coverage 100.0%

🌀 Generated Regression Tests Details

Click to view details
import pytest  # used for our unit tests
from black.trans import CustomSplitMapMixin

# unit tests

@pytest.fixture
def mixin():
    return CustomSplitMapMixin()

def test_string_in_map(mixin):
    # Adding a string to the custom split map
    mixin._CUSTOM_SPLIT_MAP[mixin._get_key("example_string")] = "custom_split"
    # Checking if the string is correctly identified as having custom splits
    codeflash_output = mixin.has_custom_splits("example_string")

def test_string_not_in_map(mixin):
    # Checking a string that is not in the custom split map
    codeflash_output = mixin.has_custom_splits("not_in_map")


def test_long_string(mixin):
    long_string = "a" * 10000
    # Adding a long string to the custom split map
    mixin._CUSTOM_SPLIT_MAP[mixin._get_key(long_string)] = "custom_split"
    # Checking if the long string is correctly identified as having custom splits
    codeflash_output = mixin.has_custom_splits(long_string)

def test_special_characters(mixin):
    special_string = "!@#$%^&*()_+-=[]{}|;':,.<>?/~`"
    # Adding a string with special characters to the custom split map
    mixin._CUSTOM_SPLIT_MAP[mixin._get_key(special_string)] = "custom_split"
    # Checking if the string with special characters is correctly identified as having custom splits
    codeflash_output = mixin.has_custom_splits(special_string)

def test_unicode_characters(mixin):
    unicode_string = "你好世界"
    # Adding a string with Unicode characters to the custom split map
    mixin._CUSTOM_SPLIT_MAP[mixin._get_key(unicode_string)] = "custom_split"
    # Checking if the string with Unicode characters is correctly identified as having custom splits
    codeflash_output = mixin.has_custom_splits(unicode_string)


def test_identical_strings_different_objects(mixin):
    string1 = "test"
    string2 = "test"
    # Adding a string to the custom split map
    mixin._CUSTOM_SPLIT_MAP[mixin._get_key(string1)] = "custom_split"
    # Checking if an identical string (different object) is correctly identified as having custom splits
    codeflash_output = mixin.has_custom_splits(string2)

def test_large_number_of_unique_strings(mixin):
    strings = [f"string_{i}" for i in range(1000)]
    # Adding a large number of unique strings to the custom split map
    for s in strings:
        mixin._CUSTOM_SPLIT_MAP[mixin._get_key(s)] = "custom_split"
    # Checking if each unique string is correctly identified as having custom splits
    for s in strings:
        codeflash_output = mixin.has_custom_splits(s)

def test_large_number_of_identical_strings(mixin):
    strings = ["identical_string"] * 100000
    # Adding an identical string to the custom split map
    mixin._CUSTOM_SPLIT_MAP[mixin._get_key("identical_string")] = "custom_split"
    # Checking if each identical string is correctly identified as having custom splits
    for s in strings:
        codeflash_output = mixin.has_custom_splits(s)

def test_large_custom_split_map(mixin):
    # Adding a large number of entries to the custom split map
    for i in range(1000):
        mixin._CUSTOM_SPLIT_MAP[mixin._get_key(f"string_{i}")] = "custom_split"
    # Checking if a specific string is correctly identified as having custom splits
    codeflash_output = mixin.has_custom_splits("string_999999")



import pytest  # used for our unit tests
from black.trans import CustomSplitMapMixin


# unit tests
class TestCustomSplitMapMixin:
    @pytest.fixture(autouse=True)
    def setup(self):
        # Setup a mock _CUSTOM_SPLIT_MAP for testing
        self.mixin = CustomSplitMapMixin()
        self.mixin._CUSTOM_SPLIT_MAP = {
            self.mixin._get_key("example"): "custom_split_data",
            self.mixin._get_key("test"): "another_custom_split",
            self.mixin._get_key("测试"): "unicode_custom_split"
        }

📣 **Feedback**

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

Discord

Here's an optimized version of the `CustomSplitMapMixin` class. The optimization focuses on reducing redundant function calls and enhancing readability without changing the function signatures or functionality.
@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 04:14
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