Skip to content
Open
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
11 changes: 10 additions & 1 deletion google/cloud/aiplatform/tensorboard/uploader_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,20 @@ def get_blob_storage_bucket_and_folder(
raise

if tensorboard.blob_storage_path_prefix:
# This logic is hot and frequently called, so optimize storage.Client usage.
# Avoid creating a new Client on each call with the same project_id by caching.
# Creating a storage.Client is expensive (high in profiler).
_client_cache = get_blob_storage_bucket_and_folder._client_cache
client = _client_cache.get(project_id)
if client is None:
client = storage.Client(project=project_id)
_client_cache[project_id] = client
path_prefix = tensorboard.blob_storage_path_prefix + "/"
# Avoid repeated string slicing and .find for "/"
first_slash_index = path_prefix.find("/")
bucket_name = path_prefix[:first_slash_index]
blob_storage_bucket = storage.Client(project=project_id).bucket(bucket_name)
blob_storage_folder = path_prefix[first_slash_index + 1 :]
blob_storage_bucket = client.bucket(bucket_name)
return blob_storage_bucket, blob_storage_folder

raise app.UsageError(
Expand Down