]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: make copyValues append instead of calling Add
authorsmasher164 <aindurti@gmail.com>
Wed, 21 Aug 2019 03:11:22 +0000 (23:11 -0400)
committerEmmanuel Odeke <emm.odeke@gmail.com>
Sat, 7 Sep 2019 22:21:30 +0000 (22:21 +0000)
This results in a performance boost:

name          old time/op    new time/op    delta
CopyValues-4    3.46µs ± 3%    1.53µs ± 3%  -55.85%  (p=0.000 n=18+19)

name          old alloc/op   new alloc/op   delta
CopyValues-4    1.52kB ± 0%    0.74kB ± 0%  -51.58%  (p=0.000 n=20+20)

name          old allocs/op  new allocs/op  delta
CopyValues-4      24.0 ± 0%      11.0 ± 0%  -54.17%  (p=0.000 n=20+20)

Fixes #33744.

Change-Id: Ibc653fb076a9a6aaa775fcc9ca720fb90e68cf96
Reviewed-on: https://go-review.googlesource.com/c/go/+/191057
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
src/net/http/http_test.go
src/net/http/request.go

index 8f466bb36686a7a173cdf29d90381392ecdfdc5d..224b46c7963e2605a8ff8f4015361dfa17d1ab3b 100644 (file)
@@ -9,6 +9,7 @@ package http
 import (
        "bytes"
        "internal/testenv"
+       "net/url"
        "os/exec"
        "reflect"
        "testing"
@@ -109,3 +110,28 @@ func TestCmdGoNoHTTPServer(t *testing.T) {
                }
        }
 }
+
+var valuesCount int
+
+func BenchmarkCopyValues(b *testing.B) {
+       b.ReportAllocs()
+       src := url.Values{
+               "a": {"1", "2", "3", "4", "5"},
+               "b": {"2", "2", "3", "4", "5"},
+               "c": {"3", "2", "3", "4", "5"},
+               "d": {"4", "2", "3", "4", "5"},
+               "e": {"1", "1", "2", "3", "4", "5", "6", "7", "abcdef", "l", "a", "b", "c", "d", "z"},
+               "j": {"1", "2"},
+               "m": nil,
+       }
+       for i := 0; i < b.N; i++ {
+               dst := url.Values{"a": {"b"}, "b": {"2"}, "c": {"3"}, "d": {"4"}, "j": nil, "m": {"x"}}
+               copyValues(dst, src)
+               if valuesCount = len(dst["a"]); valuesCount != 6 {
+                       b.Fatalf(`%d items in dst["a"] but expected 6`, valuesCount)
+               }
+       }
+       if valuesCount == 0 {
+               b.Fatal("Benchmark wasn't run")
+       }
+}
index 6e113f1607c32de5b1201f84b1815121c4f2c853..0b195a89a67880d0c0b3581b7f6be227ead21f54 100644 (file)
@@ -1165,9 +1165,7 @@ func (l *maxBytesReader) Close() error {
 
 func copyValues(dst, src url.Values) {
        for k, vs := range src {
-               for _, value := range vs {
-                       dst.Add(k, value)
-               }
+               dst[k] = append(dst[k], vs...)
        }
 }