Skip to content

Commit 8409c47

Browse files
committed
perf(serverHandler): use slice struct for proxy handler
Slice is about more than 15x faster than map.
1 parent 9e3266a commit 8409c47

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/serverHandler/main.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type handler struct {
1414
root string
1515
urlPrefix string
1616

17-
fallbackProxies map[string]http.Handler
18-
alwaysProxies map[string]http.Handler
17+
fallbackProxies proxyHandlers
18+
alwaysProxies proxyHandlers
1919
aliases aliases
2020

2121
globalUpload bool
@@ -134,8 +134,8 @@ func NewHandler(
134134
urlPrefix string,
135135
p *param.Param,
136136
users user.Users,
137-
fallbackProxies map[string]http.Handler,
138-
alwaysProxies map[string]http.Handler,
137+
fallbackProxiesMap map[string]http.Handler,
138+
alwaysProxiesMap map[string]http.Handler,
139139
template *template.Template,
140140
logger *serverLog.Logger,
141141
errHandler *serverErrHandler.ErrHandler,
@@ -145,6 +145,16 @@ func NewHandler(
145145
aliases = append(aliases, &alias{urlPath, fsPath})
146146
}
147147

148+
fallbackProxies := proxyHandlers{}
149+
for urlPath, handler := range fallbackProxiesMap {
150+
fallbackProxies = append(fallbackProxies, &proxyHandler{urlPath, handler})
151+
}
152+
153+
alwaysProxies := proxyHandlers{}
154+
for urlPath, handler := range alwaysProxiesMap {
155+
alwaysProxies = append(alwaysProxies, &proxyHandler{urlPath, handler})
156+
}
157+
148158
h := &handler{
149159
root: root,
150160
urlPrefix: urlPrefix,

src/serverHandler/proxy.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import (
55
"net/http"
66
)
77

8-
func getProxyHandler(r *http.Request, proxies map[string]http.Handler) http.Handler {
8+
type proxyHandler struct {
9+
urlPath string
10+
handler http.Handler
11+
}
12+
13+
type proxyHandlers []*proxyHandler
14+
15+
func getProxyHandler(r *http.Request, proxies proxyHandlers) http.Handler {
916
if len(proxies) == 0 {
1017
return nil
1118
}
@@ -14,7 +21,10 @@ func getProxyHandler(r *http.Request, proxies map[string]http.Handler) http.Hand
1421
var proxyHandler http.Handler = nil
1522

1623
requestUrlPath := r.RequestURI
17-
for urlPath, handler := range proxies {
24+
for _, proxy := range proxies {
25+
urlPath := proxy.urlPath
26+
handler := proxy.handler
27+
1828
if len(requestUrlPath) < len(urlPath) || !util.HasUrlPrefixDir(requestUrlPath, urlPath) {
1929
continue
2030
}

0 commit comments

Comments
 (0)