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 BaseInputMixin.validate_field_type by 26% in src/backend/base/langflow/inputs/input_mixin.py #102

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

📄 BaseInputMixin.validate_field_type in src/backend/base/langflow/inputs/input_mixin.py

✨ Performance Summary:

  • Speed Increase: 📈 26% (0.26x faster)
  • Runtime Reduction: ⏱️ From 8.97 milliseconds down to 7.14 milliseconds (best of 28 runs)

📝 Explanation and details

Sure, here is the optimized version of the given program.

Changes and Optimizations.


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

🌀 Generated Regression Tests Details

Click to view details
from enum import Enum

# imports
import pytest  # used for our unit tests
from langflow.inputs.input_mixin import BaseInputMixin
from pydantic import BaseModel, field_validator


# unit tests
def test_valid_field_types():
    # Test valid field types
    codeflash_output = BaseInputMixin.validate_field_type("str")
    codeflash_output = BaseInputMixin.validate_field_type("int")
    codeflash_output = BaseInputMixin.validate_field_type("float")
    codeflash_output = BaseInputMixin.validate_field_type("bool")
    codeflash_output = BaseInputMixin.validate_field_type("dict")
    codeflash_output = BaseInputMixin.validate_field_type("NestedDict")
    codeflash_output = BaseInputMixin.validate_field_type("file")
    codeflash_output = BaseInputMixin.validate_field_type("prompt")
    codeflash_output = BaseInputMixin.validate_field_type("code")
    codeflash_output = BaseInputMixin.validate_field_type("other")
    codeflash_output = BaseInputMixin.validate_field_type("table")
    codeflash_output = BaseInputMixin.validate_field_type("link")
    codeflash_output = BaseInputMixin.validate_field_type("slider")

def test_case_sensitivity():
    # Test case sensitivity
    codeflash_output = BaseInputMixin.validate_field_type("Str")
    codeflash_output = BaseInputMixin.validate_field_type("INT")
    codeflash_output = BaseInputMixin.validate_field_type("Float")

def test_invalid_field_types():
    # Test invalid field types
    codeflash_output = BaseInputMixin.validate_field_type("unknown")
    codeflash_output = BaseInputMixin.validate_field_type("random")
    codeflash_output = BaseInputMixin.validate_field_type("123")
    codeflash_output = BaseInputMixin.validate_field_type("")

def test_numeric_inputs():
    # Test numeric inputs
    codeflash_output = BaseInputMixin.validate_field_type(123)
    codeflash_output = BaseInputMixin.validate_field_type(0)
    codeflash_output = BaseInputMixin.validate_field_type(-1)

def test_special_characters():
    # Test inputs with special characters
    codeflash_output = BaseInputMixin.validate_field_type("str@")
    codeflash_output = BaseInputMixin.validate_field_type("int!")
    codeflash_output = BaseInputMixin.validate_field_type("float#")

def test_mixed_valid_invalid_inputs():
    # Test mixed valid and invalid inputs
    codeflash_output = BaseInputMixin.validate_field_type("str123")
    codeflash_output = BaseInputMixin.validate_field_type("int-str")
    codeflash_output = BaseInputMixin.validate_field_type("float!")

def test_large_scale_valid_inputs():
    # Test large scale valid inputs
    for _ in range(1000):
        codeflash_output = BaseInputMixin.validate_field_type("str")

def test_large_scale_invalid_inputs():
    # Test large scale invalid inputs
    for _ in range(1000):
        codeflash_output = BaseInputMixin.validate_field_type("invalid")

def test_large_scale_mixed_inputs():
    # Test large scale mixed inputs
    for i in range(1000):
        if i % 2 == 0:
            codeflash_output = BaseInputMixin.validate_field_type("str")
        else:
            codeflash_output = BaseInputMixin.validate_field_type("invalid")

def test_edge_cases():
    # Test edge cases
    codeflash_output = BaseInputMixin.validate_field_type(None)
    codeflash_output = BaseInputMixin.validate_field_type(True)
    codeflash_output = BaseInputMixin.validate_field_type(False)
    codeflash_output = BaseInputMixin.validate_field_type([])
    codeflash_output = BaseInputMixin.validate_field_type({})

def test_non_string_iterable_inputs():
    # Test non-string iterable inputs
    codeflash_output = BaseInputMixin.validate_field_type(["str"])
    codeflash_output = BaseInputMixin.validate_field_type(("int",))
    codeflash_output = BaseInputMixin.validate_field_type({"float"})

def test_whitespace_inputs():
    # Test inputs with leading or trailing whitespace
    codeflash_output = BaseInputMixin.validate_field_type(" str")
    codeflash_output = BaseInputMixin.validate_field_type("int ")
    codeflash_output = BaseInputMixin.validate_field_type(" float ")

