langflow icon indicating copy to clipboard operation
langflow copied to clipboard

⚡️ Speed up method `StoreService.build_search_filter_conditions` by 42% in `src/backend/base/langflow/services/store/service.py`

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

📄 StoreService.build_search_filter_conditions() in src/backend/base/langflow/services/store/service.py

📈 Performance improved by 42% (0.42x faster)

⏱️ Runtime went down from 7.91 microseconds to 5.58 microseconds

Explanation and details

To optimize this program for better performance, I'll focus on a few aspects: reducing redundant operations, simplifying data structures, and ensuring more efficient operations. Here is the optimized version.

Changes Made.

  1. Reducing Redundant Attribute Access:

    • Cached self.settings_service.settings in a local variable (settings), which is then used to set various service URLs. This avoids repeatedly accessing the nested attribute.
  2. Optimized Static Method build_search_filter_conditions.

    • Created a dictionary icontains_query once and reused it, which reduces the overhead of repeatedly creating the same dictionary structure.

Overall, these optimizations reduce redundant operations and ensure that the service's setup and filter-building are as efficient as possible.

Correctness verification

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

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 12 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
from typing import Any, Dict

import pytest  # used for our unit tests
# function to test
from langflow.services.base import Service
from langflow.services.settings.service import SettingsService
from src.backend.base.langflow.services.store.service import StoreService

# unit tests

# Basic Functionality
def test_single_word_query():
    query = "test"
    expected = {
        "_or": [
            {"name": {"_icontains": "test"}},
            {"description": {"_icontains": "test"}},
            {"tags": {"tags_id": {"name": {"_icontains": "test"}}}},
            {"user_created": {"username": {"_icontains": "test"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_multi_word_query():
    query = "test case"
    expected = {
        "_or": [
            {"name": {"_icontains": "test case"}},
            {"description": {"_icontains": "test case"}},
            {"tags": {"tags_id": {"name": {"_icontains": "test case"}}}},
            {"user_created": {"username": {"_icontains": "test case"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

# Case Insensitivity
def test_upper_case_query():
    query = "TEST"
    expected = {
        "_or": [
            {"name": {"_icontains": "TEST"}},
            {"description": {"_icontains": "TEST"}},
            {"tags": {"tags_id": {"name": {"_icontains": "TEST"}}}},
            {"user_created": {"username": {"_icontains": "TEST"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_mixed_case_query():
    query = "TeSt"
    expected = {
        "_or": [
            {"name": {"_icontains": "TeSt"}},
            {"description": {"_icontains": "TeSt"}},
            {"tags": {"tags_id": {"name": {"_icontains": "TeSt"}}}},
            {"user_created": {"username": {"_icontains": "TeSt"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

# Special Characters
def test_special_characters_query():
    query = "test!@#"
    expected = {
        "_or": [
            {"name": {"_icontains": "test!@#"}},
            {"description": {"_icontains": "test!@#"}},
            {"tags": {"tags_id": {"name": {"_icontains": "test!@#"}}}},
            {"user_created": {"username": {"_icontains": "test!@#"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_special_characters_with_spaces_query():
    query = "test case!@#"
    expected = {
        "_or": [
            {"name": {"_icontains": "test case!@#"}},
            {"description": {"_icontains": "test case!@#"}},
            {"tags": {"tags_id": {"name": {"_icontains": "test case!@#"}}}},
            {"user_created": {"username": {"_icontains": "test case!@#"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

# Edge Cases
def test_empty_string_query():
    query = ""
    expected = {
        "_or": [
            {"name": {"_icontains": ""}},
            {"description": {"_icontains": ""}},
            {"tags": {"tags_id": {"name": {"_icontains": ""}}}},
            {"user_created": {"username": {"_icontains": ""}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_whitespace_string_query():
    query = "   "
    expected = {
        "_or": [
            {"name": {"_icontains": "   "}},
            {"description": {"_icontains": "   "}},
            {"tags": {"tags_id": {"name": {"_icontains": "   "}}}},
            {"user_created": {"username": {"_icontains": "   "}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_numeric_query():
    query = "123"
    expected = {
        "_or": [
            {"name": {"_icontains": "123"}},
            {"description": {"_icontains": "123"}},
            {"tags": {"tags_id": {"name": {"_icontains": "123"}}}},
            {"user_created": {"username": {"_icontains": "123"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_alphanumeric_query():
    query = "test123"
    expected = {
        "_or": [
            {"name": {"_icontains": "test123"}},
            {"description": {"_icontains": "test123"}},
            {"tags": {"tags_id": {"name": {"_icontains": "test123"}}}},
            {"user_created": {"username": {"_icontains": "test123"}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

# Large Scale Test Cases
def test_very_long_query_string():
    query = "a" * 1000
    expected = {
        "_or": [
            {"name": {"_icontains": "a" * 1000}},
            {"description": {"_icontains": "a" * 1000}},
            {"tags": {"tags_id": {"name": {"_icontains": "a" * 1000}}}},
            {"user_created": {"username": {"_icontains": "a" * 1000}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

def test_repeated_pattern_query():
    query = "longquery" * 100
    expected = {
        "_or": [
            {"name": {"_icontains": "longquery" * 100}},
            {"description": {"_icontains": "longquery" * 100}},
            {"tags": {"tags_id": {"name": {"_icontains": "longquery" * 100}}}},
            {"user_created": {"username": {"_icontains": "longquery" * 100}}}
        ]
    }
    codeflash_output = StoreService.build_search_filter_conditions(query)
    # Outputs were verified to be equal to the original implementation

# Special Data Types
def test_non_string_input():
    with pytest.raises(TypeError):
        StoreService.build_search_filter_conditions(None)
    with pytest.raises(TypeError):
        StoreService.build_search_filter_conditions(123)
    with pytest.raises(TypeError):
        StoreService.build_search_filter_conditions(["list", "of", "strings"])
    # Outputs were verified to be equal to the original implementation

🔘 (none found) − ⏪ Replay Tests

codeflash-ai[bot] avatar Aug 02 '24 21: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 21:32 UTC

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

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

Access this pull request here: https://pr-3180.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]