Monorepo for Tangled tangled.org

reporesolver: compile path regexps at package init #1122

Summary#

  • Move blobPattern, treePattern, and pathAfterRefRE into package-level compiled regex variables in appview/reporesolver/resolver.go.
  • Reuse precompiled patterns in extractCurrentDir and extractPathAfterRef instead of recompiling on each call.
  • Keep path extraction behavior unchanged while reducing per-request overhead in repo path resolution.

Why#

Regexes were being compiled repeatedly during request handling. Compiling once at package initialization avoids unnecessary work in hot paths and improves efficiency with no functional change.

Test Plan#

  • Run unit tests for appview/reporesolver package.
  • Manually verify repo view routes still resolve correctly for:
    • /blob/<ref>/<path>
    • /tree/<ref>/<path>
    • /raw/<ref>/<path>
  • Confirm root and empty-path cases still return expected directories.
Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:wtmr5brxfbwmb666krk4y75r/sh.tangled.repo.pull/3mgiszoyu4d22
+9 -9
Diff #0
+9 -9
appview/reporesolver/resolver.go
··· 18 18 "tangled.org/core/rbac" 19 19 ) 20 20 21 + var ( 22 + blobPattern = regexp.MustCompile(`blob/[^/]+/(.*)$`) 23 + treePattern = regexp.MustCompile(`tree/[^/]+/(.*)$`) 24 + pathAfterRefRE = regexp.MustCompile(`(?:blob|tree|raw)/[^/]+/(.*)$`) 25 + ) 26 + 21 27 type RepoResolver struct { 22 28 config *config.Config 23 29 enforcer *rbac.Enforcer ··· 140 146 func extractCurrentDir(fullPath string) string { 141 147 fullPath = strings.TrimPrefix(fullPath, "/") 142 148 143 - blobPattern := regexp.MustCompile(`blob/[^/]+/(.*)$`) 144 149 if matches := blobPattern.FindStringSubmatch(fullPath); len(matches) > 1 { 145 150 return path.Dir(matches[1]) 146 151 } 147 152 148 - treePattern := regexp.MustCompile(`tree/[^/]+/(.*)$`) 149 153 if matches := treePattern.FindStringSubmatch(fullPath); len(matches) > 1 { 150 154 dir := strings.TrimSuffix(matches[1], "/") 151 155 if dir == "" { ··· 164 168 func extractPathAfterRef(fullPath string) string { 165 169 fullPath = strings.TrimPrefix(fullPath, "/") 166 170 167 - // match blob/, tree/, or raw/ followed by any ref and then a slash 168 - // 169 - // captures everything after the final slash 170 - pattern := `(?:blob|tree|raw)/[^/]+/(.*)$` 171 - 172 - re := regexp.MustCompile(pattern) 173 - matches := re.FindStringSubmatch(fullPath) 171 + // pathAfterRefRE matches blob/, tree/, or raw/ followed by any ref and then a slash; 172 + // it captures everything after the final slash. 173 + matches := pathAfterRefRE.FindStringSubmatch(fullPath) 174 174 175 175 if len(matches) > 1 { 176 176 return matches[1]

History

1 round 2 comments
sign up or login to add to the discussion
matias.tngl.sh submitted #0
1 commit
expand
reporesolver: compile path regexps at package init
expand 2 comments

lgtm! Thank you for the contribution!

you're welcome!

pull request successfully merged