From 6896b40dee96e214f086134399092ab3894c4007 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 13 Dec 2019 18:46:05 +0300 Subject: [PATCH] state: use open-coded array (de)serialization for accounts We're spending a lot of time here, 100K blocks import starting at 1.4M, before this patch: real 4m17,748s user 6m23,316s sys 0m37,866s After: real 3m54,968s user 5m56,547s sys 0m39,398s 9% is quite a substantial improvement to justify this change. --- pkg/core/state/account.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/core/state/account.go b/pkg/core/state/account.go index d1a016d6c..12522b0d4 100644 --- a/pkg/core/state/account.go +++ b/pkg/core/state/account.go @@ -49,8 +49,11 @@ func (s *Account) DecodeBinary(br *io.BinReader) { for i := 0; i < int(lenBalances); i++ { key := util.Uint256{} br.ReadBytes(key[:]) - ubs := make([]UnspentBalance, 0) - br.ReadArray(&ubs) + len := int(br.ReadVarUint()) + ubs := make([]UnspentBalance, len) + for j := 0; j < len; j++ { + ubs[j].DecodeBinary(br) + } s.Balances[key] = ubs } } @@ -65,7 +68,10 @@ func (s *Account) EncodeBinary(bw *io.BinWriter) { bw.WriteVarUint(uint64(len(s.Balances))) for k, v := range s.Balances { bw.WriteBytes(k[:]) - bw.WriteArray(v) + bw.WriteVarUint(uint64(len(v))) + for i := range v { + v[i].EncodeBinary(bw) + } } }