def test_null_character_inputs():
    # Test inputs with embedded null characters
    codeflash_output = BaseInputMixin.validate_field_type("str\0")
    codeflash_output = BaseInputMixin.validate_field_type("int\0")
    codeflash_output = BaseInputMixin.validate_field_type("\0float")

def test_extremely_long_strings():
    # Test extremely long string inputs
    codeflash_output = BaseInputMixin.validate_field_type("str" * 1000)
    codeflash_output = BaseInputMixin.validate_field_type("int" * 1000)
    codeflash_output = BaseInputMixin.validate_field_type("float" * 1000)

def test_special_unicode_characters():
    # Test inputs with special Unicode characters
    codeflash_output = BaseInputMixin.validate_field_type("str😊")
    codeflash_output = BaseInputMixin.validate_field_type("int🚀")
    codeflash_output = BaseInputMixin.validate_field_type("float🌟")

def test_escape_sequences():
    # Test inputs with escape sequences
    codeflash_output = BaseInputMixin.validate_field_type("str\n")
    codeflash_output = BaseInputMixin.validate_field_type("int\t")
    codeflash_output = BaseInputMixin.validate_field_type("float\r")

def test_mixed_type_inputs():
    # Test inputs that are a mix of different types
    codeflash_output = BaseInputMixin.validate_field_type(["str", 123])
    codeflash_output = BaseInputMixin.validate_field_type({"int": "float"})
    codeflash_output = BaseInputMixin.validate_field_type(("bool", None))


def test_embedded_json():
    # Test inputs that are JSON strings
    codeflash_output = BaseInputMixin.validate_field_type('{"type": "str"}')
    codeflash_output = BaseInputMixin.validate_field_type('{"type": "int"}')
    codeflash_output = BaseInputMixin.validate_field_type('{"type": "float"}')

def test_embedded_xml():
    # Test inputs that are XML strings
    codeflash_output = BaseInputMixin.validate_field_type('<type>str</type>')
    codeflash_output = BaseInputMixin.validate_field_type('<type>int</type>')
    codeflash_output = BaseInputMixin.validate_field_type('<type>float</type>')
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from enum import Enum

# imports
import pytest  # used for our unit tests
from langflow.inputs.input_mixin import BaseInputMixin
from pydantic import BaseModel, field_validator

# unit tests

# Basic Test Cases
def test_valid_field_types():
    codeflash_output = BaseInputMixin.validate_field_type("str")
    codeflash_output = BaseInputMixin.validate_field_type("int")
    codeflash_output = BaseInputMixin.validate_field_type("float")
    codeflash_output = BaseInputMixin.validate_field_type("bool")
    codeflash_output = BaseInputMixin.validate_field_type("dict")
    codeflash_output = BaseInputMixin.validate_field_type("NestedDict")
    codeflash_output = BaseInputMixin.validate_field_type("file")
    codeflash_output = BaseInputMixin.validate_field_type("prompt")
    codeflash_output = BaseInputMixin.validate_field_type("code")
    codeflash_output = BaseInputMixin.validate_field_type("table")
    codeflash_output = BaseInputMixin.validate_field_type("link")
    codeflash_output = BaseInputMixin.validate_field_type("slider")

def test_invalid_field_types():
    codeflash_output = BaseInputMixin.validate_field_type("unknown")
    codeflash_output = BaseInputMixin.validate_field_type("123")
    codeflash_output = BaseInputMixin.validate_field_type("boolean")

def test_case_sensitivity():
    codeflash_output = BaseInputMixin.validate_field_type("Str")
    codeflash_output = BaseInputMixin.validate_field_type("INT")
    codeflash_output = BaseInputMixin.validate_field_type("Float")

# Edge Test Cases
def test_empty_string():
    codeflash_output = BaseInputMixin.validate_field_type("")

def test_whitespace_strings():
    codeflash_output = BaseInputMixin.validate_field_type(" ")
    codeflash_output = BaseInputMixin.validate_field_type("   ")


def test_large_number_of_valid_inputs():
    valid_inputs = ["str", "int", "float", "bool", "dict", "NestedDict", "file", "prompt", "code", "table", "link", "slider"]
    for input_value in valid_inputs * 100:
        codeflash_output = BaseInputMixin.validate_field_type(input_value)

def test_large_number_of_invalid_inputs():
    invalid_inputs = ["unknown", "123", "boolean", "Str", "INT", "Float"]
    for input_value in invalid_inputs * 100:
        codeflash_output = BaseInputMixin.validate_field_type(input_value)


def test_stress_test_with_large_input():
    codeflash_output = BaseInputMixin.validate_field_type("a" * 10000)

# Boundary Testing
def test_boundary_length_strings():
    codeflash_output = BaseInputMixin.validate_field_type("a" * 255)
    codeflash_output = BaseInputMixin.validate_field_type("a" * 256)
    codeflash_output = BaseInputMixin.validate_field_type("a" * 257)
# 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

Sure, here is the optimized version of the given program.

### Changes and Optimizations.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 12, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 December 12, 2024 20:13
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