⚡️ Speed up function replace_api_key_with_env_var_name by 23% in PR #12129 (fix-api-key-components)#12133
Closed
codeflash-ai[bot] wants to merge 6 commits into
Closed
Conversation
Made-with: Cursor
The optimization pre-compiles the regex pattern `[A-Za-z][A-Za-z0-9_]*` at module scope instead of recompiling it on every `_looks_like_variable_name` call. Line profiler shows the original `re.fullmatch(...)` consumed 92.7% (2.1 ms) of the helper's runtime, while the optimized version using the pre-compiled pattern drops this to 51% (369 µs)—a ~5.7× reduction in per-call cost. The helper is invoked 428 times per run, so eliminating repeated pattern compilation yields a cumulative 22% speedup across the entire `replace_api_key_with_env_var_name` function. The optimized code also hoists `flow.get("data", {}).get("nodes", [])` out of the loop header to avoid redundant dict lookups, though profiler data confirms regex compilation was the dominant bottleneck.
Codecov Report❌ Patch coverage is ❌ Your project status has failed because the head coverage (41.43%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## release-1.8.1 #12133 +/- ##
==============================================
Coverage 37.18% 37.19%
==============================================
Files 1592 1592
Lines 78216 78275 +59
Branches 11865 11872 +7
==============================================
+ Hits 29087 29116 +29
- Misses 47461 47491 +30
Partials 1668 1668
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.📄 23% (0.23x) speedup for
replace_api_key_with_env_var_nameinsrc/backend/base/langflow/api/utils/core.py⏱️ Runtime :
899 microseconds→732 microseconds(best of104runs)📝 Explanation and details
The optimization pre-compiles the regex pattern
[A-Za-z][A-Za-z0-9_]*at module scope instead of recompiling it on every_looks_like_variable_namecall. Line profiler shows the originalre.fullmatch(...)consumed 92.7% (2.1 ms) of the helper's runtime, while the optimized version using the pre-compiled pattern drops this to 51% (369 µs)—a ~5.7× reduction in per-call cost. The helper is invoked 428 times per run, so eliminating repeated pattern compilation yields a cumulative 22% speedup across the entirereplace_api_key_with_env_var_namefunction. The optimized code also hoistsflow.get("data", {}).get("nodes", [])out of the loop header to avoid redundant dict lookups, though profiler data confirms regex compilation was the dominant bottleneck.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr12129-2026-03-10T17.52.35and push.