summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/francoispqt/gojay/encode_pool.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-08-10 00:29:54 +0200
committerGitHub <noreply@github.com>2020-08-10 00:29:54 +0200
commit4e50fd864921c556988c919269448efdb90fa961 (patch)
treea3625f03f8de3c4f3841364000a4ea3aa42c1533 /vendor/github.com/francoispqt/gojay/encode_pool.go
parentdfdffa0027334e55ce213fc6eb62206dbf48baf6 (diff)
downloadmatterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.gz
matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.bz2
matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.zip
Use mattermost v5 module (#1192)
Diffstat (limited to 'vendor/github.com/francoispqt/gojay/encode_pool.go')
-rw-r--r--vendor/github.com/francoispqt/gojay/encode_pool.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/github.com/francoispqt/gojay/encode_pool.go b/vendor/github.com/francoispqt/gojay/encode_pool.go
new file mode 100644
index 00000000..3b263225
--- /dev/null
+++ b/vendor/github.com/francoispqt/gojay/encode_pool.go
@@ -0,0 +1,50 @@
+package gojay
+
+import (
+ "io"
+ "sync"
+)
+
+var encPool = sync.Pool{
+ New: func() interface{} {
+ return NewEncoder(nil)
+ },
+}
+
+var streamEncPool = sync.Pool{
+ New: func() interface{} {
+ return Stream.NewEncoder(nil)
+ },
+}
+
+func init() {
+ for i := 0; i < 32; i++ {
+ encPool.Put(NewEncoder(nil))
+ }
+ for i := 0; i < 32; i++ {
+ streamEncPool.Put(Stream.NewEncoder(nil))
+ }
+}
+
+// NewEncoder returns a new encoder or borrows one from the pool
+func NewEncoder(w io.Writer) *Encoder {
+ return &Encoder{w: w}
+}
+
+// BorrowEncoder borrows an Encoder from the pool.
+func BorrowEncoder(w io.Writer) *Encoder {
+ enc := encPool.Get().(*Encoder)
+ enc.w = w
+ enc.buf = enc.buf[:0]
+ enc.isPooled = 0
+ enc.err = nil
+ enc.hasKeys = false
+ enc.keys = nil
+ return enc
+}
+
+// Release sends back a Encoder to the pool.
+func (enc *Encoder) Release() {
+ enc.isPooled = 1
+ encPool.Put(enc)
+}