Skip to content

Commit 580f35a

Browse files
committed
net/textproto: eliminate some bounds checks
This change lifts bounds checks out of loops in the trim function. Here are some benchmark results (no change to allocations): goos: darwin goarch: amd64 pkg: net/textproto cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz │ old │ new │ │ sec/op │ sec/op vs base │ ReadMIMEHeader/client_headers-8 3.531µ ± 3% 3.506µ ± 1% ~ (p=0.068 n=20) ReadMIMEHeader/server_headers-8 2.409µ ± 0% 2.384µ ± 1% -1.02% (p=0.000 n=20) Uncommon-8 545.2n ± 1% 536.7n ± 0% -1.58% (p=0.000 n=20) geomean 1.667µ 1.649µ -1.10%
1 parent d86ec92 commit 580f35a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/net/textproto/reader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ func trim(s []byte) []byte {
110110
for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
111111
i++
112112
}
113-
n := len(s)
114-
for n > i && (s[n-1] == ' ' || s[n-1] == '\t') {
113+
s = s[i:]
114+
n := len(s) - 1
115+
for n >= 0 && (s[n] == ' ' || s[n] == '\t') {
115116
n--
116117
}
117-
return s[i:n]
118+
return s[:n+1]
118119
}
119120

120121
// ReadContinuedLineBytes is like [Reader.ReadContinuedLine] but

0 commit comments

Comments
 (0)