From d8f624e99a6eecd45413c7fe544806c082afd396 Mon Sep 17 00:00:00 2001 From: michaelj Date: Sat, 7 Mar 2026 09:56:35 +0000 Subject: [PATCH 1/2] fix(perf): cache verified operation IDs to skip redundant preflight DB lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _perform_preflight_checks_for_sample_store_methods decorates every sample store method (measurement_requests_for_operation, complete_measurement_request_with_results_timeseries, etc.) and verifies that the supplied operation_id belongs to the space before allowing the call through. It did this by calling getResource(operation) on every decorated call — a full DB round-trip each time. For the common path for `ado show X operation` which goes through from_operation_id, this check is redundant as the operation was already fetched to locate the space, so ownership is proven by construction. The fix adds _verified_operation_ids: set[str] to DiscoverySpace.__init__ and guards the getResource call in the preflight decorator behind a set membership test. from_operation_id pre-populates the set with the operation_id immediately after the space is built, so any subsequent decorated call on that space skips the DB round-trip entirely. The full ownership check (getResource + uri comparison) is still executed for operation IDs not in the set, preserving correctness for spaces constructed via other paths (e.g. from_stored_configuration called directly) where ownership has not yet been established. Measured saving per decorated method call: ~653ms (this is saving in ado show X operation call) --- orchestrator/core/discoveryspace/space.py | 28 +++++++++++++++-------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/orchestrator/core/discoveryspace/space.py b/orchestrator/core/discoveryspace/space.py index f09b90d89..f5eba9d08 100644 --- a/orchestrator/core/discoveryspace/space.py +++ b/orchestrator/core/discoveryspace/space.py @@ -77,16 +77,22 @@ def perform_checks( ) operation_id = kwargs.get("operation_id") or args[0] - space_for_operation = self._metadataStore.getResource( - identifier=operation_id, - kind=CoreResourceKinds.OPERATION, - raise_error_if_no_resource=True, - ).config.spaces[0] - if self.uri != space_for_operation: - raise ValueError( - f"Operation {operation_id} does not belong to space {self.uri}, but rather to {space_for_operation}" - ) + # Skip the DB round-trip when we've already verified this operation + # belongs to this space (e.g. the space was built via from_operation_id). + if operation_id not in self._verified_operation_ids: + space_for_operation = self._metadataStore.getResource( + identifier=operation_id, + kind=CoreResourceKinds.OPERATION, + raise_error_if_no_resource=True, + ).config.spaces[0] + + if self.uri != space_for_operation: + raise ValueError( + f"Operation {operation_id} does not belong to space {self.uri}, but rather to {space_for_operation}" + ) + + self._verified_operation_ids.add(operation_id) return f(self, *args, **kwargs) @@ -385,6 +391,10 @@ def __init__( else f"space-{str(uuid.uuid4())[:6]}-{self._sample_store.identifier}" ) + # Operation IDs that have already passed the preflight ownership check. + # Pre-populated by from_operation_id to avoid a redundant DB round-trip. + self._verified_operation_ids: set[str] = set() + def __rich__(self) -> "RenderableType": """Rich console representation of the DiscoverySpace.""" import rich.box From 9d744156ae15d632c68e12339ba0e3ec1ac54a4e Mon Sep 17 00:00:00 2001 From: michaelj Date: Sat, 7 Mar 2026 10:16:33 +0000 Subject: [PATCH 2/2] fix(core): Missing cache addition in last commit --- orchestrator/core/discoveryspace/space.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/orchestrator/core/discoveryspace/space.py b/orchestrator/core/discoveryspace/space.py index f5eba9d08..1372ef432 100644 --- a/orchestrator/core/discoveryspace/space.py +++ b/orchestrator/core/discoveryspace/space.py @@ -297,11 +297,13 @@ def from_operation_id( raise_error_if_no_resource=True, ).config.spaces[0] - return cls.from_stored_configuration( + space = cls.from_stored_configuration( project_context=project_context, space_identifier=space_id, metadata_store=metadata_store, ) + space._verified_operation_ids.add(operation_id) + return space def __init__( self,