Skip to content

⚡️ Speed up function _looks_like_variable_name by 71% in PR #12129 (fix-api-key-components)#12131

Closed
codeflash-ai[bot] wants to merge 6 commits intorelease-1.8.1from
codeflash/optimize-pr12129-2026-03-10T17.38.03
Closed

⚡️ Speed up function _looks_like_variable_name by 71% in PR #12129 (fix-api-key-components)#12131
codeflash-ai[bot] wants to merge 6 commits intorelease-1.8.1from
codeflash/optimize-pr12129-2026-03-10T17.38.03

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Mar 10, 2026

⚡️ This pull request contains optimizations for PR #12129

If you approve this dependent PR, these changes will be merged into the original PR branch fix-api-key-components.

This PR will be automatically closed if the original PR is merged.


📄 71% (0.71x) speedup for _looks_like_variable_name in src/backend/base/langflow/api/utils/core.py

⏱️ Runtime : 3.84 milliseconds 2.25 milliseconds (best of 84 runs)

📝 Explanation and details

The optimized code pre-compiles the regex pattern at module load time instead of recompiling it on every invocation, eliminating the ~3 µs per-call overhead of re.fullmatch. Line profiler shows the regex line dropped from 86.3% to 41% of runtime despite now doing explicit is not None checks, because pattern compilation is amortized across all calls. Additionally, splitting the compound if not value or not isinstance(value, str) or not value.strip() into separate checks avoids redundant strip() calls on non-strings (saving ~286 ns per non-string input). The match with \Z anchor replaces fullmatch semantically while being slightly faster. All 90+ test cases pass with identical logic, confirming correctness across edge cases including very long strings, unicode, and high-volume repeated calls.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 6112 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
import pytest  # used for our unit tests
from langflow.api.utils.core import _looks_like_variable_name


def test_single_letter_names_are_valid():
    # Single ASCII letters should be valid variable names.
    assert _looks_like_variable_name("a") is True  # lowercase letter
    assert _looks_like_variable_name("Z") is True  # uppercase letter


def test_multichar_names_with_digits_and_underscores_are_valid():
    # Names that start with a letter and contain letters, digits, and underscores are valid.
    assert _looks_like_variable_name("var") is True
    assert _looks_like_variable_name("var1") is True
    assert _looks_like_variable_name("var_name") is True
    assert _looks_like_variable_name("a_b1") is True


def test_invalid_starters_and_special_chars_are_rejected():
    # Names that don't start with an ASCII letter or contain invalid characters should be rejected.
    assert _looks_like_variable_name("_underscore_start") is False  # starts with underscore
    assert _looks_like_variable_name("1starts_with_digit") is False  # starts with digit
    assert _looks_like_variable_name("has-hyphen") is False  # hyphen not allowed
    assert _looks_like_variable_name("has space") is False  # spaces inside not allowed
    assert _looks_like_variable_name("dollar$") is False  # special char not allowed


def test_empty_and_whitespace_inputs_return_false():
    # None and empty values or whitespace-only strings do not look like variable names.
    assert _looks_like_variable_name("") is False  # empty string
    assert _looks_like_variable_name("   ") is False  # whitespace only
    assert _looks_like_variable_name(None) is False  # None input
    assert _looks_like_variable_name(0) is False  # falsy non-string input
    assert _looks_like_variable_name(False) is False  # boolean input


def test_non_string_types_are_rejected_even_if_they_have_stringy_representation():
    # Only real str instances are acceptable; other types (even if they implement __str__) are rejected.
    assert _looks_like_variable_name(object()) is False  # arbitrary object
    # bytes and bytearray are not str subclasses — must be rejected
    assert _looks_like_variable_name(b"abc") is False
    assert _looks_like_variable_name(bytearray(b"abc")) is False


