Skip to content

Commit 0e015e2

Browse files
bremlmvdan
authored andcommitted
encoding/json: fix and optimize marshal for quoted string
Since Go 1.2 every string can be marshaled to JSON without error even if it contains invalid UTF-8 byte sequences. Therefore there is no need to use Marshal again for the only reason of enclosing the string in double quotes. Not using Marshal here also removes the error check as there has not been a way for Marshal to fail anyway. name old time/op new time/op delta Issue34127-4 360ns ± 3% 200ns ± 3% -44.56% (p=0.008 n=5+5) name old alloc/op new alloc/op delta Issue34127-4 56.0B ± 0% 40.0B ± 0% -28.57% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Issue34127-4 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.008 n=5+5) Fixes #34154 Change-Id: Ib60dc11980f9b20d8bef2982de7168943d632263 GitHub-Last-Rev: 9b0ac1d GitHub-Pull-Request: #34127 Reviewed-on: https://go-review.googlesource.com/c/go/+/193604 Reviewed-by: Daniel Martí <[email protected]> Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 11c2411 commit 0e015e2

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/encoding/json/bench_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ func BenchmarkIssue10335(b *testing.B) {
297297
})
298298
}
299299

300+
func BenchmarkIssue34127(b *testing.B) {
301+
b.ReportAllocs()
302+
j := struct {
303+
Bar string `json:"bar,string"`
304+
}{
305+
Bar: `foobar`,
306+
}
307+
b.RunParallel(func(pb *testing.PB) {
308+
for pb.Next() {
309+
if _, err := Marshal(&j); err != nil {
310+
b.Fatal(err)
311+
}
312+
}
313+
})
314+
}
315+
300316
func BenchmarkUnmapped(b *testing.B) {
301317
b.ReportAllocs()
302318
j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`)

src/encoding/json/encode.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,11 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
600600
return
601601
}
602602
if opts.quoted {
603-
sb, err := Marshal(v.String())
604-
if err != nil {
605-
e.error(err)
606-
}
607-
e.string(string(sb), opts.escapeHTML)
603+
b := make([]byte, 0, v.Len()+2)
604+
b = append(b, '"')
605+
b = append(b, []byte(v.String())...)
606+
b = append(b, '"')
607+
e.stringBytes(b, opts.escapeHTML)
608608
} else {
609609
e.string(v.String(), opts.escapeHTML)
610610
}

src/encoding/json/stream_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func TestEncoderSetEscapeHTML(t *testing.T) {
118118
Ptr strPtrMarshaler
119119
}{`"<str>"`, `"<str>"`}
120120

121+
// https://golang.org/issue/34154
122+
stringOption := struct {
123+
Bar string `json:"bar,string"`
124+
}{`<html>foobar</html>`}
125+
121126
for _, tt := range []struct {
122127
name string
123128
v interface{}
@@ -137,6 +142,11 @@ func TestEncoderSetEscapeHTML(t *testing.T) {
137142
`{"NonPtr":"\u003cstr\u003e","Ptr":"\u003cstr\u003e"}`,
138143
`{"NonPtr":"<str>","Ptr":"<str>"}`,
139144
},
145+
{
146+
"stringOption", stringOption,
147+
`{"bar":"\"\u003chtml\u003efoobar\u003c/html\u003e\""}`,
148+
`{"bar":"\"<html>foobar</html>\""}`,
149+
},
140150
} {
141151
var buf bytes.Buffer
142152
enc := NewEncoder(&buf)

0 commit comments

Comments
 (0)