From b84f4ed6f83df60e3fbde1b39af70fa317f4c3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Irmak?= Date: Thu, 1 Aug 2024 14:52:43 +0300 Subject: [PATCH] refactor: reduce number of heap allocations in tracing (#952) --- core/types/l2trace.go | 6 +++--- core/vm/logger.go | 22 +++++++--------------- eth/tracers/api_test.go | 10 +++++----- params/version.go | 2 +- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/core/types/l2trace.go b/core/types/l2trace.go index b5a1ebd8d053..08e36f97892b 100644 --- a/core/types/l2trace.go +++ b/core/types/l2trace.go @@ -70,7 +70,7 @@ type ExecutionResult struct { // currently they are just `from` and `to` account AccountsAfter []*AccountWrapper `json:"accountAfter"` - StructLogs []*StructLogRes `json:"structLogs"` + StructLogs []StructLogRes `json:"structLogs"` CallTrace json.RawMessage `json:"callTrace"` } @@ -91,8 +91,8 @@ type StructLogRes struct { // NewStructLogResBasic Basic StructLogRes skeleton, Stack&Memory&Storage&ExtraData are separated from it for GC optimization; // still need to fill in with Stack&Memory&Storage&ExtraData -func NewStructLogResBasic(pc uint64, op string, gas, gasCost uint64, depth int, refundCounter uint64, err error) *StructLogRes { - logRes := &StructLogRes{ +func NewStructLogResBasic(pc uint64, op string, gas, gasCost uint64, depth int, refundCounter uint64, err error) StructLogRes { + logRes := StructLogRes{ Pc: pc, Op: op, Gas: gas, diff --git a/core/vm/logger.go b/core/vm/logger.go index 740c7b93cc05..65970d6c39d6 100644 --- a/core/vm/logger.go +++ b/core/vm/logger.go @@ -79,8 +79,8 @@ type StructLog struct { Err error `json:"-"` } -func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) *StructLog { - return &StructLog{ +func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) StructLog { + return StructLog{ Pc: pc, Op: op, Gas: gas, @@ -90,14 +90,6 @@ func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) } } -func (s *StructLog) clean() { - s.Memory.Reset() - s.Stack = s.Stack[:0] - s.ReturnData.Reset() - s.Storage = nil - s.Err = nil -} - // overrides for gencodec type structLogMarshaling struct { Gas math.HexOrDecimal64 @@ -159,7 +151,7 @@ type StructLogger struct { createdAccount *types.AccountWrapper callStackLogInd []int - logs []*StructLog + logs []StructLog output []byte err error } @@ -359,7 +351,7 @@ func (l *StructLogger) TracedBytecodes() map[common.Hash]CodeInfo { func (l *StructLogger) CreatedAccount() *types.AccountWrapper { return l.createdAccount } // StructLogs returns the captured log entries. -func (l *StructLogger) StructLogs() []*StructLog { return l.logs } +func (l *StructLogger) StructLogs() []StructLog { return l.logs } // Error returns the VM error captured by the trace. func (l *StructLogger) Error() error { return l.err } @@ -368,7 +360,7 @@ func (l *StructLogger) Error() error { return l.err } func (l *StructLogger) Output() []byte { return l.output } // WriteTrace writes a formatted trace to the given writer -func WriteTrace(writer io.Writer, logs []*StructLog) { +func WriteTrace(writer io.Writer, logs []StructLog) { for _, log := range logs { fmt.Fprintf(writer, "%-16spc=%08d gas=%v cost=%v", log.Op, log.Pc, log.Gas, log.GasCost) if log.Err != nil { @@ -488,8 +480,8 @@ func (t *mdLogger) CaptureEnter(typ OpCode, from common.Address, to common.Addre func (t *mdLogger) CaptureExit(output []byte, gasUsed uint64, err error) {} // FormatLogs formats EVM returned structured logs for json output -func FormatLogs(logs []*StructLog) []*types.StructLogRes { - formatted := make([]*types.StructLogRes, 0, len(logs)) +func FormatLogs(logs []StructLog) []types.StructLogRes { + formatted := make([]types.StructLogRes, 0, len(logs)) for _, trace := range logs { logRes := types.NewStructLogResBasic(trace.Pc, trace.Op.String(), trace.Gas, trace.GasCost, trace.Depth, trace.RefundCounter, trace.Err) diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index e9928434a981..a020d8c3d43a 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -227,7 +227,7 @@ func TestTraceCall(t *testing.T) { Gas: params.TxGas, Failed: false, ReturnValue: "", - StructLogs: []*types.StructLogRes{}, + StructLogs: []types.StructLogRes{}, }, }, // Standard JSON trace upon the head, plain transfer. @@ -245,7 +245,7 @@ func TestTraceCall(t *testing.T) { Gas: params.TxGas, Failed: false, ReturnValue: "", - StructLogs: []*types.StructLogRes{}, + StructLogs: []types.StructLogRes{}, }, }, // Standard JSON trace upon the non-existent block, error expects @@ -275,7 +275,7 @@ func TestTraceCall(t *testing.T) { Gas: params.TxGas, Failed: false, ReturnValue: "", - StructLogs: []*types.StructLogRes{}, + StructLogs: []types.StructLogRes{}, }, }, // Standard JSON trace upon the pending block @@ -293,7 +293,7 @@ func TestTraceCall(t *testing.T) { Gas: params.TxGas, Failed: false, ReturnValue: "", - StructLogs: []*types.StructLogRes{}, + StructLogs: []types.StructLogRes{}, }, }, } @@ -347,7 +347,7 @@ func TestTraceTransaction(t *testing.T) { Gas: params.TxGas, Failed: false, ReturnValue: "", - StructLogs: []*types.StructLogRes{}, + StructLogs: []types.StructLogRes{}, }) { t.Error("Transaction tracing result is different") } diff --git a/params/version.go b/params/version.go index fe87555ffb66..49e55d52b498 100644 --- a/params/version.go +++ b/params/version.go @@ -24,7 +24,7 @@ import ( const ( VersionMajor = 5 // Major version component of the current release VersionMinor = 5 // Minor version component of the current release - VersionPatch = 19 // Patch version component of the current release + VersionPatch = 20 // Patch version component of the current release VersionMeta = "mainnet" // Version metadata to append to the version string )