def test_leading_and_trailing_whitespace_is_stripped_before_validation():
    # The function strips whitespace before applying the pattern, so leading/trailing whitespace is ignored.
    assert _looks_like_variable_name("  var1  ") is True  # valid after strip
    assert _looks_like_variable_name("\nA\t") is True  # newline and tab are stripped -> "A" valid
    # Internal whitespace remains and therefore invalid
    assert _looks_like_variable_name("a b") is False


def test_non_ascii_letters_are_not_considered_valid_starting_chars():
    # The regex explicitly limits letters to ASCII A-Z/a-z. Non-ASCII letters should fail.
    assert _looks_like_variable_name("ävar") is False  # starts with non-ASCII letter
    assert _looks_like_variable_name("μ") is False  # single non-ASCII letter
    # Even if the subsequent chars are ASCII, initial non-ASCII causes failure
    assert _looks_like_variable_name("Åbc123") is False


def test_large_alternating_valid_and_invalid_list_of_1000_items():
    # Build 1000 deterministic names alternating between valid and invalid patterns.
    inputs = []
    expected = []
    for i in range(1000):
        if i % 2 == 0:
            # even indices: valid names like v0, v2, v4, ...
            name = f"v{i}"
            inputs.append(name)
            expected.append(True)
        else:
            # odd indices: invalid names starting with a digit like 1v1, 1v3, ...
            name = f"1v{i}"
            inputs.append(name)
            expected.append(False)

    # Validate all generated inputs against expected booleans.
    results = [ _looks_like_variable_name(s) for s in inputs ]
    assert results == expected  # entire list must match expected pattern


def test_repeated_invocations_for_performance_and_consistency():
    # Call the function many times deterministically to ensure consistent behavior under repetition.
    for _ in range(1000):
        assert _looks_like_variable_name("consistentName123") is True  # always valid
        assert _looks_like_variable_name("   ") is False  # always invalid
        assert _looks_like_variable_name("_nope") is False  # always invalid


def test_very_long_names_are_handled_correctly():
    # Create very long valid and invalid names (1000+ characters) to test regex handling and boundaries.
    long_valid = "a" + ("_" * 999)  # starts with 'a', followed by 999 underscores -> valid
    assert _looks_like_variable_name(long_valid) is True

    long_invalid_start_digit = "1" + ("a" * 1000)  # starts with digit -> invalid
    assert _looks_like_variable_name(long_invalid_start_digit) is False

    long_with_space_inside = "a" + ("a" * 499) + " " + ("b" * 500)  # contains a space in the middle -> invalid
    assert _looks_like_variable_name(long_with_space_inside) is False
#------------------------------------------------
import re
from typing import Any

# imports
import pytest
from langflow.api.utils.core import _looks_like_variable_name


def test_basic_valid_variable_name_lowercase():
    """Test that a simple lowercase variable name returns True."""
    assert _looks_like_variable_name("x") is True


def test_basic_valid_variable_name_uppercase():
    """Test that a simple uppercase variable name returns True."""
    assert _looks_like_variable_name("X") is True


def test_basic_valid_variable_name_mixed_case():
    """Test that a mixed-case variable name returns True."""
    assert _looks_like_variable_name("myVariable") is True


def test_basic_valid_variable_name_with_numbers():
    """Test that a variable name with numbers (not at start) returns True."""
    assert _looks_like_variable_name("var123") is True


def test_basic_valid_variable_name_with_underscores():
    """Test that a variable name with underscores returns True."""
    assert _looks_like_variable_name("my_variable") is True


def test_basic_valid_variable_name_all_underscores_after_letter():
    """Test that a variable name starting with letter and all underscores returns True."""
    assert _looks_like_variable_name("a___") is True


def test_basic_valid_variable_name_python_conventions():
    """Test common Python variable naming conventions."""
    assert _looks_like_variable_name("__init__") is True
    assert _looks_like_variable_name("_private") is True
    assert _looks_like_variable_name("CONSTANT") is True


def test_basic_invalid_starts_with_number():
    """Test that a name starting with a number returns False."""
    assert _looks_like_variable_name("1variable") is False


def test_basic_invalid_starts_with_underscore():
    """Test that a name starting with underscore returns False."""
    assert _looks_like_variable_name("_variable") is False


