⚡️ Speed up function get_uuid by 146% in PR #11344 (feat/flow-history)
#11345
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 #11344
If you approve this dependent PR, these changes will be merged into the original PR branch
feat/flow-history.📄 146% (1.46x) speedup for
get_uuidinsrc/backend/base/langflow/helpers/utils.py⏱️ Runtime :
3.03 milliseconds→1.23 milliseconds(best of124runs)📝 Explanation and details
The optimization introduces memoization via
@lru_cache(maxsize=1024)for UUID string parsing. Here's why this achieves a 145% speedup:Key Optimization
What changed: The UUID string-to-object conversion is now wrapped in a cached helper function
_uuid_from_str(). When the same UUID string is parsed multiple times, subsequent calls return the cached UUID object instead of re-parsing.Why it's faster: The
UUID()constructor performs string validation and parsing on every call (~4.8μs per hit in the original). With caching, duplicate string inputs hit the LRU cache (~2.5μs per hit in the optimized version), nearly halving the per-call overhead when cache hits occur.Performance Characteristics
Best case scenarios (evident from test results):
test_large_scale_repeated_same_uuid: Converting the same UUID string 500 times sees maximum benefit as all calls after the first are cache hitstest_edge_duplicate_uuid_comparison: Repeated conversions of identical stringstest_large_scale_alternating_string_and_object: When string inputs repeat in patternsMarginal benefit scenarios:
test_large_scale_many_distinct_uuids: 500 unique UUID strings see minimal caching benefit (mostly cache misses)Memory vs. Speed Tradeoff
The
maxsize=1024bounds memory growth to ~1024 cached UUID objects (approximately 128KB overhead), preventing unbounded memory consumption while providing substantial speedup for typical workloads where UUID strings are reused (e.g., database IDs, request identifiers, entity references).Behavioral Preservation
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11344-2026-01-18T04.40.48and push.