-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codec: add package for codec pooling
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Package codec is a unified place for configuring and allocating JSON encoders | ||
// and decoders. | ||
package codec | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
var jsonHandle codec.JsonHandle | ||
|
||
func init() { | ||
// This is documented to cause "smart buffering". | ||
jsonHandle.WriterBufferSize = 4096 | ||
jsonHandle.ReaderBufferSize = 4096 | ||
} | ||
|
||
// Encoder and decoder pools, to reuse if possible. | ||
var ( | ||
encPool = sync.Pool{ | ||
New: func() interface{} { | ||
return codec.NewEncoder(nil, &jsonHandle) | ||
}, | ||
} | ||
decPool = sync.Pool{ | ||
New: func() interface{} { | ||
return codec.NewDecoder(nil, &jsonHandle) | ||
}, | ||
} | ||
) | ||
|
||
// Encoder encodes. | ||
type Encoder = codec.Encoder | ||
|
||
// GetEncoder returns an encoder configured to write to w. | ||
func GetEncoder(w io.Writer) *Encoder { | ||
e := encPool.Get().(*Encoder) | ||
e.Reset(w) | ||
return e | ||
} | ||
|
||
// PutEncoder returns an encoder to the pool. | ||
func PutEncoder(e *Encoder) { | ||
e.Reset(nil) | ||
encPool.Put(e) | ||
} | ||
|
||
// Decoder decodes. | ||
type Decoder = codec.Decoder | ||
|
||
// GetDecoder returns a decoder configured to read from r. | ||
func GetDecoder(r io.Reader) *Decoder { | ||
d := decPool.Get().(*Decoder) | ||
d.Reset(r) | ||
return d | ||
} | ||
|
||
// PutDecoder returns a decoder to the pool. | ||
func PutDecoder(d *Decoder) { | ||
d.Reset(nil) | ||
decPool.Put(d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package codec | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func Example() { | ||
enc := GetEncoder(os.Stdout) | ||
defer PutEncoder(enc) | ||
enc.MustEncode([]string{"a", "slice", "of", "strings"}) | ||
fmt.Fprintln(os.Stdout) | ||
enc.MustEncode(nil) | ||
fmt.Fprintln(os.Stdout) | ||
enc.MustEncode(map[string]string{}) | ||
fmt.Fprintln(os.Stdout) | ||
// Output: ["a","slice","of","strings"] | ||
// null | ||
// {} | ||
} | ||
|
||
func BenchmarkDecode(b *testing.B) { | ||
b.ReportAllocs() | ||
want := map[string]string{ | ||
"a": strings.Repeat(`A`, 2048), | ||
"b": strings.Repeat(`B`, 2048), | ||
"c": strings.Repeat(`C`, 2048), | ||
"d": strings.Repeat(`D`, 2048), | ||
} | ||
got := make(map[string]string, len(want)) | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
dec := GetDecoder(JSONReader(want)) | ||
err := dec.Decode(&got) | ||
PutDecoder(dec) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
if !cmp.Equal(got, want) { | ||
b.Error(cmp.Diff(got, want)) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkDecodeStdlib(b *testing.B) { | ||
b.ReportAllocs() | ||
want := map[string]string{ | ||
"a": strings.Repeat(`A`, 2048), | ||
"b": strings.Repeat(`B`, 2048), | ||
"c": strings.Repeat(`C`, 2048), | ||
"d": strings.Repeat(`D`, 2048), | ||
} | ||
got := make(map[string]string, len(want)) | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
x, err := json.Marshal(want) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
if err := json.Unmarshal(x, &got); err != nil { | ||
b.Error(err) | ||
} | ||
if !cmp.Equal(got, want) { | ||
b.Error(cmp.Diff(got, want)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package codec | ||
|
||
import "io" | ||
|
||
// JSONReader returns an io.ReadCloser backed by a pipe being fed by a JSON | ||
// encoder. | ||
func JSONReader(v interface{}) io.ReadCloser { | ||
r, w := io.Pipe() | ||
// This unsupervised goroutine should be fine, because the writer will error | ||
// once the reader is closed. | ||
go func() { | ||
enc := GetEncoder(w) | ||
defer PutEncoder(enc) | ||
defer w.Close() | ||
if err := enc.Encode(v); err != nil { | ||
w.CloseWithError(err) | ||
} | ||
}() | ||
return r | ||
} |