From b07d141f4ce40f939d18661d4860911a50e7e3d6 Mon Sep 17 00:00:00 2001 From: Lewis Date: Mon, 29 Jun 2026 20:28:29 +0300 Subject: [PATCH] appview/pulls: cache combined diffs in 15m lru Lewis: May this revision serve well! --- appview/pulls/pulls.go | 10 ++++++++++ appview/pulls/single.go | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/appview/pulls/pulls.go b/appview/pulls/pulls.go index 99764cdb..beb3cadb 100644 --- a/appview/pulls/pulls.go +++ b/appview/pulls/pulls.go @@ -7,6 +7,7 @@ import ( "io" "log/slog" "strings" + "time" "tangled.org/core/appview/config" "tangled.org/core/appview/db" @@ -21,12 +22,19 @@ import ( "tangled.org/core/idresolver" "tangled.org/core/ogre" "tangled.org/core/patchutil" + "tangled.org/core/types" + "github.com/hashicorp/golang-lru/v2/expirable" indigoxrpc "github.com/bluesky-social/indigo/xrpc" ) const ApplicationGzip = "application/gzip" +const ( + diffCacheSize = 128 + diffCacheTTL = 15 * time.Minute +) + type Pulls struct { oauth *oauth.OAuth repoResolver *reporesolver.RepoResolver @@ -40,6 +48,7 @@ type Pulls struct { logger *slog.Logger indexer *pulls_indexer.Indexer ogreClient *ogre.Client + diffCache *expirable.LRU[string, types.DiffRenderer] } func New( @@ -68,6 +77,7 @@ func New( logger: logger, indexer: indexer, ogreClient: ogre.NewClient(config.Ogre.Host), + diffCache: expirable.NewLRU[string, types.DiffRenderer](diffCacheSize, nil, diffCacheTTL), } } diff --git a/appview/pulls/single.go b/appview/pulls/single.go index c166f637..5280ed0f 100644 --- a/appview/pulls/single.go +++ b/appview/pulls/single.go @@ -226,10 +226,7 @@ func (s *Pulls) repoPullHelper(w http.ResponseWriter, r *http.Request, interdiff vouchSkips[ownerDid] = skipped } - patch := pull.Submissions[roundIdInt].CombinedPatch() var diff types.DiffRenderer - diff = patchutil.AsNiceDiff(patch, pull.TargetBranch) - if interdiff { currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].CombinedPatch()) if err != nil { @@ -246,6 +243,8 @@ func (s *Pulls) repoPullHelper(w http.ResponseWriter, r *http.Request, interdiff } diff = patchutil.Interdiff(previousPatch, currentPatch) + } else { + diff = s.combinedDiff(pull, roundIdInt) } err = s.pages.RepoSinglePull(w, pages.RepoSinglePullParams{ @@ -275,6 +274,18 @@ func (s *Pulls) repoPullHelper(w http.ResponseWriter, r *http.Request, interdiff } } +func (s *Pulls) combinedDiff(pull *models.Pull, round int) types.DiffRenderer { + submission := pull.Submissions[round] + key := fmt.Sprintf("%s|%d|%s", pull.AtUri(), round, submission.SourceRev) + if cached, ok := s.diffCache.Get(key); ok { + return cached + } + + diff := patchutil.AsNiceDiff(submission.CombinedPatch(), pull.TargetBranch) + s.diffCache.Add(key, diff) + return diff +} + func (s *Pulls) RepoSinglePull(w http.ResponseWriter, r *http.Request) { l := s.logger.With("handler", "RepoSinglePull") -- 2.51.2