⚡️ Speed up method _Resource.get_or_create
by 6%
#23
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.
📄 6% (0.06x) speedup for
_Resource.get_or_create
ingoogle/cloud/aiplatform/metadata/resource.py
⏱️ Runtime :
337 milliseconds
→317 milliseconds
(best of15
runs)📝 Explanation and details
The optimized code achieves a 6% speedup through three key micro-optimizations:
1. Dictionary Pre-binding in
__init__
: The original code constructs theparent_resource_name_fields
dictionary inline within theutils.full_resource_name()
call. The optimized version pre-constructs this dictionary in a separate variable, reducing overhead during the function call.2. Method Lookup Caching: Instead of calling
getattr(self.api_client, self._getter_method)
directly within the function call, the optimized version stores the result ingetter_method
variable first. This avoids redundant attribute lookups during the API call.3. Explicit None Comparisons: The condition
if not resource:
was changed toif resource is None:
inget_or_create()
, andif resource:
becameif resource is not None:
in__init__
. This provides slightly faster boolean evaluation by avoiding the__bool__
method call on the resource object.These optimizations primarily benefit scenarios with frequent resource initialization, particularly when resources don't exist and need to be created (as seen in the test cases). The improvements are most noticeable in high-frequency usage patterns where the small per-call reductions accumulate meaningfully. The line profiler shows the main performance bottleneck remains the API calls themselves (~93% of time), but these micro-optimizations reduce the Python-level overhead around those calls.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/aiplatform/test_metadata_resources.py::TestArtifact.test_get_or_create_artifact
unit/aiplatform/test_metadata_resources.py::TestContext.test_add_artifacts_and_executions
unit/aiplatform/test_metadata_resources.py::TestContext.test_add_artifacts_only
unit/aiplatform/test_metadata_resources.py::TestContext.test_add_executions_only
unit/aiplatform/test_metadata_resources.py::TestContext.test_get_or_create_context
unit/aiplatform/test_metadata_resources.py::TestExecution.test_add_artifact
unit/aiplatform/test_metadata_resources.py::TestExecution.test_add_vertex_model
unit/aiplatform/test_metadata_resources.py::TestExecution.test_add_vertex_model_not_resolved
unit/aiplatform/test_metadata_resources.py::TestExecution.test_get_or_create_execution
unit/aiplatform/test_metadata_resources.py::TestExecution.test_query_input_and_output_artifacts
To edit these changes
git checkout codeflash/optimize-_Resource.get_or_create-mgipu9p1
and push.