dao: delay buffer creation until it's needed

Verification contexts don't ever touch the storage, so these allocations can
be avoided for them.
This commit is contained in:
Roman Khimov 2022-02-18 14:24:45 +03:00
parent e864768c88
commit 1ca918e631

View file

@ -31,6 +31,7 @@ var (
type Simple struct { type Simple struct {
Version Version Version Version
Store *storage.MemCachedStore Store *storage.MemCachedStore
private bool
keyBuf []byte keyBuf []byte
dataBuf *io.BufBinWriter dataBuf *io.BufBinWriter
} }
@ -71,12 +72,7 @@ func (dao *Simple) GetPrivate() *Simple {
d := &Simple{} d := &Simple{}
*d = *dao // Inherit everything... *d = *dao // Inherit everything...
d.Store = storage.NewPrivateMemCachedStore(dao.Store) // except storage, wrap another layer. d.Store = storage.NewPrivateMemCachedStore(dao.Store) // except storage, wrap another layer.
if d.keyBuf == nil { d.private = true
d.keyBuf = make([]byte, 0, 1+4+storage.MaxStorageKeyLen) // Prefix, uint32, key.
}
if dao.dataBuf == nil {
d.dataBuf = io.NewBufBinWriter()
}
return d return d
} }
@ -748,14 +744,20 @@ func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32,
} }
func (dao *Simple) getKeyBuf(len int) []byte { 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 dao.keyBuf[:len] // Should have enough capacity.
} }
return make([]byte, len) return make([]byte, len)
} }
func (dao *Simple) getDataBuf() *io.BufBinWriter { func (dao *Simple) getDataBuf() *io.BufBinWriter {
if dao.dataBuf != nil { if dao.private {
if dao.dataBuf == nil {
dao.dataBuf = io.NewBufBinWriter()
}
dao.dataBuf.Reset() dao.dataBuf.Reset()
return dao.dataBuf return dao.dataBuf
} }