Skip to content

Commit 526176d

Browse files
⚡️ Speed up method OpikTracer.get_required_variable_names by 21% in PR #7183 (feat/global_vars_tracing)
To optimize the code for faster execution, we can make a few changes related to avoiding unnecessary function calls, improving memory usage, and setting values more efficiently. 1. Move the call to `OpikTracer.get_required_variable_names()` out of the loop in `__init__()` to avoid redundant function calls. 2. Eliminate unnecessary intermediate variables where possible. Here's the optimized version of the code. Changes made. 1. Called `get_required_variable_names()` once and stored the result in `required_variables` to avoid redundant function calls. 2. Added a check for `global_vars` before the loop to avoid iterating if `global_vars` is `None`. These changes should help in reducing the function call overhead and avoiding unnecessary computations, thus potentially improving the runtime.
1 parent 17a3766 commit 526176d

File tree

1 file changed

+6
-2
lines changed
  • src/backend/base/langflow/services/tracing

1 file changed

+6
-2
lines changed

src/backend/base/langflow/services/tracing/opik.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import types
55
from typing import TYPE_CHECKING, Any
6+
from uuid import UUID
67

78
from langchain_core.documents import Document
89
from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage
@@ -59,8 +60,11 @@ def __init__(
5960
self.flow_id = trace_name.split(" - ")[-1]
6061
self.spans: dict = {}
6162

62-
for key in OpikTracer.get_required_variable_names():
63-
set_env_from_globals(key, global_vars)
63+
required_variables = OpikTracer.get_required_variable_names() # Call once
64+
65+
if global_vars: # Add a check to avoid unnecessary iteration
66+
for key in required_variables:
67+
set_env_from_globals(key, global_vars)
6468

6569
config = self._get_config()
6670
self._ready: bool = self._setup_opik(config, trace_id) if config else False

0 commit comments

Comments
 (0)