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 _contains_asexpr by 238% in src/black/__init__.py #66

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

📄 _contains_asexpr in src/black/__init__.py

✨ Performance Summary:

  • Speed Increase: 📈 238% (2.38x faster)
  • Runtime Reduction: ⏱️ From 31.0 microseconds down to 9.17 microseconds (best of 1 runs)

📝 Explanation and details

To optimize the given code for speed, I focused on minimizing repetitive operations and enhancing efficiency where possible. Here are the changes I made.

  1. Removed redundant assertions and unnecessary checks.
  2. Optimized list iteration and dictionary operations.
  3. Simplified some logic in methods to reduce redundant computations.

Here is the revised version of the code.

These changes should generally reduce the computational complexity and improve the performance of the code.


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

🌀 Generated Regression Tests Details

Click to view details
from typing import Any, Final, Optional, Union

# imports
import pytest  # used for our unit tests
from black.__init__ import _contains_asexpr
from blib2to3 import pygram
from blib2to3.pgen2 import token
from blib2to3.pytree import Leaf, Node

# function to test
syms: Final = pygram.python_symbols

LPAR: Final = 7
RPAR: Final = 8
from black.__init__ import _contains_asexpr

# unit tests

# Basic Functionality
def test_node_with_asexpr_test():
    node = Node(type=syms.asexpr_test, children=[])
    codeflash_output = _contains_asexpr(node)

def test_node_without_asexpr_test():
    node = Node(type=syms.atom, children=[])
    codeflash_output = _contains_asexpr(node)

def test_leaf_without_asexpr_test():
    leaf = Leaf(type=token.NAME, value="test")
    codeflash_output = _contains_asexpr(leaf)

# Nested Structures
def test_nested_asexpr_within_atom():
    inner_node = Node(type=syms.asexpr_test, children=[])
    node = Node(type=syms.atom, children=[
        Leaf(type=token.LPAR, value="("),
        inner_node,
        Leaf(type=token.RPAR, value=")")
    ])
    codeflash_output = _contains_asexpr(node)

def test_deeply_nested_asexpr():
    inner_node = Node(type=syms.asexpr_test, children=[])
    middle_node = Node(type=syms.atom, children=[
        Leaf(type=token.LPAR, value="("),
        inner_node,
        Leaf(type=token.RPAR, value=")")
    ])
    outer_node = Node(type=syms.atom, children=[
        Leaf(type=token.LPAR, value="("),
        middle_node,
        Leaf(type=token.RPAR, value=")")
    ])
    codeflash_output = _contains_asexpr(outer_node)

# Complex Structures
def test_testlist_gexp_with_asexpr():
    child_node = Node(type=syms.asexpr_test, children=[])
    node = Node(type=syms.testlist_gexp, children=[child_node])
    codeflash_output = _contains_asexpr(node)

def test_testlist_gexp_without_asexpr():
    child_node = Node(type=syms.atom, children=[])
    node = Node(type=syms.testlist_gexp, children=[child_node])
    codeflash_output = _contains_asexpr(node)

# Edge Cases
def test_empty_node():
    node = Node(type=syms.atom, children=[])
    codeflash_output = _contains_asexpr(node)

def test_single_child_node():
    child_node = Node(type=syms.atom, children=[])
    node = Node(type=syms.atom, children=[child_node])
    codeflash_output = _contains_asexpr(node)

def test_non_standard_atom_structure():
    node = Node(type=syms.atom, children=[
        Leaf(type=token.LPAR, value="("),
        Leaf(type=token.NAME, value="name")
    ])
    codeflash_output = _contains_asexpr(node)

# Leaf Nodes
def test_leaf_node():
    leaf = Leaf(type=token.NAME, value="name")
    codeflash_output = _contains_asexpr(leaf)

# Large Scale Test Cases
def test_large_tree():
    deep_node = Node(type=syms.asexpr_test, children=[])
    for _ in range(1000):
        deep_node = Node(type=syms.atom, children=[
            Leaf(type=token.LPAR, value="("),
            deep_node,
            Leaf(type=token.RPAR, value=")")
        ])
    codeflash_output = _contains_asexpr(deep_node)

def test_wide_tree():
    children = [Node(type=syms.atom, children=[]) for _ in range(1000)]
    node = Node(type=syms.testlist_gexp, children=children)
    codeflash_output = _contains_asexpr(node)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import pytest  # used for our unit tests
