perf: ⚡️ Speed up convert_kwargs() by 9% in src/backend/base/langflow/interface/initialize/loading.py (#2529)
* refactor(base.py): refactor logic to find start_component_id based on multiple keywords for improved flexibility and readability
* feat(schema.py): add WebhookInput component type to INPUT_COMPONENTS list for handling webhook inputs in the graph schema
* refactor(base.py): refactor logic to determine start_component_id based on webhook or chat component presence in input vertices
* refactor: prioritize webhook component for determining start_component_id
* ⚡️ Speed up convert_kwargs() by 9%
To optimize the given Python program, we can focus on a few key areas.
1. **Avoid Repeated Lookups:** Instead of repeatedly looking up keys and values in the dictionary, we can iterate over items directly.
2. **Efficient JSON Parsing:** Using `orjson` is already a good choice for performance. We will handle the exception based on `orjson` capabilities.
3. **In-place Modification:** We can modify the dictionary in place without creating additional lists.
Here is the optimized program.
### Changes Made.
1. **Direct Looping:** We iterate directly over `params.items()` to process keys and values together, which helps avoid multiple lookups.
2. **Exception Handling:** We catch `orjson.JSONDecodeError` directly, avoiding unnecessary import and potential mismatches.
3. **Deferred Removal:** We collect keys to remove in `items_to_remove` and then remove them outside the loop, which helps avoid modification issues during iteration.
This should result in more efficient iteration and handling while reducing overhead from unnecessary operations.
* [autofix.ci] apply automated fixes
---------
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
6f827ca3dd
commit
4954b3fa66
1 changed files with 22 additions and 14 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
from typing import TYPE_CHECKING, Any, Type
|
from typing import TYPE_CHECKING, Any, Type
|
||||||
|
|
@ -59,7 +58,10 @@ async def build_component_and_get_results(
|
||||||
# Remove code from params
|
# Remove code from params
|
||||||
class_object: Type["CustomComponent" | "Component"] = eval_custom_component_code(params_copy.pop("code"))
|
class_object: Type["CustomComponent" | "Component"] = eval_custom_component_code(params_copy.pop("code"))
|
||||||
custom_component: "CustomComponent" | "Component" = class_object(
|
custom_component: "CustomComponent" | "Component" = class_object(
|
||||||
user_id=user_id, parameters=params_copy, vertex=vertex, tracing_service=tracing_service
|
user_id=user_id,
|
||||||
|
parameters=params_copy,
|
||||||
|
vertex=vertex,
|
||||||
|
tracing_service=tracing_service,
|
||||||
)
|
)
|
||||||
params_copy = update_params_with_load_from_db_fields(
|
params_copy = update_params_with_load_from_db_fields(
|
||||||
custom_component, params_copy, vertex.load_from_db_fields, fallback_to_env_vars
|
custom_component, params_copy, vertex.load_from_db_fields, fallback_to_env_vars
|
||||||
|
|
@ -84,22 +86,28 @@ def convert_params_to_sets(params):
|
||||||
|
|
||||||
|
|
||||||
def convert_kwargs(params):
|
def convert_kwargs(params):
|
||||||
# if *kwargs are passed as a string, convert to dict
|
# Loop through items to avoid repeated lookups
|
||||||
# first find any key that has kwargs or config in it
|
items_to_remove = []
|
||||||
kwargs_keys = [key for key in params.keys() if "kwargs" in key or "config" in key]
|
for key, value in params.items():
|
||||||
for key in kwargs_keys:
|
if "kwargs" in key or "config" in key:
|
||||||
if isinstance(params[key], str):
|
if isinstance(value, str):
|
||||||
try:
|
try:
|
||||||
params[key] = orjson.loads(params[key])
|
params[key] = orjson.loads(value)
|
||||||
except json.JSONDecodeError:
|
except orjson.JSONDecodeError:
|
||||||
# if the string is not a valid json string, we will
|
items_to_remove.append(key)
|
||||||
# remove the key from the params
|
|
||||||
|
# Remove invalid keys outside the loop to avoid modifying dict during iteration
|
||||||
|
for key in items_to_remove:
|
||||||
params.pop(key, None)
|
params.pop(key, None)
|
||||||
|
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
def update_params_with_load_from_db_fields(
|
def update_params_with_load_from_db_fields(
|
||||||
custom_component: "CustomComponent", params, load_from_db_fields, fallback_to_env_vars=False
|
custom_component: "CustomComponent",
|
||||||
|
params,
|
||||||
|
load_from_db_fields,
|
||||||
|
fallback_to_env_vars=False,
|
||||||
):
|
):
|
||||||
# For each field in load_from_db_fields, we will check if it's in the params
|
# For each field in load_from_db_fields, we will check if it's in the params
|
||||||
# and if it is, we will get the value from the custom_component.keys(name)
|
# and if it is, we will get the value from the custom_component.keys(name)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue