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 _calculate_lines_mappings by 6% in src/black/ranges.py #54

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 14, 2024

📄 _calculate_lines_mappings in src/black/ranges.py

✨ Performance Summary:

  • Speed Increase: 📈 6% (0.06x faster)
  • Runtime Reduction: ⏱️ From 63.1 microseconds down to 59.6 microseconds (best of 72 runs)

📝 Explanation and details

Here is the optimized version of the program.

Optimizations Made.

  1. Split Lines Early: Rather than calling splitlines inside SequenceMatcher, we do it once at the beginning and reuse the split line lists.
  2. Avoid Multiple if and else Statements: By checking for previous_block only once at the start of the loop, we avoid redundant if checks inside the loop.
  3. Direct _LinesMapping Object Appends: Append the _LinesMapping objects directly inside the loop without needing an additional if statement for the first block.
  4. Reuse previous_block: Store and reuse previous_block to avoid recalculating or reiterating over previous blocks.

Note.

The optimized code maintains the same logic but avoids unnecessary splits, rechecks, and leverages straightforward list operations to enhance performance and clarity.


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

🌀 Generated Regression Tests Details

Click to view details
import difflib
from collections.abc import Sequence
from dataclasses import dataclass

# imports
import pytest  # used for our unit tests
from black.ranges import _calculate_lines_mappings

# unit tests



def test_empty_files():
    codeflash_output = _calculate_lines_mappings("", "")
    codeflash_output = _calculate_lines_mappings("line1\nline2\n", "")
    codeflash_output = _calculate_lines_mappings("", "line1\nline2\n")

def test_single_line_files():
    codeflash_output = _calculate_lines_mappings("line1\n", "line1\n")
    codeflash_output = _calculate_lines_mappings("line1\n", "line2\n")














import difflib
from collections.abc import Sequence
from dataclasses import dataclass

# imports
import pytest  # used for our unit tests
from black.ranges import _calculate_lines_mappings

# unit tests



def test_empty_sources():
    original_source = ""
    modified_source = ""
    expected = []
    codeflash_output = _calculate_lines_mappings(original_source, modified_source)

📣 **Feedback**

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

Discord

Here is the optimized version of the program.



### Optimizations Made.

1. **Split Lines Early**: Rather than calling `splitlines` inside `SequenceMatcher`, we do it once at the beginning and reuse the split line lists.
2. **Avoid Multiple if and else Statements**: By checking for `previous_block` only once at the start of the loop, we avoid redundant `if` checks inside the loop.
3. **Direct `_LinesMapping` Object Appends**: Append the `_LinesMapping` objects directly inside the loop without needing an additional `if` statement for the first block.
4. **Reuse previous_block**: Store and reuse `previous_block` to avoid recalculating or reiterating over previous blocks. 

### Note.
The optimized code maintains the same logic but avoids unnecessary splits, rechecks, and leverages straightforward list operations to enhance performance and clarity.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 14, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 December 14, 2024 03:50
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