From 8cbf9a0b440b6a60090b29001f67052fcaa4d16a Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 6 Feb 2024 10:19:51 +0300 Subject: [PATCH] [#957] treesvc: Use sorting when number of items is small Signed-off-by: Evgenii Stratonikov --- pkg/services/tree/bench_test.go | 2 +- pkg/services/tree/service.go | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pkg/services/tree/bench_test.go b/pkg/services/tree/bench_test.go index 1d2512d03..12325b99f 100644 --- a/pkg/services/tree/bench_test.go +++ b/pkg/services/tree/bench_test.go @@ -13,7 +13,7 @@ import ( ) func BenchmarkGetSubTree(b *testing.B) { - const count = 100_000 + const count = simpleHeapFallbackThreshold d := pilorama.CIDDescriptor{CID: cidtest.ID(), Size: 1} treeID := "sometree" diff --git a/pkg/services/tree/service.go b/pkg/services/tree/service.go index 33ac3ef98..2f2479edc 100644 --- a/pkg/services/tree/service.go +++ b/pkg/services/tree/service.go @@ -446,11 +446,19 @@ type Heap interface { ExtractMin() pilorama.NodeInfo } -func makeHeap(ordered bool) Heap { - if ordered { +// simpleHeapFallbackThreshold is the least number of nodes on a single level, +// 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() + 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 { @@ -466,7 +474,7 @@ func getSubTree(ctx context.Context, srv TreeService_GetSubTreeServer, cid cidSD return err } - stack := []Heap{makeHeap(ordered)} + stack := []Heap{makeHeap(ordered, 1)} stack[0].Insert(pilorama.NodeInfo{ ID: b.GetRootId(), Meta: m, @@ -501,7 +509,7 @@ func getSubTree(ctx context.Context, srv TreeService_GetSubTreeServer, cid cidSD return err } if len(children) != 0 { - h := makeHeap(ordered) + h := makeHeap(ordered, len(children)) h.Insert(children...) stack = append(stack, h) }