[#957] treesvc: Use sorting when number of items is small

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-02-06 10:19:51 +03:00
parent d18eaf060d
commit 8cbf9a0b44
2 changed files with 14 additions and 6 deletions

View file

@ -13,7 +13,7 @@ import (
) )
func BenchmarkGetSubTree(b *testing.B) { func BenchmarkGetSubTree(b *testing.B) {
const count = 100_000 const count = simpleHeapFallbackThreshold
d := pilorama.CIDDescriptor{CID: cidtest.ID(), Size: 1} d := pilorama.CIDDescriptor{CID: cidtest.ID(), Size: 1}
treeID := "sometree" treeID := "sometree"

View file

@ -446,11 +446,19 @@ type Heap interface {
ExtractMin() pilorama.NodeInfo ExtractMin() pilorama.NodeInfo
} }
func makeHeap(ordered bool) Heap { // simpleHeapFallbackThreshold is the least number of nodes on a single level,
if ordered { // for which pairing heap implementation is used.
const simpleHeapFallbackThreshold = 100_000
func makeHeap(ordered bool, count int) Heap {
switch {
case ordered && simpleHeapFallbackThreshold <= count:
return heap.NewPairing() return heap.NewPairing()
case ordered:
return heap.NewOrderedSlice()
default:
return heap.NewUnorderedSlice()
} }
return heap.NewUnorderedSlice()
} }
func getSubTree(ctx context.Context, srv TreeService_GetSubTreeServer, cid cidSDK.ID, b *GetSubTreeRequest_Body, forest pilorama.Forest) error { func getSubTree(ctx context.Context, srv TreeService_GetSubTreeServer, cid cidSDK.ID, b *GetSubTreeRequest_Body, forest pilorama.Forest) error {
@ -466,7 +474,7 @@ func getSubTree(ctx context.Context, srv TreeService_GetSubTreeServer, cid cidSD
return err return err
} }
stack := []Heap{makeHeap(ordered)} stack := []Heap{makeHeap(ordered, 1)}
stack[0].Insert(pilorama.NodeInfo{ stack[0].Insert(pilorama.NodeInfo{
ID: b.GetRootId(), ID: b.GetRootId(),
Meta: m, Meta: m,
@ -501,7 +509,7 @@ func getSubTree(ctx context.Context, srv TreeService_GetSubTreeServer, cid cidSD
return err return err
} }
if len(children) != 0 { if len(children) != 0 {
h := makeHeap(ordered) h := makeHeap(ordered, len(children))
h.Insert(children...) h.Insert(children...)
stack = append(stack, h) stack = append(stack, h)
} }