Set limits for Projects::CompareController#show

What does this MR do and why?

The issue: https://gitlab.com/gitlab-org/gitlab/-/issues/550847+

This MR optimizes the compare page performance by preventing unnecessary pipeline queries when there are too many commits to display pipeline status.

Problem

Currently, the compare page always executes expensive pipeline queries (with_latest_pipeline) regardless of whether the pipeline status will be displayed in the UI. When there are many commits (above MergeRequestDiff::COMMITS_SAFE_SIZE), GitLab hides the pipeline status indicators but still runs the database queries to fetch pipeline data, resulting in wasted resources.

with few commits with many commits
Screenshot_2025-08-15_at_12.26.20_am Screenshot_2025-08-15_at_12.21.05_am

Solution

Add a conditional check to only fetch pipeline information when the number of commits is below the safe display limit. This prevents expensive pipeline queries when the results won't be shown to users, improving page load performance for comparisons with many commits.

How to test

  1. if the project has no .gitlab-ci.yml file, you should create a new one, for example

    test:
      script:
      - echo hello world
  2. create a new brach with a few commits

  3. you can adjust MergeRequestDiff::Commits_safe_size for testing

  4. with the total number of commits < threshold: check your performance bar. You should see sql queries for p_ci_pipelines

  5. with the total number of commits > threshold: check your performance bar. There shouldn't be sql queries for p_ci_pipelines

Screenshots or screen recordings

note: I adjusted MergeRequestDiff::Commits_safe_size = 1 for the many commits case.

the compare page the performance bar sql queries

3 commits < commits_safe_size(100)

Screenshot 2025-09-01 at 11.59.33 pm.png

Screenshot 2025-09-01 at 11.54.10 pm.png

3 commits >

commits_safe_size(1)

Screenshot 2025-09-01 at 11.57.58 pm.png

Screenshot 2025-09-01 at 11.57.40 pm.png

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Emma Park

Merge request reports

Loading