Skip to content

Commit 7fb20f6

Browse files
committed
perf(serverHandler/log): allocate memory space more accurately
1 parent 89b78c6 commit 7fb20f6

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/serverHandler/log.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,38 @@ import (
44
"net/http"
55
)
66

7-
const LOG_BUF_SIZE = 80
8-
97
func (h *handler) logRequest(r *http.Request) {
108
if !h.logger.CanLogAccess() {
119
return
1210
}
1311

14-
payload := []byte(r.RemoteAddr + " " + r.Method + " " + r.RequestURI)
12+
buf := make([]byte, 0, 2+len(r.RemoteAddr)+len(r.Method)+len(r.RequestURI))
13+
14+
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
15+
buf = append(buf, ' ') // 1 byte
16+
buf = append(buf, []byte(r.Method)...) // ~ 3-4 bytes
17+
buf = append(buf, ' ') // 1 byte
18+
buf = append(buf, []byte(r.RequestURI)...)
1519

16-
h.logger.LogAccess(payload)
20+
h.logger.LogAccess(buf)
1721
}
1822

1923
func (h *handler) logMutate(username, action, detail string, r *http.Request) {
2024
if !h.logger.CanLogAccess() {
2125
return
2226
}
2327

24-
buf := make([]byte, 0, LOG_BUF_SIZE)
28+
buf := make([]byte, 0, 6+len(r.RemoteAddr)+len(username)+len(action)+len(detail))
2529

26-
buf = append(buf, []byte(r.RemoteAddr)...) // 9-47 bytes, mainly 21 bytes
30+
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
2731
if len(username) > 0 {
28-
buf = append(buf, []byte(" (")...) // 2 bytes
32+
buf = append(buf, ' ', '(') // 2 bytes
2933
buf = append(buf, []byte(username)...)
3034
buf = append(buf, ')') // 1 byte
3135
}
3236
buf = append(buf, ' ') // 1 byte
33-
buf = append(buf, []byte(action)...) // 5-6 bytes
34-
buf = append(buf, []byte(": ")...) // 2 bytes
37+
buf = append(buf, []byte(action)...) // ~ 5-6 bytes
38+
buf = append(buf, ':', ' ') // 2 bytes
3539
buf = append(buf, []byte(detail)...)
3640

3741
h.logger.LogAccess(buf)
@@ -42,11 +46,11 @@ func (h *handler) logUpload(username, filename, fsPath string, r *http.Request)
4246
return
4347
}
4448

45-
buf := make([]byte, 0, LOG_BUF_SIZE)
49+
buf := make([]byte, 0, 16+len(r.RemoteAddr)+len(username)+len(filename)+len(fsPath))
4650

47-
buf = append(buf, []byte(r.RemoteAddr)...) // 9-47 bytes, mainly 21 bytes
51+
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
4852
if len(username) > 0 {
49-
buf = append(buf, []byte(" (")...) // 2 bytes
53+
buf = append(buf, ' ', '(') // 2 bytes
5054
buf = append(buf, []byte(username)...)
5155
buf = append(buf, ')') // 1 byte
5256
}
@@ -63,9 +67,9 @@ func (h *handler) logArchive(filename, relPath string, r *http.Request) {
6367
return
6468
}
6569

66-
buf := make([]byte, 0, LOG_BUF_SIZE)
70+
buf := make([]byte, 0, 19+len(r.RemoteAddr)+len(filename)+len(relPath))
6771

68-
buf = append(buf, []byte(r.RemoteAddr)...) // 9-47 bytes, mainly 21 bytes
72+
buf = append(buf, []byte(r.RemoteAddr)...) // ~ 9-47 bytes, mainly 21 bytes
6973
buf = append(buf, []byte(" archive file: ")...) // 15 bytes
7074
buf = append(buf, []byte(filename)...)
7175
buf = append(buf, []byte(" <- ")...) // 4 bytes

0 commit comments

Comments
 (0)