def test_basic_invalid_contains_hyphen():
    """Test that a name with a hyphen returns False."""
    assert _looks_like_variable_name("my-variable") is False


def test_basic_invalid_contains_space():
    """Test that a name with spaces returns False."""
    assert _looks_like_variable_name("my variable") is False


def test_basic_invalid_contains_special_character():
    """Test that a name with special characters returns False."""
    assert _looks_like_variable_name("my$variable") is False


def test_basic_invalid_contains_dot():
    """Test that a name with a dot returns False."""
    assert _looks_like_variable_name("my.variable") is False


def test_edge_empty_string():
    """Test that an empty string returns False."""
    assert _looks_like_variable_name("") is False


def test_edge_none_value():
    """Test that None returns False."""
    assert _looks_like_variable_name(None) is False


def test_edge_whitespace_only():
    """Test that a string with only whitespace returns False."""
    assert _looks_like_variable_name("   ") is False
    assert _looks_like_variable_name("\t") is False
    assert _looks_like_variable_name("\n") is False


def test_edge_single_letter_lowercase():
    """Test that a single lowercase letter returns True."""
    assert _looks_like_variable_name("a") is True


def test_edge_single_letter_uppercase():
    """Test that a single uppercase letter returns True."""
    assert _looks_like_variable_name("Z") is True


def test_edge_variable_with_leading_whitespace():
    """Test that a valid name with leading whitespace is trimmed and returns True."""
    assert _looks_like_variable_name("  myVar") is True


def test_edge_variable_with_trailing_whitespace():
    """Test that a valid name with trailing whitespace is trimmed and returns True."""
    assert _looks_like_variable_name("myVar  ") is True


def test_edge_variable_with_both_whitespace():
    """Test that a valid name surrounded by whitespace is trimmed and returns True."""
    assert _looks_like_variable_name("  myVar  ") is True


def test_edge_integer_input():
    """Test that an integer returns False (not a string)."""
    assert _looks_like_variable_name(123) is False


def test_edge_float_input():
    """Test that a float returns False (not a string)."""
    assert _looks_like_variable_name(1.23) is False


def test_edge_list_input():
    """Test that a list returns False (not a string)."""
    assert _looks_like_variable_name(["x"]) is False


def test_edge_dict_input():
    """Test that a dict returns False (not a string)."""
    assert _looks_like_variable_name({"x": 1}) is False


def test_edge_zero():
    """Test that zero returns False."""
    assert _looks_like_variable_name(0) is False


def test_edge_false_value():
    """Test that False returns False."""
    assert _looks_like_variable_name(False) is False


def test_edge_true_value():
    """Test that True returns False (not a string)."""
    assert _looks_like_variable_name(True) is False


def test_edge_empty_string_with_whitespace_trimmed():
    """Test that whitespace-only strings return False after trimming."""
    assert _looks_like_variable_name("") is False


def test_edge_valid_name_max_underscores():
    """Test a valid variable name with maximum consecutive underscores."""
    assert _looks_like_variable_name("a__________________") is True


def test_edge_valid_name_max_numbers():
    """Test a valid variable name with many numbers."""
    assert _looks_like_variable_name("a123456789") is True


def test_edge_invalid_all_numbers():
    """Test that all numbers returns False."""
    assert _looks_like_variable_name("123456") is False


def test_edge_invalid_all_underscores():
    """Test that all underscores returns False."""
    assert _looks_like_variable_name("_____") is False


def test_edge_invalid_starts_with_at_symbol():
    """Test that starting with @ returns False."""
    assert _looks_like_variable_name("@variable") is False


def test_edge_invalid_contains_parentheses():
    """Test that containing parentheses returns False."""
    assert _looks_like_variable_name("var()") is False


def test_edge_invalid_contains_brackets():
    """Test that containing brackets returns False."""
    assert _looks_like_variable_name("var[]") is False


