From 6b3ca47623680d77ed5b43ebac483b26dc373485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Insaurralde?= Date: Tue, 3 Mar 2026 17:25:13 -0300 Subject: [PATCH] reporesolver: compile path regexps at package init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move blobPattern, treePattern, and pathAfterRefRE to package-level vars so they are compiled once and reused across GetRepoInfo and path resolution calls instead of recompiling on every request. Signed-off-by: Matías Insaurralde --- appview/reporesolver/resolver.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/appview/reporesolver/resolver.go b/appview/reporesolver/resolver.go index 18e493b2..76c4320a 100644 --- a/appview/reporesolver/resolver.go +++ b/appview/reporesolver/resolver.go @@ -18,6 +18,12 @@ import ( "tangled.org/core/rbac" ) +var ( + blobPattern = regexp.MustCompile(`blob/[^/]+/(.*)$`) + treePattern = regexp.MustCompile(`tree/[^/]+/(.*)$`) + pathAfterRefRE = regexp.MustCompile(`(?:blob|tree|raw)/[^/]+/(.*)$`) +) + type RepoResolver struct { config *config.Config enforcer *rbac.Enforcer @@ -140,12 +146,10 @@ func (rr *RepoResolver) GetRepoInfo(r *http.Request, user *oauth.MultiAccountUse func extractCurrentDir(fullPath string) string { fullPath = strings.TrimPrefix(fullPath, "/") - blobPattern := regexp.MustCompile(`blob/[^/]+/(.*)$`) if matches := blobPattern.FindStringSubmatch(fullPath); len(matches) > 1 { return path.Dir(matches[1]) } - treePattern := regexp.MustCompile(`tree/[^/]+/(.*)$`) if matches := treePattern.FindStringSubmatch(fullPath); len(matches) > 1 { dir := strings.TrimSuffix(matches[1], "/") if dir == "" { @@ -164,13 +168,9 @@ func extractCurrentDir(fullPath string) string { func extractPathAfterRef(fullPath string) string { fullPath = strings.TrimPrefix(fullPath, "/") - // match blob/, tree/, or raw/ followed by any ref and then a slash - // - // captures everything after the final slash - pattern := `(?:blob|tree|raw)/[^/]+/(.*)$` - - re := regexp.MustCompile(pattern) - matches := re.FindStringSubmatch(fullPath) + // pathAfterRefRE matches blob/, tree/, or raw/ followed by any ref and then a slash; + // it captures everything after the final slash. + matches := pathAfterRefRE.FindStringSubmatch(fullPath) if len(matches) > 1 { return matches[1] -- 2.51.2