From 0518efb4c39cd62fbc67f60037ac22a63932eed2 Mon Sep 17 00:00:00 2001 From: rian Date: Thu, 19 Dec 2024 16:44:17 +0200 Subject: [PATCH] some heap optimisations --- mempool/mempool.go | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/mempool/mempool.go b/mempool/mempool.go index 6367fb0fb1..04d5fcca3e 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -3,7 +3,6 @@ package mempool import ( "encoding/binary" "errors" - "fmt" "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" @@ -51,22 +50,6 @@ func (p *Pool) WithValidator(validator ValidatorFunc) *Pool { return p } -func (p *Pool) rejectDuplicateTxn(userTxn *BroadcastedTransaction) error { - txHash := userTxn.Transaction.Hash().Marshal() - err := p.db.View(func(txn db.Transaction) error { - return txn.Get(txHash, func(val []byte) error { - if val != nil { - return fmt.Errorf("transaction already exists in the mempool: %x", txHash) - } - return nil - }) - }) - if errors.Is(err, db.ErrKeyNotFound) { - return nil - } - return err -} - // Push queues a transaction to the pool func (p *Pool) Push(userTxn *BroadcastedTransaction) error { err := p.validator(userTxn) @@ -74,13 +57,9 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error { return err } - err = p.rejectDuplicateTxn(userTxn) - if err != nil { - return err - } - if err := p.db.Update(func(txn db.Transaction) error { - tail, err := p.tailHash(txn) + var tail *felt.Felt + tail, err := p.tailHash(txn, tail) if err != nil && !errors.Is(err, db.ErrKeyNotFound) { return err } @@ -90,7 +69,6 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error { }); err != nil { return err } - fmt.Println("tail", tail) if tail != nil { var oldTail storageElem oldTail, err = p.elem(txn, tail) @@ -135,7 +113,8 @@ func (p *Pool) Push(userTxn *BroadcastedTransaction) error { func (p *Pool) Pop() (BroadcastedTransaction, error) { var nextTxn BroadcastedTransaction return nextTxn, p.db.Update(func(txn db.Transaction) error { - headHash, err := p.headHash(txn) + var headHash *felt.Felt + headHash, err := p.headHash(txn, headHash) if err != nil { return err } @@ -212,10 +191,9 @@ func (p *Pool) updateLen(txn db.Transaction, l uint64) error { return txn.Set([]byte(poolLengthKey), binary.BigEndian.AppendUint64(nil, l)) } -func (p *Pool) headHash(txn db.Transaction) (*felt.Felt, error) { - var head *felt.Felt - return head, txn.Get([]byte(headKey), func(b []byte) error { - head = new(felt.Felt).SetBytes(b) +func (p *Pool) headHash(txn db.Transaction, headHash *felt.Felt) (*felt.Felt, error) { + return headHash, txn.Get([]byte(headKey), func(b []byte) error { + headHash = headHash.SetBytes(b) return nil }) } @@ -224,10 +202,9 @@ func (p *Pool) updateHead(txn db.Transaction, head *felt.Felt) error { return txn.Set([]byte(headKey), head.Marshal()) } -func (p *Pool) tailHash(txn db.Transaction) (*felt.Felt, error) { - var tail *felt.Felt +func (p *Pool) tailHash(txn db.Transaction, tail *felt.Felt) (*felt.Felt, error) { return tail, txn.Get([]byte(tailKey), func(b []byte) error { - tail = new(felt.Felt).SetBytes(b) + tail = tail.SetBytes(b) return nil }) }