⚡️ Speed up function create_uri_from_resource_name
by 49%
#27
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.
📄 49% (0.49x) speedup for
create_uri_from_resource_name
ingoogle/cloud/aiplatform/metadata/schema/utils.py
⏱️ Runtime :
5.52 milliseconds
→3.71 milliseconds
(best of177
runs)📝 Explanation and details
The optimized code achieves a 48% speedup by precompiling the regex pattern at module load time instead of recompiling it on every function call.
What changed:
_RESOURCE_NAME_REGEX
re.match()
call to use the precompiled pattern object instead of the raw string patternWhy this is faster:
In Python's
re
module, callingre.match(pattern_string, text)
internally compiles the pattern on every call. The line profiler shows the original code spent 67.9% of its time in there.match()
call. By precompiling the pattern once at import time, each function call only needs to execute the already-compiled pattern against the input string, eliminating the compilation overhead.Performance characteristics:
This is a classic performance optimization for frequently-called functions that use regex patterns.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-create_uri_from_resource_name-mgkcwlaq
and push.