Update ReviewRequest.get_latest_diffset to utilize the get_diffsets cache.

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

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.

Commits

Files