mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-20 15:16:42 +00:00
app/vmselect/netstorage: optimize mergeSortBlocks() for the worst case when blocks contain interleaved samples
This commit is contained in:
parent
aee08117e9
commit
fbb403b5c0
1 changed files with 19 additions and 4 deletions
|
@ -514,14 +514,13 @@ func mergeSortBlocks(dst *Result, sbh sortBlocksHeap, dedupInterval int64) {
|
|||
heap.Init(&sbh)
|
||||
for {
|
||||
top := sbh[0]
|
||||
heap.Pop(&sbh)
|
||||
if len(sbh) == 0 {
|
||||
if len(sbh) == 1 {
|
||||
dst.Timestamps = append(dst.Timestamps, top.Timestamps[top.NextIdx:]...)
|
||||
dst.Values = append(dst.Values, top.Values[top.NextIdx:]...)
|
||||
putSortBlock(top)
|
||||
break
|
||||
}
|
||||
sbNext := sbh[0]
|
||||
sbNext := sbh.getNextBlock()
|
||||
tsNext := sbNext.Timestamps[sbNext.NextIdx]
|
||||
topTimestamps := top.Timestamps
|
||||
topNextIdx := top.NextIdx
|
||||
|
@ -535,8 +534,9 @@ func mergeSortBlocks(dst *Result, sbh sortBlocksHeap, dedupInterval int64) {
|
|||
dst.Values = append(dst.Values, top.Values[topNextIdx:top.NextIdx]...)
|
||||
}
|
||||
if top.NextIdx < len(topTimestamps) {
|
||||
heap.Push(&sbh, top)
|
||||
heap.Fix(&sbh, 0)
|
||||
} else {
|
||||
heap.Pop(&sbh)
|
||||
putSortBlock(top)
|
||||
}
|
||||
}
|
||||
|
@ -603,6 +603,21 @@ func (sb *sortBlock) unpackFrom(tmpBlock *storage.Block, tbf *tmpBlocksFile, add
|
|||
|
||||
type sortBlocksHeap []*sortBlock
|
||||
|
||||
func (sbh sortBlocksHeap) getNextBlock() *sortBlock {
|
||||
if len(sbh) < 2 {
|
||||
return nil
|
||||
}
|
||||
if len(sbh) < 3 {
|
||||
return sbh[1]
|
||||
}
|
||||
a := sbh[1]
|
||||
b := sbh[2]
|
||||
if a.Timestamps[a.NextIdx] <= b.Timestamps[b.NextIdx] {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (sbh sortBlocksHeap) Len() int {
|
||||
return len(sbh)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue