Skip to content

Commit 157bab7

Browse files
committed
perf(serverHandler/page): evaluate context querystring only once
1 parent 51fdec7 commit 157bab7

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/serverHandler/page.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ const typeFile = template.HTML("file")
1414

1515
func updateSubItemsHtml(data *responseData) {
1616
length := len(data.SubItems)
17+
if length == 0 {
18+
return
19+
}
1720
data.SubItemsHtml = make([]itemHtml, length)
1821

22+
dirSuffix := "/" + data.Context.QueryString()
23+
fileSuffix := data.Context.FileQueryString()
24+
1925
for i, info := range data.SubItems {
2026
name := info.Name()
21-
urlEscapedName := tplUtil.FormatFileUrl(name)
2227

2328
var displayName template.HTML
2429
var typ template.HTML
@@ -28,11 +33,11 @@ func updateSubItemsHtml(data *responseData) {
2833
if info.IsDir() {
2934
displayName = tplUtil.FormatFilename(name) + "/"
3035
typ = typeDir
31-
url = data.SubItemPrefix + urlEscapedName + "/" + data.Context.QueryString()
36+
url = data.SubItemPrefix + tplUtil.FormatFileUrl(name) + dirSuffix
3237
} else {
3338
displayName = tplUtil.FormatFilename(name)
3439
typ = typeFile
35-
url = data.SubItemPrefix + urlEscapedName + data.Context.FileQueryString()
40+
url = data.SubItemPrefix + tplUtil.FormatFileUrl(name) + fileSuffix
3641
readableSize = tplUtil.FormatSize(info.Size())
3742
}
3843

src/serverHandler/pathContext.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type pathContext struct {
1010
func (ctx pathContext) QueryString() string {
1111
// ?downloadfile&sort=x/&
1212
buffer := make([]byte, 1, 22)
13-
buffer[0] = '?'
13+
buffer[0] = '?' // 1 byte
1414

1515
switch {
1616
case ctx.downloadfile:
@@ -29,16 +29,16 @@ func (ctx pathContext) QueryString() string {
2929
return string(buffer)
3030
}
3131

32+
func (ctx pathContext) QueryStringOfSort(sort string) string {
33+
copiedCtx := ctx
34+
copiedCtx.sort = &sort
35+
return copiedCtx.QueryString()
36+
}
37+
3238
func (ctx pathContext) FileQueryString() string {
3339
if ctx.downloadfile {
3440
return "?downloadfile"
3541
}
3642

3743
return ""
3844
}
39-
40-
func (ctx pathContext) QueryStringOfSort(sort string) string {
41-
copiedCtx := ctx
42-
copiedCtx.sort = &sort
43-
return copiedCtx.QueryString()
44-
}

0 commit comments

Comments
 (0)