INFURA / go-ethlibs

Ethereum libraries in Go for interacting with Ethereum nodes
MIT License
162 stars 34 forks source link

Use deepcopy-gen to generate .DeepCopy methods. #31

Closed ryanschneider closed 4 years ago

ryanschneider commented 4 years ago

I was profiling some code that returned a []eth.Block and was using JSON encode/decode to make a "clean" copy of that data, and was pretty shocked how slow that was. Turns out these generated deep-copy methods are about 30-40x faster.

The reason this is necessary is that since eth.Block.Transactions is a slice, doing:

type api struct{
  blocks []eth.Block
}

func (a *api) Blocks() []eth.Block {
   result := make([]eth.Block, len(a.blocks)
   copy(result, a.blocks)
   return result
}

Would return blocks that point to the same transaction objects (or more specifically the same TxOrHash objects), which means that, for example, calling .DepopulateTransactions() would end up mutating values inside api.blocks.

Anyways, this k8s deepcopy-gen tool worked pretty well, the only caveat was that it couldn't handle the [256]byte in eth.Bloom and the big.Int in Quantity so I had to special case Quantity and just skipped deep copying eth.Bloom for now.