From b94bdf94d04517f32eeda648a06fb07106e54a2c Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 6 Aug 2024 19:38:17 +0200 Subject: [PATCH] Add par sort slice --- store/cachekv/store.go | 10 ++++++++-- store/go.mod | 3 +++ store/go.sum | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/store/cachekv/store.go b/store/cachekv/store.go index 08cfc2b325f6..8e3ad98bf15c 100644 --- a/store/cachekv/store.go +++ b/store/cachekv/store.go @@ -14,6 +14,7 @@ import ( "cosmossdk.io/store/internal/kv" "cosmossdk.io/store/tracekv" "cosmossdk.io/store/types" + "github.com/jfcg/sorty/v2" ) // cValue represents a cached value. @@ -380,9 +381,14 @@ func (store *Store) clearUnsortedCacheSubset(unsorted []*kv.Pair, sortState sort } if sortState == stateUnsorted { - sort.Slice(unsorted, func(i, j int) bool { + fn := func(i, j int) bool { return bytes.Compare(unsorted[i].Key, unsorted[j].Key) < 0 - }) + } + if len(unsorted) > 1000 { + sort.Slice(unsorted, fn) + } else { + sorty.SortSlice(unsorted) + } } for _, item := range unsorted { diff --git a/store/go.mod b/store/go.mod index 4c381e31397f..e15d76d043fb 100644 --- a/store/go.mod +++ b/store/go.mod @@ -26,6 +26,8 @@ require ( gotest.tools/v3 v3.5.1 ) +require github.com/jfcg/sorty/v2 v2.1.1 + require ( github.com/DataDog/zstd v1.5.5 // indirect github.com/beorn7/perks v1.0.1 // indirect @@ -48,6 +50,7 @@ require ( github.com/hashicorp/go-immutable-radix v1.0.0 // indirect github.com/hashicorp/go-uuid v1.0.1 // indirect github.com/hashicorp/yamux v0.1.1 // indirect + github.com/jfcg/sixb v1.4.1 // indirect github.com/jhump/protoreflect v1.15.3 // indirect github.com/klauspost/compress v1.17.7 // indirect github.com/kr/pretty v0.3.1 // indirect diff --git a/store/go.sum b/store/go.sum index 360753608a69..8b3f23bef83d 100644 --- a/store/go.sum +++ b/store/go.sum @@ -136,6 +136,14 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jfcg/opt v0.3.1 h1:6zgKvv3fR5OlX2nxUYJC4wtosY30N4vypILgXmRNr34= +github.com/jfcg/opt v0.3.1/go.mod h1:3ZUYQhiqKM6vVjMRYV1fVZ9a91EQ47b5kg7KsnfRClk= +github.com/jfcg/rng v1.0.6 h1:JCYvI/GaSSd3lL0zl15J7FNHqMZYNosPmNKDrysKhH0= +github.com/jfcg/rng v1.0.6/go.mod h1:UqYFfcn9XCugyejaC+8cXQgrWtHQtvlWGiLQXu/7Cnw= +github.com/jfcg/sixb v1.4.1 h1:lb/fWXTn7G+Om2K2/NhZByppgPyQ7mWyQ5XOHu9PAfU= +github.com/jfcg/sixb v1.4.1/go.mod h1:hofNeC6Ua8uwQ7X14L2/byXF1xJyUyX4lk53SmeAopI= +github.com/jfcg/sorty/v2 v2.1.1 h1:jMgkME/JZ4dVFxOVtAeQUXbSzLhbPGxjgfhtnCMXXVM= +github.com/jfcg/sorty/v2 v2.1.1/go.mod h1:wFv8kNl8smeqwsx62BPpgxyjxMY+4ylIug1ARe4nLnI= github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls= github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=