From 3d86d5a177d162e61675bcd4afca4ae4dcbb3ece Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:12:47 -0400 Subject: db/pdatadb: Use sync.Pool for gzip readers/writers --- db/pdatadb/db.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/db/pdatadb/db.go b/db/pdatadb/db.go index 1e4c9e5..baebcb9 100644 --- a/db/pdatadb/db.go +++ b/db/pdatadb/db.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/url" + "sync" "github.com/jmoiron/sqlx" "github.com/klauspost/compress/gzip" @@ -16,7 +17,9 @@ import ( // DB stores player data in a sqlite3 database. type DB struct { - x *sqlx.DB + x *sqlx.DB + gzipW sync.Pool + gzipR sync.Pool } // Open opens a DB from the provided sqlite3 uri. @@ -35,7 +38,7 @@ func Open(name string) (*DB, error) { if _, err := x.Exec(`PRAGMA page_size = 8192`); err != nil { panic(err) } - return &DB{x}, nil + return &DB{x: x}, nil } func (db *DB) Close() error { @@ -100,7 +103,15 @@ func (db *DB) GetPdataCached(uid uint64, sha [sha256.Size]byte) (buf []byte, exi case "": case "gzip": var b bytes.Buffer - zr, err := gzip.NewReader(bytes.NewReader(obj.Pdata)) + var zr *gzip.Reader + var err error + if o := db.gzipR.Get(); o == nil { + zr, err = gzip.NewReader(bytes.NewReader(obj.Pdata)) + } else { + zr = o.(*gzip.Reader) + err = zr.Reset(bytes.NewReader(obj.Pdata)) + } + defer db.gzipR.Put(zr) if err != nil { return nil, false, fmt.Errorf("decompress gzip: %w", err) } @@ -134,7 +145,14 @@ func (db *DB) SetPdata(uid uint64, buf []byte) (n int, err error) { var b bytes.Buffer b.Grow(2000) - zw := gzip.NewWriter(&b) + var zw *gzip.Writer + if o := db.gzipW.Get(); o == nil { + zw = gzip.NewWriter(&b) + } else { + zw = o.(*gzip.Writer) + zw.Reset(&b) + } + defer db.gzipW.Put(zw) if _, err := zw.Write(buf); err != nil { return 0, fmt.Errorf("compress pdata: %w", err) } -- cgit v1.2.3