Skip to content

Commit

Permalink
⚡️ Speed up method `AstraDBVectorStoreComponent.reset_collection_list…
Browse files Browse the repository at this point in the history
…` by 12% in PR #6048 (`bugfix-dev-astradb`)

To optimize the given Python program for better runtime performance, we can reduce the number of times certain functions are called and avoid redundant computations. We'll make changes to avoid multiple calls to retrieve the collection data and reuse already created objects where possible.



### Changes Made.
1. **Caching Keyspace**: By caching the keyspace value in a variable (`keyspace`), we avoid multiple calls to the `get_keyspace()` method.
2. **Inlined Metadata Extraction**: Instead of extracting metadata in the list comprehension directly, we define a helper function `get_collection_metadata` to avoid re-fetching data.
3. **Single Pass for Options and Metadata**: Combined options and metadata generation into a single pass loop to avoid repeated dictionary comprehension.

These changes reduce function calls, loop iterations, and improve the readability and maintainability of the code.
  • Loading branch information
codeflash-ai[bot] authored Feb 3, 2025
1 parent 61a8b61 commit ccef0d6
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/backend/base/langflow/components/vectorstores/astradb.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,37 +459,38 @@ def _initialize_database_options(self):
def _initialize_collection_options(self, api_endpoint: str | None = None):
# Retrieve the database object
database = self.get_database_object(api_endpoint=api_endpoint)
keyspace = self.get_keyspace() # Cache the keyspace

# Get the list of collections
collection_list = list(database.list_collections(keyspace=self.get_keyspace()))
collection_list = list(database.list_collections(keyspace=keyspace))

# Return the list of collections and metadata associated
return [
{
def get_collection_metadata(col):
service = col.options.vector.service if col.options.vector else None
return {
"name": col.name,
"records": self.collection_data(collection_name=col.name, database=database),
"provider": (
col.options.vector.service.provider if col.options.vector and col.options.vector.service else None
),
"provider": service.provider if service else None,
"icon": "",
"model": (
col.options.vector.service.model_name if col.options.vector and col.options.vector.service else None
),
"model": service.model_name if service else None,
}
for col in collection_list
]

# Return the list of collections and their metadata
return [get_collection_metadata(col) for col in collection_list]

def reset_collection_list(self, build_config: dict):
# Get the list of options we have based on the token provided
collection_options = self._initialize_collection_options()

# If we retrieved options based on the token, show the dropdown
build_config["collection_name"]["options"] = [col["name"] for col in collection_options]
build_config["collection_name"]["options_metadata"] = [
{k: v for k, v in col.items() if k not in ["name"]} for col in collection_options
]
# Generate options and options_metadata in a single pass
options = []
options_metadata = []
for col in collection_options:
options.append(col["name"])
options_metadata.append({k: v for k, v in col.items() if k != "name"})

# Reset the selected collection
# Update build_config with collection options and reset selected collection
build_config["collection_name"]["options"] = options
build_config["collection_name"]["options_metadata"] = options_metadata
build_config["collection_name"]["value"] = ""

return build_config
Expand Down

0 comments on commit ccef0d6

Please sign in to comment.