Skip to content

⚡️ Speed up function coerce_to_str_if_uuid by 54% in PR #11958 (fix/lfx-chatresp-uuid)#11959

Closed
codeflash-ai[bot] wants to merge 1 commit into
fix/lfx-chatresp-uuidfrom
codeflash/optimize-pr11958-2026-03-01T23.12.15
Closed

⚡️ Speed up function coerce_to_str_if_uuid by 54% in PR #11958 (fix/lfx-chatresp-uuid)#11959
codeflash-ai[bot] wants to merge 1 commit into
fix/lfx-chatresp-uuidfrom
codeflash/optimize-pr11958-2026-03-01T23.12.15

Conversation

@codeflash-ai
Copy link
Copy Markdown
Contributor

@codeflash-ai codeflash-ai Bot commented Mar 1, 2026

⚡️ This pull request contains optimizations for PR #11958

If you approve this dependent PR, these changes will be merged into the original PR branch fix/lfx-chatresp-uuid.

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


📄 54% (0.54x) speedup for coerce_to_str_if_uuid in src/backend/base/langflow/schema/validators.py

⏱️ Runtime : 1.59 milliseconds 1.04 milliseconds (best of 125 runs)

📝 Explanation and details

The optimization adds a fast-path early return for str and None inputs (which account for 56% of calls per profiler) and a bounded module-level cache for hashable inputs, avoiding repeated str(UUID) conversions that cost ~2.5 µs each. The cache reduces cache-hit UUID conversions from ~2.5 µs to ~1.5 µs (profiler lines show _CACHE.get at 1450 ns vs. str(value) at 2491 ns), delivering a 53% speedup despite the 1000-call test_large_scale_repeated_same_uuid test benefiting most. Unhashable inputs bypass the cache via exception handling to preserve the original behavior without raising TypeError, and the 4096-entry limit with arbitrary eviction keeps memory bounded.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 3040 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
from uuid import (  # real UUID classes and factory for constructing UUID instances
    UUID, uuid4)

# imports
import pytest  # used for our unit tests
from langflow.schema.validators import coerce_to_str_if_uuid


def test_basic_with_uuid_instance():
    # Create a concrete UUID instance from a fixed canonical string (deterministic)
    u = UUID("12345678-1234-5678-1234-567812345678")
    # Call the function under test with a UUID instance
    codeflash_output = coerce_to_str_if_uuid(u); result = codeflash_output


@pytest.mark.parametrize(
    "value",
    [
        "simple-string",    # ordinary string should be returned unchanged
        "",                 # empty string edge case
        None,               # None should be returned unchanged
        0,                  # integer should be returned unchanged
        999999999999,       # large integer should be returned unchanged
        b"bytes-value",     # bytes (real type) should be returned unchanged
        True,               # bool (subclass of int) should be returned unchanged
    ],
)
def test_non_uuid_values_return_identity(value):
    # For all non-UUID inputs, the function returns the exact same object/reference/value
    codeflash_output = coerce_to_str_if_uuid(value); ret = codeflash_output


def test_string_that_looks_like_uuid_remains_string_identity():
    # A string that matches a UUID textual form must NOT be converted (it's already a string)
    s = "123e4567-e89b-12d3-a456-426655440000"
    # The function should return the same string object (no conversion)
    codeflash_output = coerce_to_str_if_uuid(s); out = codeflash_output


def test_mutable_non_uuid_objects_return_same_reference():
    # Use real mutable Python objects (list and dict) as inputs — they are not UUIDs
    mutable_list = [1, 2, 3]
    mutable_dict = {"a": 1}
    # The function must return the exact same object (no coercion)
    codeflash_output = coerce_to_str_if_uuid(mutable_list)
    codeflash_output = coerce_to_str_if_uuid(mutable_dict)


def test_large_scale_uuid_and_string_handling():
    # Build 1000 UUID instances deterministically using uuid4 (content doesn't matter for correctness)
    N = 1000
    uuids = [uuid4() for _ in range(N)]  # real UUID instances
    # Create corresponding string representations (these are real Python str objects)
    uuid_strings = [str(u) for u in uuids]

    # Validate conversion for UUID instances at scale
    for u in uuids:
        codeflash_output = coerce_to_str_if_uuid(u); out = codeflash_output

    # Validate identity preservation for string inputs at scale
    for s in uuid_strings:
        codeflash_output = coerce_to_str_if_uuid(s); out = codeflash_output

    # Cross-check mixed input sequence performance/behavior in a single pass
    mixed_inputs = []
    expected = []
    # Interleave UUID instances and their string forms
    for i in range(N):
        if i % 2 == 0:
            mixed_inputs.append(uuids[i])
            expected.append(str(uuids[i]))  # expected string for UUID input
        else:
            mixed_inputs.append(uuid_strings[i])
            expected.append(uuid_strings[i])  # expected same string object for string input

    # Process the mixed list and assert results match expectations
    results = [coerce_to_str_if_uuid(v) for v in mixed_inputs]
    for inp, out, exp in zip(mixed_inputs, results, expected):
        # If the input was a UUID instance, out must be a string equal to exp
        if isinstance(inp, UUID):
            pass
        else:
            pass


