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.
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: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
ineth.Bloom
and thebig.Int
inQuantity
so I had to special case Quantity and just skipped deep copying eth.Bloom for now.