From 7c2d07e66a2c5d4630aa2c435c37cd2a667dff7d Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Sun, 23 Oct 2022 11:55:44 -0400 Subject: pkg/api/api0/serverlist: Use sync.Pool for gzip writers --- pkg/api/api0/serverlist.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'pkg/api/api0/serverlist.go') diff --git a/pkg/api/api0/serverlist.go b/pkg/api/api0/serverlist.go index 53f7b95..021af65 100644 --- a/pkg/api/api0/serverlist.go +++ b/pkg/api/api0/serverlist.go @@ -45,6 +45,7 @@ type ServerList struct { csBytes atomic.Pointer[[]byte] // contents of buffer must not be modified; only swapped // /client/servers gzipped json + csgzPool sync.Pool // gzip writer pool csgzUpdate atomic.Pointer[*byte] // pointer to the first byte of the last known json (works because it must be swapped, not modified) csgzUpdatePf bool // ensures only one update runs at a time csgzUpdateCv *sync.Cond // allows other goroutines to wait for that update to complete @@ -351,7 +352,14 @@ func (s *ServerList) csGetJSONGzip() ([]byte, bool) { } var b bytes.Buffer - zw := gzip.NewWriter(&b) + var zw *gzip.Writer + if o := s.csgzPool.Get(); o == nil { + zw = gzip.NewWriter(&b) + } else { + zw = o.(*gzip.Writer) + zw.Reset(&b) + } + defer s.csgzPool.Put(zw) if _, err := zw.Write(buf); err != nil { s.csgzBytes.Store(nil) s.csgzUpdate.Store(&cur) -- cgit v1.2.3