def test_none_behavior_explicit():
    # Explicitly test None stays None
    codeflash_output = coerce_to_str_if_uuid(None)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from uuid import UUID

# imports
import pytest
from langflow.schema.validators import coerce_to_str_if_uuid


def test_basic_uuid_to_string():
    """Test that a UUID object is converted to its string representation."""
    test_uuid = UUID('12345678-1234-5678-1234-567812345678')
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_basic_string_unchanged():
    """Test that a string value is returned unchanged."""
    test_string = 'hello world'
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_basic_none_unchanged():
    """Test that None is returned unchanged."""
    codeflash_output = coerce_to_str_if_uuid(None); result = codeflash_output


def test_basic_empty_string_unchanged():
    """Test that an empty string is returned unchanged."""
    test_string = ''
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_basic_uuid_different_version():
    """Test conversion of UUIDs created with different methods."""
    # Create a UUID version 1 (time-based)
    test_uuid_v1 = UUID('550e8400-e29b-41d4-a716-446655440000')
    codeflash_output = coerce_to_str_if_uuid(test_uuid_v1); result = codeflash_output


def test_edge_uuid_with_lowercase_hex():
    """Test UUID with lowercase hexadecimal digits."""
    # Create a UUID and verify it handles lowercase representation
    test_uuid = UUID('abcdef01-2345-6789-abcd-ef0123456789')
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_edge_uuid_with_uppercase_hex():
    """Test UUID with uppercase hexadecimal digits in constructor."""
    # UUIDs normalize to lowercase, so we pass uppercase to the constructor
    test_uuid = UUID('ABCDEF01-2345-6789-ABCD-EF0123456789')
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_edge_string_with_uuid_like_format():
    """Test that a string that looks like a UUID is NOT converted."""
    uuid_string = '12345678-1234-5678-1234-567812345678'
    codeflash_output = coerce_to_str_if_uuid(uuid_string); result = codeflash_output


def test_edge_string_with_special_characters():
    """Test that a string with special characters is unchanged."""
    test_string = '!@#$%^&*()'
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_string_with_newlines():
    """Test that a string with newlines is unchanged."""
    test_string = 'line1\nline2\nline3'
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_string_with_unicode_characters():
    """Test that a string with Unicode characters is unchanged."""
    test_string = '你好世界🌍'
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_string_with_tabs_and_spaces():
    """Test that whitespace is preserved in strings."""
    test_string = '  \t  hello  \t  '
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_zero_uuid():
    """Test UUID with all zeros."""
    test_uuid = UUID('00000000-0000-0000-0000-000000000000')
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_edge_max_uuid():
    """Test UUID with all F's (maximum value)."""
    test_uuid = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_edge_long_string():
    """Test that very long strings are unchanged."""
    test_string = 'a' * 10000
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_numeric_string():
    """Test that a numeric string is unchanged."""
    test_string = '1234567890'
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_uuid_from_bytes():
    """Test UUID created from bytes."""
    test_bytes = b'\x12\x34\x56\x78' * 4  # 16 bytes
    test_uuid = UUID(bytes=test_bytes)
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_edge_uuid_from_int():
    """Test UUID created from a large integer."""
    test_int = 123456789012345678901234567890123456789
    test_uuid = UUID(int=test_int)
    codeflash_output = coerce_to_str_if_uuid(test_uuid); result = codeflash_output


def test_edge_single_character_string():
    """Test single character string."""
    test_string = 'x'
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_edge_whitespace_only_string():
    """Test string with only whitespace."""
    test_string = '   '
    codeflash_output = coerce_to_str_if_uuid(test_string); result = codeflash_output


def test_large_scale_many_uuid_conversions():
    """Test converting 1000 UUID objects to strings."""
    # Create 1000 different UUIDs
    uuids = [UUID(f'00000000-0000-0000-0000-{i:012d}') for i in range(1000)]
    
    # Convert all UUIDs
    results = [coerce_to_str_if_uuid(uuid) for uuid in uuids]


def test_large_scale_many_string_passthrough():
    """Test that 1000 strings pass through unchanged."""
    # Create 1000 different strings
    strings = [f'string_{i}' for i in range(1000)]
    
    # Pass all strings through the function
    results = [coerce_to_str_if_uuid(s) for s in strings]


def test_large_scale_mixed_types():
    """Test 1000 mixed UUID and string inputs."""
    # Create 500 UUIDs and 500 strings
    inputs = []
    for i in range(500):
        inputs.append(UUID(f'00000000-0000-0000-0000-{i:012d}'))
        inputs.append(f'string_{i}')
    
    # Process all inputs
    results = [coerce_to_str_if_uuid(item) for item in inputs]
    # Verify the odd-indexed results (original strings) are unchanged
    for i in range(1, 1000, 2):
        pass


