Skip to content

Commit b87bd31

Browse files
omerfirmaklwedge99
authored andcommitted
refactor: reduce number of heap allocations in tracing (scroll-tech#952)
1 parent b3cdb86 commit b87bd31

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

@@ -92,8 +92,8 @@ type StructLogRes struct {
9292

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

core/vm/logger.go

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

84-
func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) *StructLog {
85-
return &StructLog{
84+
func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error) StructLog {
85+
return StructLog{
8686
Pc: pc,
8787
Op: op,
8888
Gas: gas,
@@ -92,14 +92,6 @@ func NewStructlog(pc uint64, op OpCode, gas, cost uint64, depth int, err error)
9292
}
9393
}
9494

95-
func (s *StructLog) clean() {
96-
s.Memory.Reset()
97-
s.Stack = s.Stack[:0]
98-
s.ReturnData.Reset()
99-
s.Storage = nil
100-
s.Err = nil
101-
}
102-
10395
// overrides for gencodec
10496
type structLogMarshaling struct {
10597
Gas math.HexOrDecimal64
@@ -165,7 +157,7 @@ type StructLogger struct {
165157
createdAccount *types.AccountWrapper
166158

167159
callStackLogInd []int
168-
logs []*StructLog
160+
logs []StructLog
169161
output []byte
170162
err error
171163

@@ -405,7 +397,7 @@ func (l *StructLogger) TracedBytecodes() map[common.Hash]CodeInfo {
405397
func (l *StructLogger) CreatedAccount() *types.AccountWrapper { return l.createdAccount }
406398

407399
// StructLogs returns the captured log entries.
408-
func (l *StructLogger) StructLogs() []*StructLog { return l.logs }
400+
func (l *StructLogger) StructLogs() []StructLog { return l.logs }
409401

410402
// Error returns the VM error captured by the trace.
411403
func (l *StructLogger) Error() error { return l.err }
@@ -414,7 +406,7 @@ func (l *StructLogger) Error() error { return l.err }
414406
func (l *StructLogger) Output() []byte { return l.output }
415407

416408
// WriteTrace writes a formatted trace to the given writer
417-
func WriteTrace(writer io.Writer, logs []*StructLog) {
409+
func WriteTrace(writer io.Writer, logs []StructLog) {
418410
for _, log := range logs {
419411
fmt.Fprintf(writer, "%-16spc=%08d gas=%v cost=%v", log.Op, log.Pc, log.Gas, log.GasCost)
420412
if log.Err != nil {
@@ -534,8 +526,8 @@ func (t *mdLogger) CaptureEnter(typ OpCode, from common.Address, to common.Addre
534526
func (t *mdLogger) CaptureExit(output []byte, gasUsed uint64, err error) {}
535527

536528
// FormatLogs formats EVM returned structured logs for json output
537-
func FormatLogs(logs []*StructLog) []*types.StructLogRes {
538-
formatted := make([]*types.StructLogRes, 0, len(logs))
529+
func FormatLogs(logs []StructLog) []types.StructLogRes {
530+
formatted := make([]types.StructLogRes, 0, len(logs))
539531

540532
for _, trace := range logs {
541533
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)