44"bytes"
55"io"
66"strconv"
7+ "sync"
78"unicode"
89"unicode/utf16"
910"unicode/utf8"
@@ -17,9 +18,24 @@ import (
1718
1819var hex = "0123456789abcdef"
1920
21+ var bufferPool = sync.Pool {
22+ New : func () interface {} {
23+ return & bytes.Buffer {}
24+ },
25+ }
26+
27+ func getBuffer () * bytes.Buffer {
28+ return bufferPool .Get ().(* bytes.Buffer )
29+ }
30+
31+ func poolBuffer (buf * bytes.Buffer ) {
32+ buf .Reset ()
33+ bufferPool .Put (buf )
34+ }
35+
2036// NOTE: keep in sync with writeQuotedBytes below.
2137func writeQuotedString (w io.Writer , s string ) (int , error ) {
22- buf := & bytes. Buffer {}
38+ buf := getBuffer ()
2339buf .WriteByte ('"' )
2440start := 0
2541for i := 0 ; i < len (s ); {
@@ -70,12 +86,14 @@ func writeQuotedString(w io.Writer, s string) (int, error) {
7086buf .WriteString (s [start :])
7187}
7288buf .WriteByte ('"' )
73- return w .Write (buf .Bytes ())
89+ n , err := w .Write (buf .Bytes ())
90+ poolBuffer (buf )
91+ return n , err
7492}
7593
7694// NOTE: keep in sync with writeQuoteString above.
7795func writeQuotedBytes (w io.Writer , s []byte ) (int , error ) {
78- buf := & bytes. Buffer {}
96+ buf := getBuffer ()
7997buf .WriteByte ('"' )
8098start := 0
8199for i := 0 ; i < len (s ); {
@@ -126,7 +144,9 @@ func writeQuotedBytes(w io.Writer, s []byte) (int, error) {
126144buf .Write (s [start :])
127145}
128146buf .WriteByte ('"' )
129- return w .Write (buf .Bytes ())
147+ n , err := w .Write (buf .Bytes ())
148+ poolBuffer (buf )
149+ return n , err
130150}
131151
132152// getu4 decodes \uXXXX from the beginning of s, returning the hex value,
0 commit comments