Skip to content

Commit 4001532

Browse files
committed
perf(serverHandler): keep sub item name's byte slice
The byte slice names can be reused for each comparation round.
1 parent 7b4119c commit 4001532

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/serverHandler/page.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseDat
1616
}
1717

1818
if needResponseBody(r.Method) {
19+
updateSubsItemHtml(data.SubItems)
1920
err := h.template.Execute(w, data)
2021
h.errHandler.LogError(err)
2122
}

src/serverHandler/responseData.go

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ type pathEntry struct {
1717
Path string
1818
}
1919

20+
type subItemSort struct {
21+
Name []byte
22+
}
23+
2024
type subItemHtml struct {
2125
IsDir bool
2226
Name template.HTML
@@ -25,8 +29,10 @@ type subItemHtml struct {
2529
}
2630

2731
type subItem struct {
32+
sort subItemSort
33+
2834
Info os.FileInfo
29-
Html subItemHtml
35+
Html *subItemHtml
3036
}
3137

3238
type responseData struct {
@@ -200,25 +206,6 @@ func getSubItemPrefix(requestPath string, tailSlash bool) (subItemPrefix string)
200206
return
201207
}
202208

203-
func sortSubInfos(subInfos []os.FileInfo) {
204-
sort.Slice(
205-
subInfos,
206-
func(prevIndex, nextIndex int) bool {
207-
prevItem := subInfos[prevIndex]
208-
nextItem := subInfos[nextIndex]
209-
210-
prevIsDir := prevItem.IsDir()
211-
nextIsDir := nextItem.IsDir()
212-
213-
if prevIsDir != nextIsDir {
214-
return prevIsDir
215-
}
216-
217-
return util.CompareNumInStr([]byte(prevItem.Name()), []byte(nextItem.Name()))
218-
},
219-
)
220-
}
221-
222209
func getItemName(info os.FileInfo, r *http.Request) (itemName string) {
223210
if info != nil {
224211
itemName = info.Name()
@@ -235,19 +222,47 @@ func getSubItems(subInfos []os.FileInfo) []*subItem {
235222
for i := 0; i < len(subInfos); i++ {
236223
info := subInfos[i]
237224
subItems[i] = &subItem{
238-
Info: info,
239-
Html: subItemHtml{
240-
IsDir: info.IsDir(),
241-
Name: tplutil.FormatFilename(info.Name()),
242-
Size: tplutil.FormatSize(info.Size()),
243-
ModTime: tplutil.FormatTime(info.ModTime()),
225+
sort: subItemSort{
226+
Name: []byte(info.Name()),
244227
},
228+
Info: info,
245229
}
246230
}
247231

248232
return subItems
249233
}
250234

235+
func updateSubsItemHtml(subItems []*subItem) {
236+
for _, item := range subItems {
237+
info := item.Info
238+
item.Html = &subItemHtml{
239+
IsDir: info.IsDir(),
240+
Name: tplutil.FormatFilename(info.Name()),
241+
Size: tplutil.FormatSize(info.Size()),
242+
ModTime: tplutil.FormatTime(info.ModTime()),
243+
}
244+
}
245+
}
246+
247+
func sortSubItems(subItems []*subItem) {
248+
sort.Slice(
249+
subItems,
250+
func(prevIndex, nextIndex int) bool {
251+
prevItem := subItems[prevIndex]
252+
nextItem := subItems[nextIndex]
253+
254+
prevIsDir := prevItem.Info.IsDir()
255+
nextIsDir := nextItem.Info.IsDir()
256+
257+
if prevIsDir != nextIsDir {
258+
return prevIsDir
259+
}
260+
261+
return util.CompareNumInStr(prevItem.sort.Name, nextItem.sort.Name)
262+
},
263+
)
264+
}
265+
251266
func (h *handler) getResponseData(r *http.Request) (data *responseData) {
252267
requestUri := r.URL.Path
253268
tailSlash := requestUri[len(requestUri)-1] == '/'
@@ -291,9 +306,9 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
291306
internalError = internalError || len(_mergeErrs) > 0
292307

293308
subInfos = h.FilterItems(subInfos)
294-
sortSubInfos(subInfos)
295309

296310
subItems := getSubItems(subInfos)
311+
sortSubItems(subItems)
297312

298313
subItemPrefix := getSubItemPrefix(reqPath, tailSlash)
299314

0 commit comments

Comments
 (0)