From 544d6526df59fa74215379c12e7b7c21d2cec7d3 Mon Sep 17 00:00:00 2001 From: KamiD <44460798+KamiD@users.noreply.github.com> Date: Wed, 2 Jun 2021 18:05:00 +0800 Subject: [PATCH] optimize code to avoid reinit cdc (#895) --- app/rpc/namespaces/eth/simulation/impl.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/rpc/namespaces/eth/simulation/impl.go b/app/rpc/namespaces/eth/simulation/impl.go index 90e01dab5c..d6fd474bcc 100644 --- a/app/rpc/namespaces/eth/simulation/impl.go +++ b/app/rpc/namespaces/eth/simulation/impl.go @@ -169,11 +169,27 @@ type InternalDba struct { ocProxy QueryOnChainProxy } -func newCdc() *codec.Codec { +var gSimulateCdc *codec.Codec = nil +var gSimulateChainConfig []byte = nil + +func instanceOfCdc() *codec.Codec { + if gSimulateCdc != nil { + return gSimulateCdc + } module := evm.AppModuleBasic{} cdc := codec.New() module.RegisterCodec(cdc) - return cdc + gSimulateCdc = cdc + return gSimulateCdc +} + +func instanceOfChainConfig() []byte { + if gSimulateChainConfig != nil { + return gSimulateChainConfig + } + cdc := instanceOfCdc() + gSimulateChainConfig = cdc.MustMarshalBinaryBare(evmtypes.DefaultChainConfig()) + return gSimulateChainConfig } func NewInternalDba(qoc QueryOnChainProxy) InternalDba { @@ -186,10 +202,9 @@ func (i InternalDba) NewStore(parent store.KVStore, Prefix []byte) evmtypes.Stor return nil } - cdc := newCdc() switch Prefix[0] { case evmtypes.KeyPrefixChainConfig[0]: - return ConfigStore{defaultConfig: cdc.MustMarshalBinaryBare(evmtypes.DefaultChainConfig())} + return ConfigStore{defaultConfig: instanceOfChainConfig()} case evmtypes.KeyPrefixBloom[0]: return BloomStore{} case evmtypes.KeyPrefixStorage[0]: