⚡️ Speed up function remove_api_keys by 11% in PR #11101 (fix-limiting-saving-flow)
#11204
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 #11101
If you approve this dependent PR, these changes will be merged into the original PR branch
fix-limiting-saving-flow.📄 11% (0.11x) speedup for
remove_api_keysinsrc/backend/base/langflow/api/utils/core.py⏱️ Runtime :
863 microseconds→775 microseconds(best of87runs)📝 Explanation and details
The optimization achieves an 11% speedup by reordering condition checks to minimize expensive operations. The key insight is that in the original code, the line profiler shows 79.2% of execution time (20.09ms) is spent on the combined condition check that includes
isinstance(),has_api_terms(), andvalue.get("password").What changed:
The optimized version restructures the conditional logic by:
isinstance(value, dict)first (cheapest operation)value.get("password")ashas_passwordbefore callinghas_api_terms()has_api_terms()when both type and password checks passWhy it's faster:
The optimization exploits Python's short-circuit evaluation more effectively:
has_api_terms()calls: The profiler shows calls dropped from 4,523 to 2,718 (40% reduction), saving ~0.94ms in string operationsvalue.get("password")even whenhas_api_terms()returnsFalse, while the optimized version caches it oncehas_passwordbefore the expensive string operations, the CPU can more efficiently predict and pipeline the executionImpact on workloads:
This optimization is particularly effective for flows with:
password=True(as shown intest_large_scale_no_password_fields)The test results confirm the optimization works best with flows containing many non-password fields or fields that fail the password check, as these scenarios maximize the benefit of avoiding the expensive
has_api_terms()string matching.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11101-2026-01-05T22.56.04and push.