Skip to content
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

⚡️ Speed up method AstraDBVectorStoreComponent.get_database_object by 1,269% in PR #6236 (LFOSS-492) #6640

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions src/backend/base/langflow/components/vectorstores/astradb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
from collections import defaultdict
from dataclasses import asdict, dataclass, field

Expand Down Expand Up @@ -433,30 +434,32 @@
return db.get("api_endpoint")

def get_api_endpoint(self):
return self.get_api_endpoint_static(
token=self.token,
environment=self.environment,
api_endpoint=self.api_endpoint,
database_name=self.database_name,
)
if not self.api_endpoint_cache:
self.api_endpoint_cache = self.get_api_endpoint_static_cached(
token=self.token,
environment=self.environment,
api_endpoint=self.api_endpoint,
Comment on lines +437 to +441
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not self.api_endpoint_cache:
self.api_endpoint_cache = self.get_api_endpoint_static_cached(
token=self.token,
environment=self.environment,
api_endpoint=self.api_endpoint,
if self.api_endpoint_cache is None:
self.token, self.environment, self.api_endpoint, self.database_name

database_name=self.database_name,
)
return self.api_endpoint_cache

def get_keyspace(self):
keyspace = self.keyspace

if keyspace:
return keyspace.strip()

if self.keyspace:
return self.strip_keyspace(self.keyspace)
return None
Comment on lines +447 to 449
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.keyspace:
return self.strip_keyspace(self.keyspace)
return None
return self.strip_keyspace(self.keyspace) if self.keyspace else None


def get_database_object(self, api_endpoint: str | None = None):
if self.database_object_cache:
return self.database_object_cache

try:
client = DataAPIClient(token=self.token, environment=self.environment)

return client.get_database(
self.database_object_cache = client.get_database(
api_endpoint=api_endpoint or self.get_api_endpoint(),
token=self.token,
keyspace=self.get_keyspace(),
)
return self.database_object_cache

Check failure on line 462 in src/backend/base/langflow/components/vectorstores/astradb.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (TRY300)

src/backend/base/langflow/components/vectorstores/astradb.py:462:13: TRY300 Consider moving this statement to an `else` block
except Exception as e:
msg = f"Error fetching database object: {e}"
raise ValueError(msg) from e
Expand Down Expand Up @@ -946,3 +949,22 @@
"search_type": self._map_search_type(),
"search_kwargs": search_args,
}

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.api_endpoint_cache = None
self.database_object_cache = None

@functools.lru_cache(maxsize=128)

Check failure on line 958 in src/backend/base/langflow/components/vectorstores/astradb.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (B019)

src/backend/base/langflow/components/vectorstores/astradb.py:958:5: B019 Use of `functools.lru_cache` or `functools.cache` on methods can lead to memory leaks
def get_api_endpoint_static_cached(self, token, environment, api_endpoint, database_name):
return self.get_api_endpoint_static(
token=token,
environment=environment,
api_endpoint=api_endpoint,
database_name=database_name,
)
Comment on lines +960 to +965
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self.get_api_endpoint_static(
token=token,
environment=environment,
api_endpoint=api_endpoint,
database_name=database_name,
)
return self.get_api_endpoint_static(token, environment, api_endpoint, database_name)


@staticmethod
@functools.lru_cache(maxsize=128)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@functools.lru_cache(maxsize=128)
@functools.cache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@functools.lru_cache(maxsize=128)
@functools.lru_cache(maxsize=512) # Increased cache size for better performance on frequent look-ups

def strip_keyspace(keyspace):
return keyspace.strip()
Loading
Loading