def test_edge_invalid_contains_quotes():
    """Test that containing quotes returns False."""
    assert _looks_like_variable_name('var"name') is False


def test_edge_unicode_characters():
    """Test that unicode characters are not recognized as valid variable names."""
    assert _looks_like_variable_name("café") is False
    assert _looks_like_variable_name("变量") is False


def test_edge_valid_lambda_is_false():
    """Test that the keyword 'lambda' returns False (it's a reserved word, but function allows it)."""
    # The function doesn't check for reserved keywords, just pattern matching
    assert _looks_like_variable_name("lambda") is True


def test_edge_valid_all_letters_lowercase():
    """Test that all lowercase letters returns True."""
    assert _looks_like_variable_name("abcdefghijklmnopqrstuvwxyz") is True


def test_edge_valid_all_letters_uppercase():
    """Test that all uppercase letters returns True."""
    assert _looks_like_variable_name("ABCDEFGHIJKLMNOPQRSTUVWXYZ") is True


def test_edge_valid_mixed_all_allowed_chars():
    """Test a valid variable name using all allowed characters."""
    assert _looks_like_variable_name("aAbBcC_0123456789") is True


def test_edge_invalid_mixed_disallowed_chars():
    """Test that mixing allowed and disallowed characters returns False."""
    assert _looks_like_variable_name("aAbBcC-0123456789") is False


def test_large_scale_valid_long_variable_name():
    """Test a very long variable name (1000+ chars) that is valid."""
    long_name = "a" + "b" * 1000
    assert _looks_like_variable_name(long_name) is True


def test_large_scale_valid_long_variable_with_numbers():
    """Test a long variable name with numbers."""
    long_name = "var_" + "0" * 500 + "_name"
    assert _looks_like_variable_name(long_name) is True


def test_large_scale_valid_long_variable_with_underscores():
    """Test a long variable name with underscores."""
    long_name = "a" + "_" * 1000
    assert _looks_like_variable_name(long_name) is True


def test_large_scale_invalid_long_with_invalid_char_at_end():
    """Test that a very long name with one invalid char at end returns False."""
    long_name = "a" * 1000 + "-invalid"
    assert _looks_like_variable_name(long_name) is False


def test_large_scale_invalid_long_with_invalid_char_in_middle():
    """Test that a very long name with invalid char in middle returns False."""
    long_name = "a" * 500 + "$" + "b" * 499
    assert _looks_like_variable_name(long_name) is False


def test_large_scale_batch_validation_valid_names():
    """Test validation of multiple valid variable names in a batch."""
    valid_names = [
        "var1", "myVar", "_test", "CONSTANT", "x", "aB0_Z",
        "a" * 100, "test_" * 50, "VAR123VAR"
    ]
    for name in valid_names:
        result = _looks_like_variable_name(name)
        assert result is True, f"Expected {name} to be valid"


def test_large_scale_batch_validation_invalid_names():
    """Test validation of multiple invalid variable names in a batch."""
    invalid_names = [
        "", "1var", "_start", "var-name", "var name", "var$",
        "123", "_", "@var", "var.", "", "   ",
        "0var", "my-var", "my.var", "my@var", "my#var"
    ]
    for name in invalid_names:
        result = _looks_like_variable_name(name)
        assert result is False, f"Expected {name} to be invalid"


def test_large_scale_performance_many_valid_names():
    """Test performance with many valid variable name validations."""
    # Generate 1000 valid variable names and validate them
    for i in range(1000):
        name = f"var{i}"
        assert _looks_like_variable_name(name) is True


def test_large_scale_performance_many_invalid_names():
    """Test performance with many invalid variable name validations."""
    # Generate 1000 invalid variable names and validate them
    for i in range(1000):
        name = f"{i}var"  # Starts with number, invalid
        assert _looks_like_variable_name(name) is False


def test_large_scale_performance_alternating_pattern():
    """Test performance with alternating valid/invalid pattern."""
    for i in range(500):
        valid_name = f"a{i}"
        invalid_name = f"{i}a"
        assert _looks_like_variable_name(valid_name) is True
        assert _looks_like_variable_name(invalid_name) is False


