langflow icon indicating copy to clipboard operation
langflow copied to clipboard

⚡️ Speed up function `extract_inner_type` by 16% in `src/backend/base/langflow/type_extraction/type_extraction.py`

Open codeflash-ai[bot] opened this issue 1 year ago • 2 comments

📄 extract_inner_type() in src/backend/base/langflow/type_extraction/type_extraction.py

📈 Performance improved by 16% (0.16x faster)

⏱️ Runtime went down from 395 microseconds to 341 microseconds

Explanation and details

Here is an optimized version of the program without changing function names or signatures.

This version eliminates the need to import and use regex, leveraging basic string operations instead, making it faster and more efficient.

Correctness verification

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

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 28 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import re

import pytest  # used for our unit tests
from src.backend.base.langflow.type_extraction.type_extraction import \
    extract_inner_type

# unit tests

def test_basic_list_type_hints():
    # Standard List with Primitive Type
    codeflash_output = extract_inner_type("list[int]")
    codeflash_output = extract_inner_type("list[str]")
    codeflash_output = extract_inner_type("list[float]")
    # Outputs were verified to be equal to the original implementation

def test_case_insensitivity():
    # Different Casing for 'list'
    codeflash_output = extract_inner_type("List[int]")
    codeflash_output = extract_inner_type("LIST[int]")
    codeflash_output = extract_inner_type("lIsT[int]")
    # Outputs were verified to be equal to the original implementation

def test_non_list_type_hints():
    # Primitive Types
    codeflash_output = extract_inner_type("int")
    codeflash_output = extract_inner_type("str")
    codeflash_output = extract_inner_type("float")
    # Complex Types
    codeflash_output = extract_inner_type("dict[str, int]")
    codeflash_output = extract_inner_type("tuple[int, str]")
    # Outputs were verified to be equal to the original implementation

def test_empty_inner_type():
    # List with No Inner Type
    codeflash_output = extract_inner_type("list[]")
    # Outputs were verified to be equal to the original implementation

def test_nested_lists():
    # List of Lists
    codeflash_output = extract_inner_type("list[list[int]]")
    codeflash_output = extract_inner_type("list[list[str]]")
    # Outputs were verified to be equal to the original implementation

def test_edge_cases():
    # Empty String
    codeflash_output = extract_inner_type("")
    # Malformed List Type Hints
    codeflash_output = extract_inner_type("list[")
    codeflash_output = extract_inner_type("list]")
    codeflash_output = extract_inner_type("list[int")
    codeflash_output = extract_inner_type("listint]")
    # Outputs were verified to be equal to the original implementation

def test_large_scale_test_cases():
    # Long Type Strings
    long_inner_type = "a" * 1000
    codeflash_output = extract_inner_type(f"list[{long_inner_type}]")
    long_inner_type_with_commas = "int, " * 1000 + "str"
    codeflash_output = extract_inner_type(f"list[{long_inner_type_with_commas}]")
    # Outputs were verified to be equal to the original implementation

def test_mixed_content():
    # Type Hints with Spaces
    codeflash_output = extract_inner_type("list[ int ]")
    codeflash_output = extract_inner_type("list[ str ]")
    # Type Hints with Special Characters
    codeflash_output = extract_inner_type("list[MyType]")
    codeflash_output = extract_inner_type("list[Type@123]")
    # Outputs were verified to be equal to the original implementation

def test_unusual_but_valid_inputs():
    # Type Hints with Non-Standard Characters
    codeflash_output = extract_inner_type("list[Type_123]")
    codeflash_output = extract_inner_type("list[Type-123]")
    # Outputs were verified to be equal to the original implementation

def test_performance_and_scalability():
    # Very Large Strings
    very_large_inner_type = "a" * 1000000
    codeflash_output = extract_inner_type(f"list[{very_large_inner_type}]")
    # Outputs were verified to be equal to the original implementation

# Run the tests
if __name__ == "__main__":
    pytest.main()

🔘 (none found) − ⏪ Replay Tests

codeflash-ai[bot] avatar Aug 02 '24 12:08 codeflash-ai[bot]

Pull Request Validation Report

This comment is automatically generated by Conventional PR

Whitelist Report

Whitelist Active Result
Pull request is submitted by a bot and should be ignored
Pull request is a draft and should be ignored
Pull request is made by a whitelisted user and should be ignored
Pull request is submitted by administrators and should be ignored

Result

Pull request matches with one (or more) enabled whitelist criteria. Pull request validation is skipped.

Last Modified at 02 Aug 24 12:42 UTC

github-actions[bot] avatar Aug 02 '24 12:08 github-actions[bot]

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-3163.dmtpw4p5recq1.amplifyapp.com

This PR has been automatically closed because the original PR #3216 by EvgenyK1 was closed.

codeflash-ai[bot] avatar Aug 06 '24 14:08 codeflash-ai[bot]