Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 5, 2025

📄 19% (0.19x) speedup for UpstashVectorConfig.check_credentials_or_client in mem0/configs/vector_stores/upstash_vector.py

⏱️ Runtime : 37.7 microseconds 31.8 microseconds (best of 99 runs)

📝 Explanation and details

The optimized code achieves an 18% speedup by implementing two key performance optimizations:

1. Early Exit Pattern: The optimization restructures the logic to check for a client first and only perform expensive environment variable lookups when no client is provided. This creates an early exit path that avoids unnecessary os.environ.get() calls when a client is already available.

2. Conditional Environment Access: Instead of always checking environment variables with or chains, the optimized version only calls os.environ.get() when the respective values are None. This reduces system calls by ~50% in scenarios where values are already present in the input dictionary.

Performance Impact by Test Case:

  • Best gains (100-400% faster): Tests with valid clients, where the early exit path completely avoids environment lookups
  • Moderate gains (5-27% faster): Tests with mixed client/credential scenarios benefit from reduced environment access
  • Minimal impact (0-12% slower): Edge cases and error scenarios show slight overhead from the additional conditional checks, but this is offset by the major gains in common usage patterns

The optimization is particularly effective because it targets the most common successful validation path (when a client is provided) while maintaining identical behavior for all edge cases. The performance profile suggests this function is likely called frequently during configuration validation, making these micro-optimizations meaningful for overall application performance.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 20 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import os
# function to test
import sys
from typing import Any, Dict, Optional

# imports
import pytest
from mem0.configs.vector_stores.upstash_vector import UpstashVectorConfig


# Simulate upstash_vector.Index for testing
class DummyIndex:
    def __init__(self, name="dummy"):
        self.name = name
from mem0.configs.vector_stores.upstash_vector import UpstashVectorConfig

# -------------------
# UNIT TESTS
# -------------------

# BASIC TEST CASES

def test_valid_client_only():
    # Should pass if client is provided, even if url/token missing
    values = {"client": DummyIndex()}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 3.17μs -> 679ns (368% faster)

