Skip to content

Commit

Permalink
Speedup block unmarshalling
Browse files Browse the repository at this point in the history
   Skipping unmarshalling of InMsgDescr and OutMsgDescr in BlockExtra
gives 90% speed improvement.
  • Loading branch information
aleksej-paschenko committed Feb 2, 2024
1 parent 05d7e5b commit 208974f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
48 changes: 41 additions & 7 deletions tlb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,47 @@ func (m *ValueFlow) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
// created_by:bits256
// custom:(Maybe ^McBlockExtra) = BlockExtra;
type BlockExtra struct {
Magic Magic `tlb:"block_extra#4a33f6fd"`
InMsgDescr HashmapAugE[Bits256, InMsg, ImportFees] `tlb:"^"` // tlb.Any `tlb:"^"`
OutMsgDescr HashmapAugE[Bits256, OutMsg, CurrencyCollection] `tlb:"^"` // tlb.Any `tlb:"^"`
AccountBlocks HashmapAugE[Bits256, AccountBlock, CurrencyCollection] `tlb:"^"` // tlb.Any `tlb:"^"` //
RandSeed Bits256
CreatedBy Bits256
Custom Maybe[Ref[McBlockExtra]]
Magic Magic `tlb:"block_extra#4a33f6fd"`
InMsgDescrCell boc.Cell `tlb:"^"`
OutMsgDescrCell boc.Cell `tlb:"^"`
AccountBlocks HashmapAugE[Bits256, AccountBlock, CurrencyCollection] `tlb:"^"`
RandSeed Bits256
CreatedBy Bits256
Custom Maybe[Ref[McBlockExtra]]
}

func (extra *BlockExtra) InMsgDescrLength() (int, error) {
// TODO: come up with a better way to get the length of the hashmap
var hashmap HashmapAugE[Bits256, boc.Cell, boc.Cell]
if err := Unmarshal(&extra.InMsgDescrCell, &hashmap); err != nil {
return 0, err
}
return len(hashmap.Keys()), nil
}

func (extra *BlockExtra) InMsgDescr() (HashmapAugE[Bits256, InMsg, ImportFees], error) {
var hashmap HashmapAugE[Bits256, InMsg, ImportFees]
if err := Unmarshal(&extra.InMsgDescrCell, &hashmap); err != nil {
return HashmapAugE[Bits256, InMsg, ImportFees]{}, err
}
return hashmap, nil
}

func (extra *BlockExtra) OutMsgDescrLength() (int, error) {
// TODO: come up with a better way to get the length of the hashmap
var hashmap HashmapAugE[Bits256, boc.Cell, boc.Cell]
if err := Unmarshal(&extra.OutMsgDescrCell, &hashmap); err != nil {
return 0, err
}
return len(hashmap.Keys()), nil
}

func (extra *BlockExtra) OutMsgDescr() (HashmapAugE[Bits256, OutMsg, CurrencyCollection], error) {
var hashmap HashmapAugE[Bits256, OutMsg, CurrencyCollection]
if err := Unmarshal(&extra.OutMsgDescrCell, &hashmap); err != nil {
return HashmapAugE[Bits256, OutMsg, CurrencyCollection]{}, err
}
return hashmap, nil
}

// masterchain_block_extra#cca5
Expand Down
12 changes: 10 additions & 2 deletions tlb/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,20 @@ func Test_tlb_Unmarshal(t *testing.T) {
sort.Slice(txHashes, func(i, j int) bool {
return txHashes[i] < txHashes[j]
})
inMsgLength, err := block.Extra.InMsgDescrLength()
if err != nil {
t.Errorf("InMsgDescrLength() failed: %v", err)
}
outMsgLength, err := block.Extra.OutMsgDescrLength()
if err != nil {
t.Errorf("InMsgDescrLength() failed: %v", err)
}
blk := BlockContent{
Accounts: accounts,
TxHashes: txHashes,
ValueFlow: block.ValueFlow,
InMsgDescrLength: len(block.Extra.InMsgDescr.Keys()),
OutMsgDescrLength: len(block.Extra.OutMsgDescr.Keys()),
InMsgDescrLength: inMsgLength,
OutMsgDescrLength: outMsgLength,
}
bs, err := json.MarshalIndent(blk, " ", " ")
if err != nil {
Expand Down

0 comments on commit 208974f

Please sign in to comment.