def test_large_scale_mixed_with_none():
    """Test 1000 mixed inputs including None values."""
    # Create mixed inputs with some None values
    inputs = []
    for i in range(333):
        inputs.append(UUID(f'00000000-0000-0000-0000-{i:012d}'))
        inputs.append(f'string_{i}')
        inputs.append(None)
    
    # Process all inputs
    results = [coerce_to_str_if_uuid(item) for item in inputs]


def test_large_scale_repeated_same_uuid():
    """Test 1000 conversions of the same UUID object."""
    # Create a single UUID
    test_uuid = UUID('12345678-1234-5678-1234-567812345678')
    
    # Convert the same UUID 1000 times
    results = [coerce_to_str_if_uuid(test_uuid) for _ in range(1000)]


def test_large_scale_repeated_same_string():
    """Test 1000 passthrough operations on the same string."""
    # Create a single string
    test_string = 'test_value'
    
    # Pass the same string through 1000 times
    results = [coerce_to_str_if_uuid(test_string) for _ in range(1000)]


def test_large_scale_sequential_type_alternation():
    """Test 1000 operations alternating between types."""
    test_uuid = UUID('12345678-1234-5678-1234-567812345678')
    test_string = 'alternating_string'
    
    # Alternate between UUID and string 500 times each
    results = []
    for i in range(500):
        results.append(coerce_to_str_if_uuid(test_uuid))
        results.append(coerce_to_str_if_uuid(test_string))
    
    # Verify the results alternate correctly
    for i in range(0, 1000, 2):
        pass
    for i in range(1, 1000, 2):
        pass


def test_large_scale_many_none_values():
    """Test 1000 None values passed through the function."""
    # Pass None 1000 times
    results = [coerce_to_str_if_uuid(None) for _ in range(1000)]


def test_large_scale_very_long_string():
    """Test passthrough of a very long string."""
    # Create a string with 100,000 characters
    test_string = 'x' * 100000
    
    # Pass through 10 times
    results = [coerce_to_str_if_uuid(test_string) for _ in range(10)]
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-pr11958-2026-03-01T23.12.15 and push.

Codeflash

The optimization adds a fast-path early return for `str` and `None` inputs (which account for 56% of calls per profiler) and a bounded module-level cache for hashable inputs, avoiding repeated `str(UUID)` conversions that cost ~2.5 µs each. The cache reduces cache-hit UUID conversions from ~2.5 µs to ~1.5 µs (profiler lines show `_CACHE.get` at 1450 ns vs. `str(value)` at 2491 ns), delivering a 53% speedup despite the 1000-call `test_large_scale_repeated_same_uuid` test benefiting most. Unhashable inputs bypass the cache via exception handling to preserve the original behavior without raising `TypeError`, and the 4096-entry limit with arbitrary eviction keeps memory bounded.
@codeflash-ai codeflash-ai Bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Mar 1, 2026
@github-actions github-actions Bot added the community Pull Request from an external contributor label Mar 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 1, 2026

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 22%
22.04% (7566/34320) 14.69% (3947/26855) 14.88% (1081/7261)

Unit Test Results

Tests Skipped Failures Errors Time
2507 0 💤 0 ❌ 0 🔥 41.975s ⏱️

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 1, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.41%. Comparing base (5fb1e89) to head (60bef39).
⚠️ Report is 14 commits behind head on fix/lfx-chatresp-uuid.

Files with missing lines Patch % Lines
src/backend/base/langflow/schema/validators.py 80.00% 4 Missing ⚠️

❌ Your project status has failed because the head coverage (41.54%) 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                    @@
##           fix/lfx-chatresp-uuid   #11959      +/-   ##
=========================================================
+ Coverage                  36.02%   36.41%   +0.39%     
=========================================================
  Files                       1570     1570              
  Lines                      76679    76698      +19     
  Branches                   11629    11629              
=========================================================
+ Hits                       27623    27932     +309     
+ Misses                     47479    47188     -291     
- Partials                    1577     1578       +1     
Flag Coverage Δ
backend 56.32% <80.00%> (+1.43%) ⬆️
frontend 19.80% <ø> (ø)
lfx 41.54% <ø> (-0.01%) ⬇️

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

Files with missing lines Coverage Δ
src/backend/base/langflow/schema/validators.py 44.44% <80.00%> (+14.89%) ⬆️

... and 52 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.

@ogabrielluiz
Copy link
Copy Markdown
Contributor

Closing automated codeflash PR.

@codeflash-ai codeflash-ai Bot deleted the codeflash/optimize-pr11958-2026-03-01T23.12.15 branch March 3, 2026 18:09
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.

1 participant