From 1ca918e6310f73fd44454c8ed68f42979cd64a5f Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 18 Feb 2022 14:24:45 +0300 Subject: [PATCH] dao: delay buffer creation until it's needed Verification contexts don't ever touch the storage, so these allocations can be avoided for them. --- pkg/core/dao/dao.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index ec9c44dce..37e8a8d6d 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -31,6 +31,7 @@ var ( type Simple struct { Version Version Store *storage.MemCachedStore + private bool keyBuf []byte dataBuf *io.BufBinWriter } @@ -71,12 +72,7 @@ func (dao *Simple) GetPrivate() *Simple { d := &Simple{} *d = *dao // Inherit everything... d.Store = storage.NewPrivateMemCachedStore(dao.Store) // except storage, wrap another layer. - if d.keyBuf == nil { - d.keyBuf = make([]byte, 0, 1+4+storage.MaxStorageKeyLen) // Prefix, uint32, key. - } - if dao.dataBuf == nil { - d.dataBuf = io.NewBufBinWriter() - } + d.private = true return d } @@ -748,14 +744,20 @@ func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32, } func (dao *Simple) getKeyBuf(len int) []byte { - if dao.keyBuf != nil { // Private DAO. + if dao.private { + if dao.keyBuf == nil { + dao.keyBuf = make([]byte, 0, 1+4+storage.MaxStorageKeyLen) // Prefix, uint32, key. + } return dao.keyBuf[:len] // Should have enough capacity. } return make([]byte, len) } func (dao *Simple) getDataBuf() *io.BufBinWriter { - if dao.dataBuf != nil { + if dao.private { + if dao.dataBuf == nil { + dao.dataBuf = io.NewBufBinWriter() + } dao.dataBuf.Reset() return dao.dataBuf }