⚡️ Speed up method S3StorageService._get_client
by 349% in PR #10170 (1.6.4-patch-s3-impl
)
#10210
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #10170
If you approve this dependent PR, these changes will be merged into the original PR branch
1.6.4-patch-s3-impl
.📄 349% (3.49x) speedup for
S3StorageService._get_client
insrc/backend/base/langflow/services/storage/s3.py
⏱️ Runtime :
3.77 milliseconds
→840 microseconds
(best of6
runs)📝 Explanation and details
The optimization implements lazy client caching in the
_get_client()
method to avoid repeated expensive client creation calls.Key changes:
if self._client is None:
check to only create the S3 client onceself._client
for reuse across subsequent callsWhy this provides a 349% speedup:
The line profiler shows that in the original code,
self.session.client("s3")
was called 1,712 times, taking 17.8ms total (10,383ns per call). In the optimized version, this expensive client creation only happens 213 times (when cache misses occur), reducing total time to 4.3ms. The remaining 1,499 calls simply return the cached client reference, which is extremely fast.Performance characteristics:
if
check)This optimization is particularly effective for workloads that make repeated S3 operations, as evidenced by the test results showing substantial time savings when
_get_client()
is called frequently within the same service instance lifecycle.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr10170-2025-10-09T22.29.12
and push.