• 
      

    Update ReviewRequest.get_latest_diffset to utilize the get_diffsets cache.

    Review Request #14327 — Created Feb. 4, 2025 and submitted

    Information

    Review Board
    release-7.1.x

    Reviewers

    We had many places in the review request page code that called
    ReviewRequest.get_latest_diffset(). Every time this was called, we
    ended up performing a query, resulting in potentially dozens of these
    for long review requests.

    We have existing caching for ReviewRequest.get_diffsets(), which
    gives the same results back for every call without additional database
    queries until the local cache is reset. Since this is likely to be
    called on the same page that calls get_latest_diffset(), there's value
    in sharing those caches, which this change does.

    The one thing that complicates that is that get_diffsets() also
    pre-fetches files. This is a difference in behavior from
    get_latest_diffset(), and while that can be valuable, it's in
    practice an extra query across all diffsets that we don't always need.

    As a solution to that, get_diffsets() now takes a with_filediffs=
    argument, which get_latest_diffset() sets to False. This is designed
    to be smart about its caching. If one call passes False and the next
    passes True, then the first call will fetch the DiffSets and the
    next will just fetch the FileDiffs using prefetch_related_objects().
    If passing False when True was previously passed, it will just
    simply get DiffSets with filediffs.

    get_latest_diffet() does manage its own cache, but based on
    get_diffsets(), optimistically avoiding a query. Detection of the
    latest DiffSet now happens client-side, sorting and picking the most
    recent.

    All unit tests passed.

    Verified this reduced the query counts on the review request page.

    Summary ID
    Update ReviewRequest.get_latest_diffset to utilize the get_diffsets cache.
    We had many places in the review request page code that called `ReviewRequest.get_latest_diffset()`. Every time this was called, we ended up performing a query, resulting in potentially dozens of these for long review requests. We have existing caching for `ReviewRequest.get_diffsets()`, which gives the same results back for every call without additional database queries until the local cache is reset. Since this is likely to be called on the same page that calls `get_latest_diffset()`, there's value in sharing those caches, which this change does. The one thing that complicates that is that `get_diffsets()` also pre-fetches files. This is a difference in behavior from `get_latest_diffset()`, and while that can be valuable, it's in practice an extra query across all diffsets that we don't always need. As a solution to that, `get_diffsets()` now takes a `with_filediffs=` argument, which `get_latest_diffset()` sets to `False`. This is designed to be smart about its caching. If one call passes `False` and the next passes `True`, then the first call will fetch the `DiffSet`s and the next will just fetch the `FileDiff`s using `prefetch_related_objects()`. If passing `False` when `True` was previously passed, it will just simply get `DiffSet`s with filediffs. `get_latest_diffet()` does manage its own cache, but based on `get_diffsets()`, optimistically avoiding a query. Detection of the latest `DiffSet` now happens client-side, sorting and picking the most recent.
    5bd22da96908e5357430d310bc1fd66bf0718dd2
    Description From Last Updated

    Needs a version added here and below.

    maubinmaubin
    david
    1. Ship It!
    2. 
        
    maubin
    1. 
        
    2. Show all issues

      Needs a version added here and below.

      1. The _diffsets one existed before, just wasn't documented. Fixing _latest_diffset.

    3. 
        
    chipx86
    Review request changed
    Status:
    Completed
    Change Summary:
    Pushed to release-7.1.x (8883cc4)