from black.__init__ import _contains_asexpr
from blib2to3 import pygram
from blib2to3.pgen2 import token
from blib2to3.pytree import Leaf, Node

# function to test
syms = pygram.python_symbols

LPAR = 7
RPAR = 8
from black.__init__ import _contains_asexpr

# unit tests

# Basic Functionality
def test_node_with_asexpr_test():
    # Create a Node with type asexpr_test
    node = Node(type=syms.asexpr_test, children=[])
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(node)

def test_atom_with_asexpr_test():
    # Create a Node with type asexpr_test as a child of an atom Node
    child = Node(type=syms.asexpr_test, children=[])
    node = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), child, Leaf(type=RPAR, value=')')])
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(node)

def test_testlist_gexp_with_asexpr_test():
    # Create a Node with type asexpr_test as a child of a testlist_gexp Node
    child = Node(type=syms.asexpr_test, children=[])
    node = Node(type=syms.testlist_gexp, children=[child])
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(node)

# Edge Cases
def test_atom_with_incorrect_structure():
    # Create an atom Node with incorrect structure (fewer children)
    node = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), Leaf(type=RPAR, value=')')])
    # Assert that _contains_asexpr returns False
    codeflash_output = _contains_asexpr(node)

def test_testlist_gexp_without_asexpr_test():
    # Create a testlist_gexp Node without any asexpr_test children
    child = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), Leaf(type=RPAR, value=')')])
    node = Node(type=syms.testlist_gexp, children=[child])
    # Assert that _contains_asexpr returns False
    codeflash_output = _contains_asexpr(node)

def test_leaf_node():
    # Create a Leaf node
    leaf = Leaf(type=token.NAME, value='x')
    # Assert that _contains_asexpr returns False
    codeflash_output = _contains_asexpr(leaf)

# Complex Nested Structures
def test_nested_atom_nodes():
    # Create nested atom Nodes containing an asexpr_test
    inner_child = Node(type=syms.asexpr_test, children=[])
    inner_node = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), inner_child, Leaf(type=RPAR, value=')')])
    outer_node = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), inner_node, Leaf(type=RPAR, value=')')])
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(outer_node)

def test_nested_testlist_gexp_nodes():
    # Create nested testlist_gexp Nodes containing an asexpr_test
    inner_child = Node(type=syms.asexpr_test, children=[])
    inner_node = Node(type=syms.testlist_gexp, children=[inner_child])
    outer_node = Node(type=syms.testlist_gexp, children=[inner_node])
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(outer_node)

# Large Scale Test Cases
def test_large_tree_with_multiple_levels():
    # Create a deeply nested tree with multiple levels containing an asexpr_test
    deep_child = Node(type=syms.asexpr_test, children=[])
    deep_node = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), deep_child, Leaf(type=RPAR, value=')')])
    for _ in range(1000):
        deep_node = Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), deep_node, Leaf(type=RPAR, value=')')])
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(deep_node)

def test_wide_tree_with_many_siblings():
    # Create a wide tree with many siblings containing an asexpr_test
    children = [Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), Leaf(type=RPAR, value=')')]) for _ in range(1000)]
    children.append(Node(type=syms.asexpr_test, children=[]))
    node = Node(type=syms.testlist_gexp, children=children)
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(node)

# Performance and Scalability
def test_very_large_tree():
    # Create a very large tree with thousands of nodes and leaves containing an asexpr_test
    children = [Node(type=syms.atom, children=[Leaf(type=LPAR, value='('), Leaf(type=RPAR, value=')')]) for _ in range(1000)]
    children.append(Node(type=syms.asexpr_test, children=[]))
    node = Node(type=syms.testlist_gexp, children=children)
    # Assert that _contains_asexpr returns True
    codeflash_output = _contains_asexpr(node)

# Invalid Inputs
def test_node_with_invalid_type():
    # Create a Node with an invalid type
    node = Node(type=999, children=[])
    # Assert that _contains_asexpr returns False
    codeflash_output = _contains_asexpr(node)
# 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 given code for speed, I focused on minimizing repetitive operations and enhancing efficiency where possible. Here are the changes I made.

1. Removed redundant assertions and unnecessary checks.
2. Optimized list iteration and dictionary operations.
3. Simplified some logic in methods to reduce redundant computations.

Here is the revised version of the code.



These changes should generally reduce the computational complexity and improve the performance of the code.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 17, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 December 17, 2024 23:59
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