internal/encoding/json: improve allocation of Value for JSON strings
Substitute interface Value.value field with per type field boo and str
instead.
name old time/op new time/op delta
String-4 286ns ± 0% 254ns ± 0% ~ (p=1.000 n=1+1)
Bool-4 209ns ± 0% 211ns ± 0% ~ (p=1.000 n=1+1)
name old alloc/op new alloc/op delta
String-4 192B ± 0% 176B ± 0% ~ (p=1.000 n=1+1)
Bool-4 0.00B 0.00B ~ (all equal)
name old allocs/op new allocs/op delta
String-4 4.00 ± 0% 3.00 ± 0% ~ (p=1.000 n=1+1)
Bool-4 0.00 0.00 ~ (all equal)
Change-Id: Ib0167d22e60d63c221c303b79c75b9e96d432fe7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/170277
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/encoding/json/bench_test.go b/internal/encoding/json/bench_test.go
index 4db8af9..6c3f1d1 100644
--- a/internal/encoding/json/bench_test.go
+++ b/internal/encoding/json/bench_test.go
@@ -11,7 +11,7 @@
)
func BenchmarkFloat(b *testing.B) {
- input := []byte("1.797693134862315708145274237317043567981e+308")
+ input := []byte(`1.797693134862315708145274237317043567981e+308`)
for i := 0; i < b.N; i++ {
dec := json.NewDecoder(input)
val, err := dec.Read()
@@ -26,7 +26,7 @@
}
func BenchmarkInt(b *testing.B) {
- input := []byte("922337203.6854775807e+10")
+ input := []byte(`922337203.6854775807e+10`)
for i := 0; i < b.N; i++ {
dec := json.NewDecoder(input)
val, err := dec.Read()
@@ -39,3 +39,30 @@
}
}
}
+
+func BenchmarkString(b *testing.B) {
+ input := []byte(`"abcdefghijklmnopqrstuvwxyz0123456789\\n\\t"`)
+ for i := 0; i < b.N; i++ {
+ dec := json.NewDecoder(input)
+ val, err := dec.Read()
+ if err != nil {
+ b.Fatal(err)
+ }
+ _ = val.String()
+ }
+}
+
+func BenchmarkBool(b *testing.B) {
+ input := []byte(`true`)
+ for i := 0; i < b.N; i++ {
+ dec := json.NewDecoder(input)
+ val, err := dec.Read()
+ if err != nil {
+ b.Fatal(err)
+ }
+ _, err = val.Bool()
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+}