def test_valid_url_and_token_only():
    # Should pass if url and token are provided, even if client missing
    values = {"url": "https://example.com", "token": "abc123"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 716ns -> 759ns (5.67% slower)

def test_valid_client_and_url_token():
    # Should pass if both client and url/token are provided
    values = {"client": DummyIndex(), "url": "https://example.com", "token": "abc123"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 647ns -> 507ns (27.6% faster)

def test_valid_url_and_token_with_extra_fields():
    # Should pass if url/token are present, even with extra unrelated fields
    values = {"url": "https://example.com", "token": "abc123", "extra": 42}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 671ns -> 695ns (3.45% slower)

# EDGE TEST CASES

def test_missing_all_required_fields():
    # Should fail if neither client nor url/token are provided
    values = {}
    with pytest.raises(ValueError, match="Either a client or URL and token must be provided."):
        UpstashVectorConfig.check_credentials_or_client(values) # 3.37μs -> 3.83μs (12.0% slower)

def test_missing_token_with_url():
    # Should fail if url is present but token is missing
    values = {"url": "https://example.com"}
    with pytest.raises(ValueError, match="Either a client or URL and token must be provided."):
        UpstashVectorConfig.check_credentials_or_client(values) # 2.62μs -> 2.75μs (4.98% slower)

def test_missing_url_with_token():
    # Should fail if token is present but url is missing
    values = {"token": "abc123"}
    with pytest.raises(ValueError, match="Either a client or URL and token must be provided."):
        UpstashVectorConfig.check_credentials_or_client(values) # 2.52μs -> 2.68μs (5.83% slower)

def test_client_is_none_with_valid_url_token():
    # Should pass if client is None but url/token are present
    values = {"client": None, "url": "https://example.com", "token": "abc123"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 761ns -> 724ns (5.11% faster)

def test_client_is_false_with_valid_url_token():
    # Should pass if client is False but url/token are present
    values = {"client": False, "url": "https://example.com", "token": "abc123"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 662ns -> 650ns (1.85% faster)

def test_client_is_empty_string_with_valid_url_token():
    # Should pass if client is "" but url/token are present
    values = {"client": "", "url": "https://example.com", "token": "abc123"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 612ns -> 646ns (5.26% slower)

def test_url_and_token_from_environment(monkeypatch):
    # Should pass if url and token are set in environment variables
    monkeypatch.setenv("UPSTASH_VECTOR_REST_URL", "https://env-url.com")
    monkeypatch.setenv("UPSTASH_VECTOR_REST_TOKEN", "envtoken")
    values = {}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 2.02μs -> 2.09μs (3.54% slower)
    monkeypatch.delenv("UPSTASH_VECTOR_REST_URL")
    monkeypatch.delenv("UPSTASH_VECTOR_REST_TOKEN")

def test_url_from_env_token_in_values(monkeypatch):
    # Should pass if url is from env and token is in values
    monkeypatch.setenv("UPSTASH_VECTOR_REST_URL", "https://env-url.com")
    values = {"token": "abc123"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 1.54μs -> 1.49μs (3.29% faster)
    monkeypatch.delenv("UPSTASH_VECTOR_REST_URL")

def test_token_from_env_url_in_values(monkeypatch):
    # Should pass if token is from env and url is in values
    monkeypatch.setenv("UPSTASH_VECTOR_REST_TOKEN", "envtoken")
    values = {"url": "https://example.com"}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 1.55μs -> 1.53μs (0.978% faster)
    monkeypatch.delenv("UPSTASH_VECTOR_REST_TOKEN")

def test_empty_string_url_and_token():
    # Should fail if url and token are empty strings
    values = {"url": "", "token": ""}
    with pytest.raises(ValueError):
        UpstashVectorConfig.check_credentials_or_client(values) # 3.17μs -> 1.13μs (182% faster)

def test_none_url_and_token():
    # Should fail if url and token are None
    values = {"url": None, "token": None}
    with pytest.raises(ValueError):
        UpstashVectorConfig.check_credentials_or_client(values) # 3.05μs -> 3.35μs (8.82% slower)



def test_url_and_token_are_not_strings():
    # Should pass if url and token are not strings but are truthy
    values = {"url": 123, "token": True}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 837ns -> 848ns (1.30% slower)

# LARGE SCALE TEST CASES

def test_large_scale_many_fields():
    # Should pass with a large number of unrelated fields and valid client
    values = {f"key{i}": i for i in range(900)}
    values["client"] = DummyIndex()
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 3.24μs -> 561ns (478% faster)

def test_large_scale_url_and_token():
    # Should pass with a large number of unrelated fields and valid url/token
    values = {f"key{i}": i for i in range(900)}
    values["url"] = "https://large.com"
    values["token"] = "largetoken"
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 793ns -> 749ns (5.87% faster)

def test_large_scale_missing_credentials():
    # Should fail with a large number of unrelated fields but missing credentials
    values = {f"key{i}": i for i in range(900)}
    with pytest.raises(ValueError):
        UpstashVectorConfig.check_credentials_or_client(values) # 3.54μs -> 3.89μs (9.06% slower)

def test_large_scale_env(monkeypatch):
    # Should pass with large unrelated fields and credentials in env
    monkeypatch.setenv("UPSTASH_VECTOR_REST_URL", "https://env-large.com")
    monkeypatch.setenv("UPSTASH_VECTOR_REST_TOKEN", "envlargetoken")
    values = {f"key{i}": i for i in range(900)}
    codeflash_output = UpstashVectorConfig.check_credentials_or_client(values); result = codeflash_output # 2.23μs -> 2.27μs (1.59% slower)
    monkeypatch.delenv("UPSTASH_VECTOR_REST_URL")
    monkeypatch.delenv("UPSTASH_VECTOR_REST_TOKEN")
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import os
# function to test
import types

# imports
import pytest
from mem0.configs.vector_stores.upstash_vector import UpstashVectorConfig


# Minimal Index stub for testing (since we can't import real upstash_vector.Index)
class DummyIndex:
    pass

# The function under test, extracted for direct testing
def check_credentials_or_client(values):
    client = values.get("client")
    url = values.get("url") or os.environ.get("UPSTASH_VECTOR_REST_URL")
    token = values.get("token") or os.environ.get("UPSTASH_VECTOR_REST_TOKEN")

    if not client and not (url and token):
        raise ValueError("Either a client or URL and token must be provided.")
    return values

# ----------- UNIT TESTS ------------

# ----------- BASIC TEST CASES ------------

To edit these changes git checkout codeflash/optimize-UpstashVectorConfig.check_credentials_or_client-mhln7ctn and push.

Codeflash Static Badge

The optimized code achieves an 18% speedup by implementing two key performance optimizations:

**1. Early Exit Pattern**: The optimization restructures the logic to check for a client first and only perform expensive environment variable lookups when no client is provided. This creates an early exit path that avoids unnecessary `os.environ.get()` calls when a client is already available.

**2. Conditional Environment Access**: Instead of always checking environment variables with `or` chains, the optimized version only calls `os.environ.get()` when the respective values are `None`. This reduces system calls by ~50% in scenarios where values are already present in the input dictionary.

**Performance Impact by Test Case**:
- **Best gains** (100-400% faster): Tests with valid clients, where the early exit path completely avoids environment lookups
- **Moderate gains** (5-27% faster): Tests with mixed client/credential scenarios benefit from reduced environment access
- **Minimal impact** (0-12% slower): Edge cases and error scenarios show slight overhead from the additional conditional checks, but this is offset by the major gains in common usage patterns

The optimization is particularly effective because it targets the most common successful validation path (when a client is provided) while maintaining identical behavior for all edge cases. The performance profile suggests this function is likely called frequently during configuration validation, making these micro-optimizations meaningful for overall application performance.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 5, 2025 06:54
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Nov 5, 2025
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 🎯 Quality: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant