Skip to content

Commit 2c8003c

Browse files
authored
RLP: remove allocations, replace buff with pooled buff (#12869)
Preparation for RLP gen package. For now replacing stack allocations with pooled buff, and removing other allocations in RLP encoding logic
1 parent 541a135 commit 2c8003c

File tree

10 files changed

+340
-114
lines changed

10 files changed

+340
-114
lines changed

core/types/access_list_tx.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,34 @@ func accessListSize(al AccessList) int {
168168
}
169169

170170
func encodeAccessList(al AccessList, w io.Writer, b []byte) error {
171-
for _, tuple := range al {
171+
for i := 0; i < len(al); i++ {
172172
tupleLen := 21
173173
// Each storage key takes 33 bytes
174-
storageLen := 33 * len(tuple.StorageKeys)
174+
storageLen := 33 * len(al[i].StorageKeys)
175175
tupleLen += rlp2.ListPrefixLen(storageLen) + storageLen
176176
if err := EncodeStructSizePrefix(tupleLen, w, b); err != nil {
177177
return err
178178
}
179-
if err := rlp.EncodeOptionalAddress(&tuple.Address, w, b); err != nil {
179+
if err := rlp.EncodeOptionalAddress(&al[i].Address, w, b); err != nil { // TODO(racytech): change addr to []byte?
180180
return err
181181
}
182182
if err := EncodeStructSizePrefix(storageLen, w, b); err != nil {
183183
return err
184184
}
185185
b[0] = 128 + 32
186-
for _, storageKey := range tuple.StorageKeys {
186+
for idx := 0; idx < len(al[i].StorageKeys); idx++ {
187187
if _, err := w.Write(b[:1]); err != nil {
188188
return err
189189
}
190-
if _, err := w.Write(storageKey.Bytes()); err != nil {
190+
if _, err := w.Write(al[i].StorageKeys[idx][:]); err != nil {
191191
return err
192192
}
193193
}
194194
}
195195
return nil
196196
}
197197

198-
func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error {
198+
func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error { // TODO(racytech): move it to rlp package?
199199
if size >= 56 {
200200
beSize := libcommon.BitLenToByteLen(bits.Len(uint(size)))
201201
binary.BigEndian.PutUint64(b[1:], uint64(size))
@@ -217,7 +217,8 @@ func EncodeStructSizePrefix(size int, w io.Writer, b []byte) error {
217217
// transactions, it returns the type and payload.
218218
func (tx *AccessListTx) MarshalBinary(w io.Writer) error {
219219
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
220-
var b [33]byte
220+
b := newEncodingBuf()
221+
defer pooledBuf.Put(b)
221222
// encode TxType
222223
b[0] = AccessListTxType
223224
if _, err := w.Write(b[:1]); err != nil {
@@ -235,15 +236,15 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL
235236
return err
236237
}
237238
// encode ChainID
238-
if err := tx.ChainID.EncodeRLP(w); err != nil {
239+
if err := rlp.EncodeUint256(tx.ChainID, w, b); err != nil {
239240
return err
240241
}
241242
// encode Nonce
242243
if err := rlp.EncodeInt(tx.Nonce, w, b); err != nil {
243244
return err
244245
}
245246
// encode GasPrice
246-
if err := tx.GasPrice.EncodeRLP(w); err != nil {
247+
if err := rlp.EncodeUint256(tx.GasPrice, w, b); err != nil {
247248
return err
248249
}
249250
// encode Gas
@@ -260,12 +261,12 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL
260261
return err
261262
}
262263
if tx.To != nil {
263-
if _, err := w.Write(tx.To.Bytes()); err != nil {
264+
if _, err := w.Write(tx.To[:]); err != nil {
264265
return err
265266
}
266267
}
267268
// encode Value
268-
if err := tx.Value.EncodeRLP(w); err != nil {
269+
if err := rlp.EncodeUint256(tx.Value, w, b); err != nil {
269270
return err
270271
}
271272
// encode Data
@@ -281,15 +282,15 @@ func (tx *AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceL
281282
return err
282283
}
283284
// encode V
284-
if err := tx.V.EncodeRLP(w); err != nil {
285+
if err := rlp.EncodeUint256(&tx.V, w, b); err != nil {
285286
return err
286287
}
287288
// encode R
288-
if err := tx.R.EncodeRLP(w); err != nil {
289+
if err := rlp.EncodeUint256(&tx.R, w, b); err != nil {
289290
return err
290291
}
291292
// encode S
292-
if err := tx.S.EncodeRLP(w); err != nil {
293+
if err := rlp.EncodeUint256(&tx.S, w, b); err != nil {
293294
return err
294295
}
295296
return nil
@@ -301,7 +302,8 @@ func (tx *AccessListTx) EncodeRLP(w io.Writer) error {
301302
payloadSize, nonceLen, gasLen, accessListLen := tx.payloadSize()
302303
// size of struct prefix and TxType
303304
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
304-
var b [33]byte
305+
b := newEncodingBuf()
306+
defer pooledBuf.Put(b)
305307
// envelope
306308
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
307309
return err

core/types/authorization.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,36 +182,34 @@ func decodeAuthorizations(auths *[]Authorization, s *rlp.Stream) error {
182182
}
183183

184184
func encodeAuthorizations(authorizations []Authorization, w io.Writer, b []byte) error {
185-
for _, auth := range authorizations {
186-
// 0. encode length of individual Authorization
187-
authLen := authorizationSize(auth)
185+
for i := 0; i < len(authorizations); i++ {
186+
authLen := authorizationSize(authorizations[i])
188187
if err := EncodeStructSizePrefix(authLen, w, b); err != nil {
189188
return err
190189
}
191190

192191
// 1. encode ChainId
193-
if err := rlp.EncodeInt(auth.ChainID, w, b); err != nil {
192+
if err := rlp.EncodeInt(authorizations[i].ChainID, w, b); err != nil {
194193
return err
195194
}
196195
// 2. encode Address
197-
if err := rlp.EncodeOptionalAddress(&auth.Address, w, b); err != nil {
196+
if err := rlp.EncodeOptionalAddress(&authorizations[i].Address, w, b); err != nil {
198197
return err
199198
}
200199
// 3. encode Nonce
201-
if err := rlp.EncodeInt(auth.Nonce, w, b); err != nil {
200+
if err := rlp.EncodeInt(authorizations[i].Nonce, w, b); err != nil {
202201
return err
203202
}
204203
// 4. encode YParity, R, S
205-
if err := rlp.EncodeInt(uint64(auth.YParity), w, b); err != nil {
204+
if err := rlp.EncodeInt(uint64(authorizations[i].YParity), w, b); err != nil {
206205
return err
207206
}
208-
if err := auth.R.EncodeRLP(w); err != nil {
207+
if err := rlp.EncodeUint256(&authorizations[i].R, w, b); err != nil {
209208
return err
210209
}
211-
if err := auth.S.EncodeRLP(w); err != nil {
210+
if err := rlp.EncodeUint256(&authorizations[i].S, w, b); err != nil {
212211
return err
213212
}
214213
}
215-
216214
return nil
217215
}

core/types/blob_tx.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func blobVersionedHashesSize(hashes []libcommon.Hash) int {
178178
}
179179

180180
func encodeBlobVersionedHashes(hashes []libcommon.Hash, w io.Writer, b []byte) error {
181-
for _, h := range hashes {
182-
if err := rlp.EncodeString(h[:], w, b); err != nil {
181+
for i := 0; i < len(hashes); i++ {
182+
if err := rlp.EncodeString(hashes[i][:], w, b); err != nil {
183183
return err
184184
}
185185
}
@@ -192,19 +192,19 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
192192
return err
193193
}
194194
// encode ChainID
195-
if err := stx.ChainID.EncodeRLP(w); err != nil {
195+
if err := rlp.EncodeUint256(stx.ChainID, w, b); err != nil {
196196
return err
197197
}
198198
// encode Nonce
199199
if err := rlp.EncodeInt(stx.Nonce, w, b); err != nil {
200200
return err
201201
}
202202
// encode MaxPriorityFeePerGas
203-
if err := stx.Tip.EncodeRLP(w); err != nil {
203+
if err := rlp.EncodeUint256(stx.Tip, w, b); err != nil {
204204
return err
205205
}
206206
// encode MaxFeePerGas
207-
if err := stx.FeeCap.EncodeRLP(w); err != nil {
207+
if err := rlp.EncodeUint256(stx.FeeCap, w, b); err != nil {
208208
return err
209209
}
210210
// encode Gas
@@ -216,11 +216,11 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
216216
if _, err := w.Write(b[:1]); err != nil {
217217
return err
218218
}
219-
if _, err := w.Write(stx.To.Bytes()); err != nil {
219+
if _, err := w.Write(stx.To[:]); err != nil {
220220
return err
221221
}
222222
// encode Value
223-
if err := stx.Value.EncodeRLP(w); err != nil {
223+
if err := rlp.EncodeUint256(stx.Value, w, b); err != nil {
224224
return err
225225
}
226226
// encode Data
@@ -236,7 +236,7 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
236236
return err
237237
}
238238
// encode MaxFeePerBlobGas
239-
if err := stx.MaxFeePerBlobGas.EncodeRLP(w); err != nil {
239+
if err := rlp.EncodeUint256(stx.MaxFeePerBlobGas, w, b); err != nil {
240240
return err
241241
}
242242
// prefix
@@ -248,15 +248,15 @@ func (stx *BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, g
248248
return err
249249
}
250250
// encode V
251-
if err := stx.V.EncodeRLP(w); err != nil {
251+
if err := rlp.EncodeUint256(&stx.V, w, b); err != nil {
252252
return err
253253
}
254254
// encode R
255-
if err := stx.R.EncodeRLP(w); err != nil {
255+
if err := rlp.EncodeUint256(&stx.R, w, b); err != nil {
256256
return err
257257
}
258258
// encode S
259-
if err := stx.S.EncodeRLP(w); err != nil {
259+
if err := rlp.EncodeUint256(&stx.S, w, b); err != nil {
260260
return err
261261
}
262262
return nil
@@ -266,7 +266,8 @@ func (stx *BlobTx) EncodeRLP(w io.Writer) error {
266266
payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize()
267267
// size of struct prefix and TxType
268268
envelopeSize := 1 + rlp2.ListPrefixLen(payloadSize) + payloadSize
269-
var b [33]byte
269+
b := newEncodingBuf()
270+
defer pooledBuf.Put(b)
270271
// envelope
271272
if err := rlp.EncodeStringSizePrefix(envelopeSize, w, b[:]); err != nil {
272273
return err
@@ -284,7 +285,8 @@ func (stx *BlobTx) EncodeRLP(w io.Writer) error {
284285

285286
func (stx *BlobTx) MarshalBinary(w io.Writer) error {
286287
payloadSize, nonceLen, gasLen, accessListLen, blobHashesLen := stx.payloadSize()
287-
var b [33]byte
288+
b := newEncodingBuf()
289+
defer pooledBuf.Put(b)
288290
// encode TxType
289291
b[0] = BlobTxType
290292
if _, err := w.Write(b[:1]); err != nil {

core/types/block.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ func (h *Header) EncodingSize() int {
200200
func (h *Header) EncodeRLP(w io.Writer) error {
201201
encodingSize := h.EncodingSize()
202202

203-
var b [33]byte
203+
b := newEncodingBuf()
204+
defer pooledBuf.Put(b)
204205
// Prefix
205206
if err := EncodeStructSizePrefix(encodingSize, w, b[:]); err != nil {
206207
return err
@@ -209,39 +210,39 @@ func (h *Header) EncodeRLP(w io.Writer) error {
209210
if _, err := w.Write(b[:1]); err != nil {
210211
return err
211212
}
212-
if _, err := w.Write(h.ParentHash.Bytes()); err != nil {
213+
if _, err := w.Write(h.ParentHash[:]); err != nil {
213214
return err
214215
}
215216
if _, err := w.Write(b[:1]); err != nil {
216217
return err
217218
}
218-
if _, err := w.Write(h.UncleHash.Bytes()); err != nil {
219+
if _, err := w.Write(h.UncleHash[:]); err != nil {
219220
return err
220221
}
221222
b[0] = 128 + 20
222223
if _, err := w.Write(b[:1]); err != nil {
223224
return err
224225
}
225-
if _, err := w.Write(h.Coinbase.Bytes()); err != nil {
226+
if _, err := w.Write(h.Coinbase[:]); err != nil {
226227
return err
227228
}
228229
b[0] = 128 + 32
229230
if _, err := w.Write(b[:1]); err != nil {
230231
return err
231232
}
232-
if _, err := w.Write(h.Root.Bytes()); err != nil {
233+
if _, err := w.Write(h.Root[:]); err != nil {
233234
return err
234235
}
235236
if _, err := w.Write(b[:1]); err != nil {
236237
return err
237238
}
238-
if _, err := w.Write(h.TxHash.Bytes()); err != nil {
239+
if _, err := w.Write(h.TxHash[:]); err != nil {
239240
return err
240241
}
241242
if _, err := w.Write(b[:1]); err != nil {
242243
return err
243244
}
244-
if _, err := w.Write(h.ReceiptHash.Bytes()); err != nil {
245+
if _, err := w.Write(h.ReceiptHash[:]); err != nil {
245246
return err
246247
}
247248
b[0] = 183 + 2
@@ -250,7 +251,7 @@ func (h *Header) EncodeRLP(w io.Writer) error {
250251
if _, err := w.Write(b[:3]); err != nil {
251252
return err
252253
}
253-
if _, err := w.Write(h.Bloom.Bytes()); err != nil {
254+
if _, err := w.Write(h.Bloom[:]); err != nil {
254255
return err
255256
}
256257
if err := rlp.EncodeBigInt(h.Difficulty, w, b[:]); err != nil {
@@ -284,7 +285,7 @@ func (h *Header) EncodeRLP(w io.Writer) error {
284285
if _, err := w.Write(b[:1]); err != nil {
285286
return err
286287
}
287-
if _, err := w.Write(h.MixDigest.Bytes()); err != nil {
288+
if _, err := w.Write(h.MixDigest[:]); err != nil {
288289
return err
289290
}
290291
b[0] = 128 + 8
@@ -307,7 +308,7 @@ func (h *Header) EncodeRLP(w io.Writer) error {
307308
if _, err := w.Write(b[:1]); err != nil {
308309
return err
309310
}
310-
if _, err := w.Write(h.WithdrawalsHash.Bytes()); err != nil {
311+
if _, err := w.Write(h.WithdrawalsHash[:]); err != nil {
311312
return err
312313
}
313314
}
@@ -328,7 +329,7 @@ func (h *Header) EncodeRLP(w io.Writer) error {
328329
if _, err := w.Write(b[:1]); err != nil {
329330
return err
330331
}
331-
if _, err := w.Write(h.ParentBeaconBlockRoot.Bytes()); err != nil {
332+
if _, err := w.Write(h.ParentBeaconBlockRoot[:]); err != nil {
332333
return err
333334
}
334335
}
@@ -338,7 +339,7 @@ func (h *Header) EncodeRLP(w io.Writer) error {
338339
if _, err := w.Write(b[:1]); err != nil {
339340
return err
340341
}
341-
if _, err := w.Write(h.RequestsHash.Bytes()); err != nil {
342+
if _, err := w.Write(h.RequestsHash[:]); err != nil {
342343
return err
343344
}
344345
}
@@ -788,7 +789,8 @@ func (rb RawBody) payloadSize() (payloadSize, txsLen, unclesLen, withdrawalsLen
788789

789790
func (rb RawBody) EncodeRLP(w io.Writer) error {
790791
payloadSize, txsLen, unclesLen, withdrawalsLen := rb.payloadSize()
791-
var b [33]byte
792+
b := newEncodingBuf()
793+
defer pooledBuf.Put(b)
792794
// prefix
793795
if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil {
794796
return err
@@ -873,7 +875,8 @@ func (bfs BodyForStorage) payloadSize() (payloadSize, unclesLen, withdrawalsLen
873875

874876
func (bfs BodyForStorage) EncodeRLP(w io.Writer) error {
875877
payloadSize, unclesLen, withdrawalsLen := bfs.payloadSize()
876-
var b [33]byte
878+
b := newEncodingBuf()
879+
defer pooledBuf.Put(b)
877880

878881
// prefix
879882
if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil {
@@ -956,7 +959,9 @@ func (bb Body) payloadSize() (payloadSize int, txsLen, unclesLen, withdrawalsLen
956959

957960
func (bb Body) EncodeRLP(w io.Writer) error {
958961
payloadSize, txsLen, unclesLen, withdrawalsLen := bb.payloadSize()
959-
var b [33]byte
962+
963+
b := newEncodingBuf()
964+
defer pooledBuf.Put(b)
960965
// prefix
961966
if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil {
962967
return err
@@ -1201,7 +1206,9 @@ func (bb *Block) EncodingSize() int {
12011206
// EncodeRLP serializes b into the Ethereum RLP block format.
12021207
func (bb *Block) EncodeRLP(w io.Writer) error {
12031208
payloadSize, txsLen, unclesLen, withdrawalsLen := bb.payloadSize()
1204-
var b [33]byte
1209+
1210+
b := newEncodingBuf()
1211+
defer pooledBuf.Put(b)
12051212
// prefix
12061213
if err := EncodeStructSizePrefix(payloadSize, w, b[:]); err != nil {
12071214
return err

0 commit comments

Comments
 (0)