Skip to content

Commit 7de2dc9

Browse files
committed
perf(responseData/getPathEntries): generate path parts more efficiently
1 parent d339873 commit 7de2dc9

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/serverHandler/responseData.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,41 @@ type responseData struct {
6868
Trans *i18n.Translation
6969
}
7070

71-
func isSlash(c rune) bool {
72-
return c == '/'
73-
}
74-
7571
func getPathEntries(currDirRelPath, path string, tailSlash bool) []pathEntry {
76-
restPaths := strings.FieldsFunc(path, isSlash)
77-
paths := make([]string, len(restPaths)+1)
78-
paths[0] = "/"
79-
copy(paths[1:], restPaths)
72+
pathSegs := make([]string, 1, 12)
73+
pathSegs[0] = "/"
74+
for refPath := path[1:]; len(refPath) > 0; {
75+
slashIndex := strings.IndexByte(refPath, '/')
76+
if slashIndex < 0 {
77+
pathSegs = append(pathSegs, refPath)
78+
break
79+
} else {
80+
pathSegs = append(pathSegs, refPath[:slashIndex])
81+
refPath = refPath[slashIndex+1:]
82+
}
83+
}
8084

81-
pathFrags := len(paths)
85+
pathCount := len(pathSegs)
8286

83-
pathDepth := pathFrags
87+
pathDepth := pathCount
8488
if !tailSlash {
8589
pathDepth--
8690
}
8791

88-
pathEntries := make([]pathEntry, pathFrags)
89-
for n := 1; n <= pathFrags; n++ {
92+
pathEntries := make([]pathEntry, pathCount)
93+
for n := 1; n <= pathCount; n++ {
9094
var relPath string
9195
if n < pathDepth {
9296
relPath = strings.Repeat("../", pathDepth-n)
9397
} else if n == pathDepth {
9498
relPath = currDirRelPath
9599
} else /*if n == pathDepth+1*/ {
96-
relPath = currDirRelPath + paths[pathDepth] + "/"
100+
relPath = currDirRelPath + pathSegs[pathDepth] + "/"
97101
}
98102

99103
i := n - 1
100104
pathEntries[i] = pathEntry{
101-
Name: paths[i],
105+
Name: pathSegs[i],
102106
Path: relPath,
103107
}
104108
}

src/serverHandler/responseData_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func TestGetPathEntries(t *testing.T) {
77

88
result = getPathEntries("./", "/", true)
99
if len(result) != 1 {
10-
t.Error(len(result))
10+
t.Errorf("%#v\n", result)
1111
}
1212
if result[0].Path != "./" || result[0].Name != "/" {
1313
t.Error(result[0])

0 commit comments

Comments
 (0)