From dce9c0c369bdaaab0d48808ea6032461224c66ff Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Sun, 21 Jan 2024 05:40:29 +0800 Subject: [PATCH 01/10] remove a redundant heap Init --- core/txpool/legacypool/list.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 05ae0b58cd5..2b3bde63849 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -166,7 +166,6 @@ func (m *sortedMap) Cap(threshold int) types.Transactions { delete(m.items, (*m.index)[size-1]) } *m.index = (*m.index)[:threshold] - heap.Init(m.index) // If we had a cache, shift the back m.cacheMu.Lock() From d5ae16c4868e802a33c07ff07502636b62d73577 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Tue, 13 Feb 2024 19:09:44 +0800 Subject: [PATCH 02/10] add a benchmark test for list.Cap --- core/txpool/legacypool/list_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/txpool/legacypool/list_test.go b/core/txpool/legacypool/list_test.go index b5cd34b23b6..59ee082bdba 100644 --- a/core/txpool/legacypool/list_test.go +++ b/core/txpool/legacypool/list_test.go @@ -70,3 +70,25 @@ func BenchmarkListAdd(b *testing.B) { } } } + +func BenchmarkListCapOneTx(b *testing.B) { + // Generate a list of transactions to insert + key, _ := crypto.GenerateKey() + + txs := make(types.Transactions, 32) + for i := 0; i < len(txs); i++ { + txs[i] = transaction(uint64(i), 0, key) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + list := newList(true) + // Insert the transactions in a random order + for _, v := range rand.Perm(len(txs)) { + list.Add(txs[v], DefaultConfig.PriceBump) + } + b.StartTimer() + list.Cap(list.Len() - 1) + b.StopTimer() + } +} From e8b23da4cd6c1e4e8fd6fdb239bc486ba2debf5b Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Tue, 13 Feb 2024 21:35:00 +0800 Subject: [PATCH 03/10] refactor Cap to use heap.Pop() instead of sorting the whole heap --- core/txpool/legacypool/list.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 2b3bde63849..de7db2ac498 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -160,12 +160,11 @@ func (m *sortedMap) Cap(threshold int) types.Transactions { // Otherwise gather and drop the highest nonce'd transactions var drops types.Transactions - sort.Sort(*m.index) for size := len(m.items); size > threshold; size-- { - drops = append(drops, m.items[(*m.index)[size-1]]) - delete(m.items, (*m.index)[size-1]) + dropIdx := m.index.Pop().(uint64) + drops = append(drops, m.items[dropIdx]) + delete(m.items, dropIdx) } - *m.index = (*m.index)[:threshold] // If we had a cache, shift the back m.cacheMu.Lock() From 2b9e72c7bf56ba98b0876883f2c6a2c90a832e75 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Tue, 13 Feb 2024 21:43:44 +0800 Subject: [PATCH 04/10] Revert "refactor Cap to use heap.Pop() instead of sorting the whole heap" This reverts commit e8b23da4cd6c1e4e8fd6fdb239bc486ba2debf5b. --- core/txpool/legacypool/list.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index de7db2ac498..2b3bde63849 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -160,11 +160,12 @@ func (m *sortedMap) Cap(threshold int) types.Transactions { // Otherwise gather and drop the highest nonce'd transactions var drops types.Transactions + sort.Sort(*m.index) for size := len(m.items); size > threshold; size-- { - dropIdx := m.index.Pop().(uint64) - drops = append(drops, m.items[dropIdx]) - delete(m.items, dropIdx) + drops = append(drops, m.items[(*m.index)[size-1]]) + delete(m.items, (*m.index)[size-1]) } + *m.index = (*m.index)[:threshold] // If we had a cache, shift the back m.cacheMu.Lock() From 7f5b1474eb4f019441dd8587340fa13b3605754c Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Tue, 13 Feb 2024 21:49:13 +0800 Subject: [PATCH 05/10] add a comment --- core/txpool/legacypool/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 2b3bde63849..72e86c60119 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -160,7 +160,7 @@ func (m *sortedMap) Cap(threshold int) types.Transactions { // Otherwise gather and drop the highest nonce'd transactions var drops types.Transactions - sort.Sort(*m.index) + sort.Sort(*m.index) // the sorted array is a heap, so no need to reheap after deleting tail items for size := len(m.items); size > threshold; size-- { drops = append(drops, m.items[(*m.index)[size-1]]) delete(m.items, (*m.index)[size-1]) From 26b8affead6c550111f8af83cb2279a702d7365a Mon Sep 17 00:00:00 2001 From: colin <102356659+colinlyguo@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:20:15 +0800 Subject: [PATCH 06/10] Update core/txpool/legacypool/list.go Co-authored-by: Martin HS --- core/txpool/legacypool/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 72e86c60119..89188e4330b 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -160,7 +160,7 @@ func (m *sortedMap) Cap(threshold int) types.Transactions { // Otherwise gather and drop the highest nonce'd transactions var drops types.Transactions - sort.Sort(*m.index) // the sorted array is a heap, so no need to reheap after deleting tail items + slices.Sort(*m.index) // the sorted array is a heap, so no need to reheap after deleting tail items for size := len(m.items); size > threshold; size-- { drops = append(drops, m.items[(*m.index)[size-1]]) delete(m.items, (*m.index)[size-1]) From 51e875ee9da46791b86d7a1ec2e0b0b19e83a832 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Thu, 15 Feb 2024 17:22:24 +0800 Subject: [PATCH 07/10] fix compiling --- core/txpool/legacypool/list.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 89188e4330b..2be1c0fa9d2 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -20,6 +20,7 @@ import ( "container/heap" "math" "math/big" + "slices" "sort" "sync" "sync/atomic" From 2fd4f2c7b785e40c024afb31f4b8f1b50404a3e9 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 15 Feb 2024 11:46:36 +0100 Subject: [PATCH 08/10] Update list.go --- core/txpool/legacypool/list.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 2be1c0fa9d2..ba2c829c404 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -160,13 +160,14 @@ func (m *sortedMap) Cap(threshold int) types.Transactions { } // Otherwise gather and drop the highest nonce'd transactions var drops types.Transactions - - slices.Sort(*m.index) // the sorted array is a heap, so no need to reheap after deleting tail items + slices.Sort(*m.index) for size := len(m.items); size > threshold; size-- { drops = append(drops, m.items[(*m.index)[size-1]]) delete(m.items, (*m.index)[size-1]) } *m.index = (*m.index)[:threshold] + // The sorted m.index slice is still a valid heap, so there is no need to + // reheap after deleting tail items. // If we had a cache, shift the back m.cacheMu.Lock() From 3af07c669d8332392169a60fbbbfc4fa9aeb52d6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 15 Feb 2024 11:47:35 +0100 Subject: [PATCH 09/10] Update list.go --- core/txpool/legacypool/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index ba2c829c404..c6f4d3767a8 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -20,7 +20,6 @@ import ( "container/heap" "math" "math/big" - "slices" "sort" "sync" "sync/atomic" @@ -28,6 +27,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "golang.org/x/exp/slices" ) // nonceHeap is a heap.Interface implementation over 64bit unsigned integers for From 84bc4d457fdedc809036e4440041205addd7da2e Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 15 Feb 2024 14:09:48 +0100 Subject: [PATCH 10/10] core/txpool/legacypool: goimports fix --- core/txpool/legacypool/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/legacypool/list.go b/core/txpool/legacypool/list.go index 8c52ff4ef69..f0f9f213f27 100644 --- a/core/txpool/legacypool/list.go +++ b/core/txpool/legacypool/list.go @@ -27,7 +27,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/holiman/uint256" + "github.com/holiman/uint256" "golang.org/x/exp/slices" )