mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-11 15:34:56 +00:00
app/vmselect/netstorage: optimize mergeSortBlocks() for the worst case when blocks contain interleaved samples
This commit is contained in:
parent
8429d4af5a
commit
159c2e15e3
1 changed files with 19 additions and 4 deletions
|
@ -507,14 +507,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
|
||||
|
@ -528,8 +527,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)
|
||||
}
|
||||
}
|
||||
|
@ -597,6 +597,21 @@ func (sb *sortBlock) unpackFrom(tmpBlock *storage.Block, tbf *tmpBlocksFile, br
|
|||
|
||||
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