Skip to content

chore(preprod): Optimize the status check task#107169

Merged
NicoHinderling merged 2 commits intomasterfrom
ensure-status-check-task-fully-optimized
Jan 29, 2026
Merged

chore(preprod): Optimize the status check task#107169
NicoHinderling merged 2 commits intomasterfrom
ensure-status-check-task-fully-optimized

Conversation

@NicoHinderling
Copy link
Contributor

@NicoHinderling NicoHinderling commented Jan 28, 2026

Performance Optimizations

  • Added select_related for commit_comparison, mobile_app_info on initial artifact fetch
  • Added select_related for preprod_artifact on size metrics fetch
  • Eliminated N+1 queries in template formatting by pre-fetching and passing base artifact/metrics data
  • Single-pass dict grouping for base metrics by artifact ID

Result: ~N+1 queries → ~7 constant queries (artifact, size metrics, rules, approvals, base commit comparison, base artifacts, base size metrics)

@NicoHinderling NicoHinderling marked this pull request as ready for review January 28, 2026 18:06
@NicoHinderling NicoHinderling requested a review from a team as a code owner January 28, 2026 18:06
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 28, 2026
Copy link
Member

@trevor-e trevor-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect most of the improvement will come from fixing the N+1 queries in 1 + 2. 3) and 4) theoretically help but our N is so small that I'm not so sure, at least relative to the tradeoff of how much more verbose the code has become. We might want to split this file up in the near future.

def _fetch_base_size_metrics(
artifacts: list[PreprodArtifact],
) -> dict[int, PreprodArtifactSizeMetrics]:
"""Fetch base artifact size metrics for head artifacts."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused why we need two maps here? Couldn't this be something like dict[int, tuple[PreprodArtifact, PreprodArtifactSizeMetrics]] so that one lookup gives both things?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although after removing metrics_artifact_type that might need to also be a list[PreprodArtifactSizeMetrics]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One artifact can have multiple size metrics (MAIN_ARTIFACT, WATCH_APP, etc. with different identifiers). So the relationship is:
1 head artifact → 1 base artifact
1 head artifact → N base metrics
If you combined as dict[int, tuple[PreprodArtifact, list[PreprodArtifactSizeMetrics]]], the filtering would change from O(1) lookup to O(n) filtering

I agree with you though that the N is so small in this file...

version_string = _format_version_string(artifact, default=str(_("Unknown")))

base_artifact = None
base_artifact = base_artifact_map.get(artifact.id) if base_artifact_map else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: seems like base_artifact_map and base_size_metrics_map should be required params, not optional

# Build lookup dict once - O(m)
# Newest base artifact wins due to -date_added ordering, so first match per key wins
base_artifacts = list(base_artifacts_qs)
base_by_key: dict[tuple[str | None, int | None, int | None], PreprodArtifact] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I had explicitly combined this into a single loop since its easier to read IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, i rolled this back

Copy link
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 4 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@NicoHinderling NicoHinderling merged commit fed4846 into master Jan 29, 2026
70 checks passed
@NicoHinderling NicoHinderling deleted the ensure-status-check-task-fully-optimized branch January 29, 2026 16:49
priscilawebdev pushed a commit that referenced this pull request Feb 2, 2026
Performance Optimizations
- Added `select_related` for `commit_comparison`, `mobile_app_info` on
initial artifact fetch
- Added `select_related` for `preprod_artifact` on size metrics fetch
- Eliminated N+1 queries in template formatting by pre-fetching and
passing base artifact/metrics data
- Single-pass dict grouping for base metrics by artifact ID

Result: ~N+1 queries → ~7 constant queries (artifact, size metrics,
rules, approvals, base commit comparison, base artifacts, base size
metrics)
@github-actions github-actions bot locked and limited conversation to collaborators Feb 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants