⚡️ Speed up function extract_project_and_location_from_parent by 45%
#38
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.
📄 45% (0.45x) speedup for
extract_project_and_location_from_parentingoogle/cloud/aiplatform/utils/__init__.py⏱️ Runtime :
3.40 milliseconds→2.34 milliseconds(best of208runs)📝 Explanation and details
The optimized code achieves a 45% speedup by pre-compiling the regular expression pattern instead of recompiling it on every function call.
Key optimization:
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)(/|$)"is compiled once at module load time and stored in_PROJECT_LOCATION_RE, rather than being recompiled byre.match()on every function invocation.Why this improves performance:
re.match()internally compiles the pattern string into a regex object every time it's called. By usingre.compile()once and reusing the compiled pattern, we avoid this expensive compilation step.match()method is called directly, eliminating the need forre.match()to parse and compile the pattern string.Performance benefits across test cases:
The optimization is most effective for functions called frequently with the same regex pattern, which is typical for utility functions like this one used throughout a codebase.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-extract_project_and_location_from_parent-mgkl03l9and push.