Skip to content

Commit

Permalink
refactor: reduce number of heap allocations in tracing (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored Aug 1, 2024
1 parent 51c7eee commit b84f4ed
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
6 changes: 3 additions & 3 deletions core/types/l2trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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,
Expand Down
22 changes: 7 additions & 15 deletions core/vm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -159,7 +151,7 @@ type StructLogger struct {
createdAccount *types.AccountWrapper

callStackLogInd []int
logs []*StructLog
logs []StructLog
output []byte
err error
}
Expand Down Expand Up @@ -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 }
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -293,7 +293,7 @@ func TestTraceCall(t *testing.T) {
Gas: params.TxGas,
Failed: false,
ReturnValue: "",
StructLogs: []*types.StructLogRes{},
StructLogs: []types.StructLogRes{},
},
},
}
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down

0 comments on commit b84f4ed

Please sign in to comment.