⚡️ Speed up function replace_api_key_with_env_var_name by 59% in PR #12129 (fix-api-key-components)#12138
Closed
codeflash-ai[bot] wants to merge 7 commits into
Closed
Conversation
Made-with: Cursor
The hot function `_looks_like_variable_name` originally called `re.fullmatch` inside a tight loop (2764 hits), recompiling the pattern string on each call—this consumed 89.8% of its runtime (~8.15 ms of 9.07 ms total). The optimization precompiles the regex to a module-level constant `_VARIABLE_NAME_PATTERN` and replaces `fullmatch` with `match` on the stripped string, cutting per-call cost from ~2947 ns to ~684 ns. Early-exit checks for non-string types, empty values, and whitespace-only strings now proceed sequentially before the regex match, avoiding redundant `strip()` evaluations and boosting cache locality. This yields a 59% overall speedup (2.98 ms → 1.87 ms) with identical test results across all 1000+ node stress tests.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## release-1.8.1 #12138 +/- ##
================================================
Coverage ? 37.18%
================================================
Files ? 1592
Lines ? 78276
Branches ? 11872
================================================
Hits ? 29110
Misses ? 47499
Partials ? 1667
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #12129
If you approve this dependent PR, these changes will be merged into the original PR branch
fix-api-key-components.📄 59% (0.59x) speedup for
replace_api_key_with_env_var_nameinsrc/backend/base/langflow/api/utils/core.py⏱️ Runtime :
2.98 milliseconds→1.87 milliseconds(best of131runs)📝 Explanation and details
The hot function
_looks_like_variable_nameoriginally calledre.fullmatchinside a tight loop (2764 hits), recompiling the pattern string on each call—this consumed 89.8% of its runtime (~8.15 ms of 9.07 ms total). The optimization precompiles the regex to a module-level constant_VARIABLE_NAME_PATTERNand replacesfullmatchwithmatchon the stripped string, cutting per-call cost from ~2947 ns to ~684 ns. Early-exit checks for non-string types, empty values, and whitespace-only strings now proceed sequentially before the regex match, avoiding redundantstrip()evaluations and boosting cache locality. This yields a 59% overall speedup (2.98 ms → 1.87 ms) with identical test results across all 1000+ node stress tests.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr12129-2026-03-10T19.02.48and push.