From a2f246c2a6fb2f3aa3a621b653aaa1240a38ab81 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Mon, 13 Dec 2021 16:05:12 -0800 Subject: [PATCH] x/evm/keeper: use the fastest slice making idiom for Keeper.EthereumTx.Logs (#827) Uses the fastest slice making idiom of creating the well known size of a slice using make([]sdk.Attribute, len(response.Logs)) for i, log := range response.Logs { txLogAttrs[i] = ... } instead of make([]sdk.Attribute, 0) for _, log := range response.Logs { txLogAttrs = append(txLogAttrs, ...) } which had a few problems: 1. Using 0 for size then appending is quite slow yet we know the exact size 2. Using append instead of indexing is slower If we examine the advisory at https://bencher.orijtech.com/perfclinic/sliceupdate/ and the verdict at https://bencher.orijtech.com/perfclinic/sliceupdate/#verdict this new scheme shows a massive improvement in that call site. Fixes #825 --- CHANGELOG.md | 1 + x/evm/keeper/msg_server.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cee2a1dd..9cf84534 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * (evm) [tharsis#826](https://github.com/tharsis/ethermint/issues/826) Improve allocation of bytes of `tx.To` address. +* (evm) [tharsis#827](https://github.com/tharsis/ethermint/issues/827) Speed up creation of event logs by using the slice insertion idiom with indices. ### Bug Fixes diff --git a/x/evm/keeper/msg_server.go b/x/evm/keeper/msg_server.go index df132a4f..52a02f4b 100644 --- a/x/evm/keeper/msg_server.go +++ b/x/evm/keeper/msg_server.go @@ -52,13 +52,13 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t attrs = append(attrs, sdk.NewAttribute(types.AttributeKeyEthereumTxFailed, response.VmError)) } - txLogAttrs := make([]sdk.Attribute, 0) - for _, log := range response.Logs { + txLogAttrs := make([]sdk.Attribute, len(response.Logs)) + for i, log := range response.Logs { value, err := json.Marshal(log) if err != nil { return nil, sdkerrors.Wrap(err, "failed to encode log") } - txLogAttrs = append(txLogAttrs, sdk.NewAttribute(types.AttributeKeyTxLog, string(value))) + txLogAttrs[i] = sdk.NewAttribute(types.AttributeKeyTxLog, string(value)) } // emit events