Skip to content

Commit 7c21557

Browse files
authored
cmd/tap: small string optimizations (#1323)
Slight speed improvements in utility functions - `matchesCollection()`: using CutSuffix instead of HasSuffix + TrimSuffix (~23% faster) - `userAgent()`: caching via package-level var (avoids Short()'s slice allocs per call)
2 parents 4bec712 + 87ddecf commit 7c21557

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

cmd/tap/util.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import (
1515
"github.com/earthboundkid/versioninfo/v2"
1616
)
1717

18+
var userAgentStr = "tap/" + versioninfo.Short()
19+
1820
func userAgent() string {
19-
return fmt.Sprintf("tap/%s", versioninfo.Short())
21+
return userAgentStr
2022
}
2123

2224
func backoff(retries int, max int) time.Duration {
@@ -38,21 +40,17 @@ func matchesCollection(collection string, filters []string) bool {
3840
}
3941

4042
for _, filter := range filters {
41-
if strings.HasSuffix(filter, "*") {
42-
prefix := strings.TrimSuffix(filter, "*")
43+
if prefix, ok := strings.CutSuffix(filter, "*"); ok {
4344
if strings.HasPrefix(collection, prefix) {
4445
return true
4546
}
46-
} else {
47-
if collection == filter {
48-
return true
49-
}
47+
} else if collection == filter {
48+
return true
5049
}
5150
}
5251

5352
return false
5453
}
55-
5654
func evtHasSignalCollection(evt *comatproto.SyncSubscribeRepos_Commit, signalColl string) bool {
5755
for _, op := range evt.Ops {
5856
collection, _, err := syntax.ParseRepoPath(op.Path)

0 commit comments

Comments
 (0)