Skip to content

Commit b84f4ed

Browse files
authored
refactor: reduce number of heap allocations in tracing (#952)
1 parent 51c7eee commit b84f4ed

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

core/types/l2trace.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type ExecutionResult struct {
7070
// currently they are just `from` and `to` account
7171
AccountsAfter []*AccountWrapper `json:"accountAfter"`
7272

73-
StructLogs []*StructLogRes `json:"structLogs"`
73+
StructLogs []StructLogRes `json:"structLogs"`
7474
CallTrace json.RawMessage `json:"callTrace"`
7575
}
7676

@@ -91,8 +91,8 @@ type StructLogRes struct {
9191

9292
// NewStructLogResBasic Basic StructLogRes skeleton, Stack&Memory&Storage&ExtraData are separated from it for GC optimization;
9393
// still need to fill in with Stack&Memory&Storage&ExtraData
94-
func NewStructLogResBasic(pc uint64, op string, gas, gasCost uint64, depth int, refundCounter uint64, err error) *StructLogRes {
95-
logRes := &StructLogRes{
94+
func NewStructLogResBasic(pc uint64, op string, gas, gasCost uint64, depth int, refundCounter uint64, err error) StructLogRes {
95+
logRes := StructLogRes{
9696
Pc: pc,
9797
Op: op,
9898
Gas: gas,

core/vm/logger.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ type StructLog struct {
7979
Err error `json:"-"`
8080
}
8181

82-
func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) *StructLog {
83-
return &StructLog{
82+
func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) StructLog {
83+
return StructLog{
8484
Pc: pc,
8585
Op: op,
8686
Gas: gas,
@@ -90,14 +90,6 @@ func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error)
9090
}
9191
}
9292

93-
func (s *StructLog) clean() {
94-
s.Memory.Reset()
95-
s.Stack = s.Stack[:0]
96-
s.ReturnData.Reset()
97-
s.Storage = nil
98-
s.Err = nil
99-
}
100-
10193
// overrides for gencodec
10294
type structLogMarshaling struct {
10395
Gas math.HexOrDecimal64
@@ -159,7 +151,7 @@ type StructLogger struct {
159151
createdAccount *types.AccountWrapper
160152

161153
callStackLogInd []int
162-
logs []*StructLog
154+
logs []StructLog
163155
output []byte
164156
err error
165157
}
@@ -359,7 +351,7 @@ func (l *StructLogger) TracedBytecodes() map[common.Hash]CodeInfo {
359351
func (l *StructLogger) CreatedAccount() *types.AccountWrapper { return l.createdAccount }
360352

361353
// StructLogs returns the captured log entries.
362-
func (l *StructLogger) StructLogs() []*StructLog { return l.logs }
354+
func (l *StructLogger) StructLogs() []StructLog { return l.logs }
363355

364356
// Error returns the VM error captured by the trace.
365357
func (l *StructLogger) Error() error { return l.err }
@@ -368,7 +360,7 @@ func (l *StructLogger) Error() error { return l.err }
368360
func (l *StructLogger) Output() []byte { return l.output }
369361

370362
// WriteTrace writes a formatted trace to the given writer
371-
func WriteTrace(writer io.Writer, logs []*StructLog) {
363+
func WriteTrace(writer io.Writer, logs []StructLog) {
372364
for _, log := range logs {
373365
fmt.Fprintf(writer, "%-16spc=%08d gas=%v cost=%v", log.Op, log.Pc, log.Gas, log.GasCost)
374366
if log.Err != nil {
@@ -488,8 +480,8 @@ func (t *mdLogger) CaptureEnter(typ OpCode, from common.Address, to common.Addre
488480
func (t *mdLogger) CaptureExit(output []byte, gasUsed uint64, err error) {}
489481

490482
// FormatLogs formats EVM returned structured logs for json output
491-
func FormatLogs(logs []*StructLog) []*types.StructLogRes {
492-
formatted := make([]*types.StructLogRes, 0, len(logs))
483+
func FormatLogs(logs []StructLog) []types.StructLogRes {
484+
formatted := make([]types.StructLogRes, 0, len(logs))
493485

494486
for _, trace := range logs {
495487
logRes := types.NewStructLogResBasic(trace.Pc, trace.Op.String(), trace.Gas, trace.GasCost, trace.Depth, trace.RefundCounter, trace.Err)

eth/tracers/api_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func TestTraceCall(t *testing.T) {
227227
Gas: params.TxGas,
228228
Failed: false,
229229
ReturnValue: "",
230-
StructLogs: []*types.StructLogRes{},
230+
StructLogs: []types.StructLogRes{},
231231
},
232232
},
233233
// Standard JSON trace upon the head, plain transfer.
@@ -245,7 +245,7 @@ func TestTraceCall(t *testing.T) {
245245
Gas: params.TxGas,
246246
Failed: false,
247247
ReturnValue: "",
248-
StructLogs: []*types.StructLogRes{},
248+
StructLogs: []types.StructLogRes{},
249249
},
250250
},
251251
// Standard JSON trace upon the non-existent block, error expects
@@ -275,7 +275,7 @@ func TestTraceCall(t *testing.T) {
275275
Gas: params.TxGas,
276276
Failed: false,
277277
ReturnValue: "",
278-
StructLogs: []*types.StructLogRes{},
278+
StructLogs: []types.StructLogRes{},
279279
},
280280
},
281281
// Standard JSON trace upon the pending block
@@ -293,7 +293,7 @@ func TestTraceCall(t *testing.T) {
293293
Gas: params.TxGas,
294294
Failed: false,
295295
ReturnValue: "",
296-
StructLogs: []*types.StructLogRes{},
296+
StructLogs: []types.StructLogRes{},
297297
},
298298
},
299299
}
@@ -347,7 +347,7 @@ func TestTraceTransaction(t *testing.T) {
347347
Gas: params.TxGas,
348348
Failed: false,
349349
ReturnValue: "",
350-
StructLogs: []*types.StructLogRes{},
350+
StructLogs: []types.StructLogRes{},
351351
}) {
352352
t.Error("Transaction tracing result is different")
353353
}

params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 5 // Major version component of the current release
2626
VersionMinor = 5 // Minor version component of the current release
27-
VersionPatch = 19 // Patch version component of the current release
27+
VersionPatch = 20 // Patch version component of the current release
2828
VersionMeta = "mainnet" // Version metadata to append to the version string
2929
)
3030

0 commit comments

Comments
 (0)