Skip to content

Commit

Permalink
encoding/json: optimize marshal for quoted string
Browse files Browse the repository at this point in the history
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.

Additionally, this code runs significantly faster for quoted string fields
(30 - 45% in my measurements, depending on the length of the string that is
processed).

Fixes: golang#34154
  • Loading branch information
breml committed Sep 9, 2019
1 parent 547021d commit 96aea0f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/encoding/json/bench_test.go
Expand Up @@ -297,6 +297,22 @@ func BenchmarkIssue10335(b *testing.B) {
})
}

func BenchmarkIssue34127(b *testing.B) {
b.ReportAllocs()
j := struct {
Bar string `json:"bar,string"`
}{
Bar: `foobar`,
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := Marshal(&j); err != nil {
b.Fatal(err)
}
}
})
}

func BenchmarkUnmapped(b *testing.B) {
b.ReportAllocs()
j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`)
Expand Down
10 changes: 5 additions & 5 deletions src/encoding/json/encode.go
Expand Up @@ -601,11 +601,11 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
return
}
if opts.quoted {
sb, err := Marshal(v.String())
if err != nil {
e.error(err)
}
e.string(string(sb), opts.escapeHTML)
b := make([]byte, 0, v.Len()+2)
b = append(b, '"')
b = append(b, []byte(v.String())...)
b = append(b, '"')
e.stringBytes(b, opts.escapeHTML)
} else {
e.string(v.String(), opts.escapeHTML)
}
Expand Down
21 changes: 21 additions & 0 deletions src/encoding/json/stream_test.go
Expand Up @@ -158,6 +158,27 @@ func TestEncoderSetEscapeHTML(t *testing.T) {
}
}

// https://golang.org/issue/34154
func TestEncoderStringOptionSetEscapeHTMLFalse(t *testing.T) {
value := struct {
Bar string `json:"bar,string"`
}{
Bar: `<html>foobar</html>`,
}
want := `{"bar":"\"<html>foobar</html>\""}`

var buf bytes.Buffer
enc := NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(value); err != nil {
t.Fatalf("Encode: %s", err)
}
if got := strings.TrimSpace(buf.String()); got != want {
t.Errorf("SetEscapeHTML(false) Encode = %#q, want %#q",
got, want)
}
}

func TestDecoder(t *testing.T) {
for i := 0; i <= len(streamTest); i++ {
// Use stream without newlines as input,
Expand Down

0 comments on commit 96aea0f

Please sign in to comment.