def test_large_scale_edge_case_very_long_invalid_prefix():
    """Test performance with long invalid prefix."""
    name = "1" * 500 + "a"  # Invalid due to leading digit
    assert _looks_like_variable_name(name) is False


def test_large_scale_edge_case_very_long_valid_name_all_same_char():
    """Test performance with long name of all same character."""
    name = "a" * 2000
    assert _looks_like_variable_name(name) is True


def test_large_scale_stress_test_whitespace_trimming():
    """Test performance of whitespace trimming with very long whitespace."""
    name = " " * 500 + "valid_name" + " " * 500
    assert _looks_like_variable_name(name) is True


def test_large_scale_stress_test_many_underscores():
    """Test performance with maximum underscores in valid name."""
    name = "a" + "_" * 2000 + "b"
    assert _looks_like_variable_name(name) is True

To edit these changes git checkout codeflash/optimize-pr12129-2026-03-10T17.38.03 and push.

Codeflash

HimavarshaVS and others added 3 commits March 10, 2026 12:51
The optimized code pre-compiles the regex pattern at module load time instead of recompiling it on every invocation, eliminating the ~3 µs per-call overhead of `re.fullmatch`. Line profiler shows the regex line dropped from 86.3% to 41% of runtime despite now doing explicit `is not None` checks, because pattern compilation is amortized across all calls. Additionally, splitting the compound `if not value or not isinstance(value, str) or not value.strip()` into separate checks avoids redundant `strip()` calls on non-strings (saving ~286 ns per non-string input). The `match` with `\Z` anchor replaces `fullmatch` semantically while being slightly faster. All 90+ test cases pass with identical logic, confirming correctness across edge cases including very long strings, unicode, and high-volume repeated calls.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Mar 10, 2026
@github-actions github-actions bot added the community Pull Request from an external contributor label Mar 10, 2026
@codecov
Copy link

codecov bot commented Mar 10, 2026

Codecov Report

❌ Patch coverage is 42.85714% with 40 lines in your changes missing coverage. Please review.
✅ Project coverage is 37.19%. Comparing base (699e356) to head (7afd5b1).
⚠️ Report is 7 commits behind head on release-1.8.1.

Files with missing lines Patch % Lines
src/lfx/src/lfx/base/models/unified_models.py 7.14% 25 Missing and 1 partial ⚠️
src/backend/base/langflow/api/utils/core.py 66.66% 13 Missing ⚠️
src/backend/base/langflow/api/v1/flows.py 66.66% 1 Missing ⚠️

❌ Your project status has failed because the head coverage (41.43%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff               @@
##           release-1.8.1   #12131   +/-   ##
==============================================
  Coverage          37.18%   37.19%           
==============================================
  Files               1592     1592           
  Lines              78216    78274   +58     
  Branches           11865    11872    +7     
==============================================
+ Hits               29087    29111   +24     
- Misses             47461    47495   +34     
  Partials            1668     1668           
Flag Coverage Δ
backend 57.35% <66.66%> (+<0.01%) ⬆️
lfx 41.43% <7.14%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/frontend/src/modals/exportModal/index.tsx 64.44% <ø> (-1.52%) ⬇️
src/frontend/src/utils/reactflowUtils.ts 11.44% <ø> (+0.07%) ⬆️
src/backend/base/langflow/api/v1/flows.py 52.60% <66.66%> (-0.02%) ⬇️
src/backend/base/langflow/api/utils/core.py 63.46% <66.66%> (+0.37%) ⬆️
src/lfx/src/lfx/base/models/unified_models.py 23.19% <7.14%> (-0.55%) ⬇️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Base automatically changed from fix-api-key-components to release-1.8.1 March 12, 2026 16:27
@codeflash-ai codeflash-ai bot deleted the codeflash/optimize-pr12129-2026-03-10T17.38.03 branch March 12, 2026 18:21
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 community Pull Request from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants