Update ReviewRequest.get_latest_diffset to utilize the get_diffsets cache.
Review Request #14327 — Created Feb. 4, 2025 and updated
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 callsget_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 awith_filediffs=
argument, whichget_latest_diffset()
sets toFalse
. This is designed
to be smart about its caching. If one call passesFalse
and the next
passesTrue
, then the first call will fetch theDiffSet
s and the
next will just fetch theFileDiff
s usingprefetch_related_objects()
.
If passingFalse
whenTrue
was previously passed, it will just
simply getDiffSet
s with filediffs.
get_latest_diffet()
does manage its own cache, but based on
get_diffsets()
, optimistically avoiding a query. Detection of the
latestDiffSet
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 |
---|---|
5bd22da96908e5357430d310bc1fd66bf0718dd2 |
Description | From | Last Updated |
---|---|---|
Needs a version added here and below. |
![]() |