Skip to content

Commit 9e3266a

Browse files
committed
perf(serverHandler): use slice struct for url alias
Slice is about more than 15x faster than map.
1 parent 3861b9b commit 9e3266a

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

src/serverHandler/alias.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package serverHandler
2+
3+
type alias struct {
4+
urlPath string
5+
fsPath string
6+
}
7+
8+
type aliases []*alias
9+
10+
func (aliases aliases) byUrlPath(urlPath string) (alias *alias, ok bool) {
11+
for _, alias := range aliases {
12+
if urlPath == alias.urlPath {
13+
return alias, true
14+
}
15+
}
16+
return nil, false
17+
}

src/serverHandler/archive.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ func (h *handler) visitFs(
1717
filterCallback filterCallback,
1818
archiveCallback archiveCallback,
1919
) {
20-
aliasedFsPath, hasAlias := h.aliases[rawRequestPath]
20+
alias, hasAlias := h.aliases.byUrlPath(rawRequestPath)
2121

2222
var fsPath string
2323
if hasAlias {
24-
fsPath = aliasedFsPath
24+
fsPath = alias.fsPath
2525
} else {
2626
fsPath = initFsPath
2727
}
@@ -70,7 +70,10 @@ func (h *handler) visitFs(
7070

7171
if fInfo.IsDir() {
7272
childAliases := map[string]string{}
73-
for aliasUrlPath, aliasFsPath := range h.aliases {
73+
for _, alias := range h.aliases {
74+
aliasUrlPath := alias.urlPath
75+
aliasFsPath := alias.fsPath
76+
7477
if aliasUrlPath != rawRequestPath && path.Dir(aliasUrlPath) == rawRequestPath {
7578
childAliases[aliasUrlPath] = aliasFsPath
7679
continue

src/serverHandler/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func (h *handler) auth(w http.ResponseWriter, r *http.Request) (success bool) {
77
header.Set("WWW-Authenticate", "Basic realm=\""+r.URL.Path+"\"")
88

99
username, password, hasAuthReq := r.BasicAuth()
10-
if (hasAuthReq) {
10+
if hasAuthReq {
1111
success = h.users.Auth(username, password)
1212
}
1313

src/serverHandler/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type handler struct {
1616

1717
fallbackProxies map[string]http.Handler
1818
alwaysProxies map[string]http.Handler
19-
aliases map[string]string
19+
aliases aliases
2020

2121
globalUpload bool
2222
uploadUrls []string
@@ -140,13 +140,18 @@ func NewHandler(
140140
logger *serverLog.Logger,
141141
errHandler *serverErrHandler.ErrHandler,
142142
) *handler {
143+
aliases := aliases{}
144+
for urlPath, fsPath := range p.Aliases {
145+
aliases = append(aliases, &alias{urlPath, fsPath})
146+
}
147+
143148
h := &handler{
144149
root: root,
145150
urlPrefix: urlPrefix,
146151

147152
fallbackProxies: fallbackProxies,
148153
alwaysProxies: alwaysProxies,
149-
aliases: p.Aliases,
154+
aliases: aliases,
150155

151156
globalUpload: p.GlobalUpload,
152157
uploadUrls: p.UploadUrls,

src/serverHandler/responseData.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ func readdir(file *os.File, item os.FileInfo) (subItems []os.FileInfo, errs []er
110110
func (h *handler) mergeAlias(rawRequestPath string, subItems *[]os.FileInfo) []error {
111111
errs := []error{}
112112

113-
for aliasUrlPath, aliasFsPath := range h.aliases {
113+
for _, alias := range h.aliases {
114+
aliasUrlPath := alias.urlPath
115+
aliasFsPath := alias.fsPath
116+
114117
if len(aliasUrlPath) <= len(rawRequestPath) {
115118
continue
116119
}

0 commit comments

Comments
 (0)