Skip to content

Commit

Permalink
heap instead of var allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
racytech committed Nov 19, 2024
1 parent af5adad commit 97e9dee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
21 changes: 21 additions & 0 deletions core/types/encdec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,24 @@ func TestBodyEncodeDecodeRLP(t *testing.T) {
}
}
}

func TestWithdrawalEncodeDecodeRLP(t *testing.T) {
tr := NewTRand()
var buf bytes.Buffer
for i := 0; i < RUNS; i++ {
enc := tr.RandWithdrawal()
buf.Reset()
if err := enc.EncodeRLP(&buf); err != nil {
t.Errorf("error: RawBody.EncodeRLP(): %v", err)
}

s := rlp.NewStream(bytes.NewReader(buf.Bytes()), 0)
dec := &Withdrawal{}
if err := dec.DecodeRLP(s); err != nil {
t.Errorf("error: RawBody.DecodeRLP(): %v", err)
panic(err)
}

checkWithdrawals(t, enc, dec)
}
}
16 changes: 15 additions & 1 deletion core/types/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ import (
"github.com/erigontech/erigon/rlp"
)

// var buff = sync.Pool{
// New: func() interface{} { return new([33]byte) },
// }

// func (b *buff) reset() {

// }

// func newEncBuff() [33]byte {
// buf := buff.Get().([33]byte)
// buf.reset()
// }

//go:generate gencodec -type Withdrawal -field-override withdrawalMarshaling -out gen_withdrawal_json.go

// Withdrawal represents a validator withdrawal from the consensus layer.
Expand All @@ -54,9 +67,10 @@ func (obj *Withdrawal) EncodingSize() int {
}

func (obj *Withdrawal) EncodeRLP(w io.Writer) error {

encodingSize := obj.EncodingSize()

var b [33]byte
b := make([]byte, 33)
if err := EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil {
return err
}
Expand Down

0 comments on commit 97e9dee

Please sign in to comment.