Skip to content

⚡️ Speed up method OpikTracer._get_config by 11% in PR #7183 (feat/global_vars_tracing) #7584

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

Open
wants to merge 1 commit into
base: feat/global_vars_tracing
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Apr 11, 2025

⚡️ This pull request contains optimizations for PR #7183

If you approve this dependent PR, these changes will be merged into the original PR branch feat/global_vars_tracing.

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


📄 11% (0.11x) speedup for OpikTracer._get_config in src/backend/base/langflow/services/tracing/opik.py

⏱️ Runtime : 39.9 microseconds 35.9 microseconds (best of 246 runs)

📝 Explanation and details

To optimize the code for better performance, we can make minimal yet effective changes. These changes mainly focus on.

  1. Reducing repetitive calls,
  2. Using more efficient default values,
  3. Simplifying logic where possible.

Here’s the optimized version of the code.

Here's what we modified to optimize the code.

  1. Avoid redundant None default parameter checks with if host or api_key: and self.global_vars or {} directly in instantiation.
  2. Unified the environmental variable fetch with no redundant None assignment since os.getenv returns None by default if the variable is not set.
  3. Simplified the boolean conversion with a direct bool(config and self._setup_opik(config, trace_id)).

These changes are oriented toward reducing function execution time by minimizing branching logic and redundant checks. The core functionality and output remain unchanged.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 16 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage
🌀 Generated Regression Tests Details
from __future__ import annotations

import os
from uuid import uuid4

# imports
import pytest  # used for our unit tests
from langflow.services.tracing.base import BaseTracer
from langflow.services.tracing.opik import OpikTracer

# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.


from __future__ import annotations

import os
from uuid import UUID

# imports
import pytest  # used for our unit tests
from langflow.services.tracing.base import BaseTracer
from langflow.services.tracing.opik import OpikTracer

# unit tests

def test_global_vars_all_keys_present():
    global_vars = {
        "OPIK_URL_OVERRIDE": "http://example.com",
        "OPIK_API_KEY": "12345",
        "OPIK_WORKSPACE": "workspace1"
    }
    expected = {
        "host": "http://example.com",
        "api_key": "12345",
        "workspace": "workspace1"
    }
    codeflash_output = OpikTracer._get_config(global_vars)

def test_global_vars_some_keys_missing():
    global_vars = {
        "OPIK_API_KEY": "12345"
    }
    expected = {
        "host": None,
        "api_key": "12345",
        "workspace": None
    }
    codeflash_output = OpikTracer._get_config(global_vars)

def test_global_vars_no_relevant_keys():
    global_vars = {
        "SOME_OTHER_KEY": "value"
    }
    expected = {}
    codeflash_output = OpikTracer._get_config(global_vars)

def test_env_vars_set(monkeypatch):
    monkeypatch.setenv("OPIK_URL_OVERRIDE", "http://example.com")
    monkeypatch.setenv("OPIK_API_KEY", "12345")
    monkeypatch.setenv("OPIK_WORKSPACE", "workspace1")
    expected = {
        "host": "http://example.com",
        "api_key": "12345",
        "workspace": "workspace1"
    }
    codeflash_output = OpikTracer._get_config(None)

def test_env_vars_some_missing(monkeypatch):
    monkeypatch.setenv("OPIK_API_KEY", "12345")
    expected = {
        "host": None,
        "api_key": "12345",
        "workspace": None
    }
    codeflash_output = OpikTracer._get_config(None)

def test_env_vars_none_set(monkeypatch):
    expected = {}
    codeflash_output = OpikTracer._get_config(None)

def test_global_vars_override_env_vars(monkeypatch):
    monkeypatch.setenv("OPIK_API_KEY", "12345")
    monkeypatch.setenv("OPIK_URL_OVERRIDE", "http://example.com")
    global_vars = {
        "OPIK_API_KEY": "67890"
    }
    expected = {
        "host": "http://example.com",
        "api_key": "67890",
        "workspace": None
    }
    codeflash_output = OpikTracer._get_config(global_vars)

def test_global_vars_partial_override(monkeypatch):
    monkeypatch.setenv("OPIK_URL_OVERRIDE", "http://example.com")
    global_vars = {
        "OPIK_API_KEY": "67890"
    }
    expected = {
        "host": "http://example.com",
        "api_key": "67890",
        "workspace": None
    }
    codeflash_output = OpikTracer._get_config(global_vars)

def test_empty_global_vars():
    global_vars = {}
    expected = {}
    codeflash_output = OpikTracer._get_config(global_vars)

def test_global_vars_with_none_values():
    global_vars = {
        "OPIK_URL_OVERRIDE": None,
        "OPIK_API_KEY": None,
        "OPIK_WORKSPACE": None
    }
    expected = {}
    codeflash_output = OpikTracer._get_config(global_vars)

def test_env_vars_with_empty_strings(monkeypatch):
    monkeypatch.setenv("OPIK_URL_OVERRIDE", "")
    monkeypatch.setenv("OPIK_API_KEY", "")
    monkeypatch.setenv("OPIK_WORKSPACE", "")
    expected = {}
    codeflash_output = OpikTracer._get_config(None)

def test_invalid_global_vars_string():
    global_vars = "invalid_string"
    with pytest.raises(AttributeError):
        OpikTracer._get_config(global_vars)

def test_invalid_global_vars_list():
    global_vars = ["list", "of", "values"]
    with pytest.raises(AttributeError):
        OpikTracer._get_config(global_vars)

def test_invalid_global_vars_int():
    global_vars = 12345
    with pytest.raises(AttributeError):
        OpikTracer._get_config(global_vars)

def test_large_number_of_irrelevant_keys():
    global_vars = {f"KEY_{i}": f"value_{i}" for i in range(1000)}
    expected = {}
    codeflash_output = OpikTracer._get_config(global_vars)

def test_large_number_of_env_vars(monkeypatch):
    for i in range(1000):
        monkeypatch.setenv(f"KEY_{i}", f"value_{i}")
    expected = {}
    codeflash_output = OpikTracer._get_config(None)
# 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-pr7183-2025-04-11T12.26.21 and push.

Codeflash

…/global_vars_tracing`)

To optimize the code for better performance, we can make minimal yet effective changes. These changes mainly focus on.
1. Reducing repetitive calls,
2. Using more efficient default values,
3. Simplifying logic where possible.

Here’s the optimized version of the code.



Here's what we modified to optimize the code.
1. Avoid redundant `None` default parameter checks with `if host or api_key:` and `self.global_vars or {}` directly in instantiation.
2. Unified the environmental variable fetch with no redundant `None` assignment since `os.getenv` returns `None` by default if the variable is not set.
3. Simplified the boolean conversion with a direct `bool(config and self._setup_opik(config, trace_id))`.

These changes are oriented toward reducing function execution time by minimizing branching logic and redundant checks. The core functionality and output remain unchanged.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Apr 11, 2025
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. enhancement New feature or request labels Apr 11, 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 enhancement New feature or request size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants