]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: reduce calls to append in hexEscapeNonASCII to gain a slight performance...
authorAndy Pan <panjf2000@gmail.com>
Wed, 24 Aug 2022 15:02:16 +0000 (23:02 +0800)
committerGopher Robot <gobot@golang.org>
Mon, 27 Feb 2023 22:37:40 +0000 (22:37 +0000)
goos: linux
goarch: amd64
pkg: net/http
cpu: DO-Premium-Intel
                    │     old      │                 new                 │
                    │    sec/op    │   sec/op     vs base                │
HexEscapeNonASCII-4   469.6n ± 20%   371.1n ± 9%  -20.98% (p=0.000 n=10)

                    │    old     │              new               │
                    │    B/op    │    B/op     vs base            │
HexEscapeNonASCII-4   192.0 ± 0%   192.0 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

                    │    old     │              new               │
                    │ allocs/op  │ allocs/op   vs base            │
HexEscapeNonASCII-4   2.000 ± 0%   2.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

Change-Id: Ic8d2b3ddcf2cf724dec3f51a2aba205f2c6e4fe6
Reviewed-on: https://go-review.googlesource.com/c/go/+/425786
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/net/http/http.go
src/net/http/http_test.go

index 101799f574f4655610c2ec49d013b66c76b7a434..9b81654fcc5886b6d1b65bf82a12354e5d959c8b 100644 (file)
@@ -86,14 +86,20 @@ func hexEscapeNonASCII(s string) string {
                return s
        }
        b := make([]byte, 0, newLen)
+       var pos int
        for i := 0; i < len(s); i++ {
                if s[i] >= utf8.RuneSelf {
+                       if pos < i {
+                               b = append(b, s[pos:i]...)
+                       }
                        b = append(b, '%')
                        b = strconv.AppendInt(b, int64(s[i]), 16)
-               } else {
-                       b = append(b, s[i])
+                       pos = i + 1
                }
        }
+       if pos < len(s) {
+               b = append(b, s[pos:]...)
+       }
        return string(b)
 }
 
index 0d92fe5f964fb704dc4fa3ef1c1c2aae6d1ecd12..1c9fb33b69d145f16272a1a84efcc8287b6719ef 100644 (file)
@@ -218,3 +218,13 @@ func TestNoUnicodeStrings(t *testing.T) {
                t.Fatal(err)
        }
 }
+
+const redirectURL = "/thisaredirect细雪withasciilettersのけぶabcdefghijk.html"
+
+func BenchmarkHexEscapeNonASCII(b *testing.B) {
+       b.ReportAllocs()
+
+       for i := 0; i < b.N; i++ {
+               hexEscapeNonASCII(redirectURL)
+       }
+}