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.
This commit is contained in:
Roman Khimov 2019-12-13 18:46:05 +03:00
parent 0d0a27d271
commit 6896b40dee

View file

@ -49,8 +49,11 @@ func (s *Account) DecodeBinary(br *io.BinReader) {
for i := 0; i < int(lenBalances); i++ { for i := 0; i < int(lenBalances); i++ {
key := util.Uint256{} key := util.Uint256{}
br.ReadBytes(key[:]) br.ReadBytes(key[:])
ubs := make([]UnspentBalance, 0) len := int(br.ReadVarUint())
br.ReadArray(&ubs) ubs := make([]UnspentBalance, len)
for j := 0; j < len; j++ {
ubs[j].DecodeBinary(br)
}
s.Balances[key] = ubs s.Balances[key] = ubs
} }
} }
@ -65,7 +68,10 @@ func (s *Account) EncodeBinary(bw *io.BinWriter) {
bw.WriteVarUint(uint64(len(s.Balances))) bw.WriteVarUint(uint64(len(s.Balances)))
for k, v := range s.Balances { for k, v := range s.Balances {
bw.WriteBytes(k[:]) bw.WriteBytes(k[:])
bw.WriteArray(v) bw.WriteVarUint(uint64(len(v)))
for i := range v {
v[i].EncodeBinary(bw)
}
} }
} }