mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-12 16:44:05 -08:00
Merge pull request #13212 from linasm/optimize-floatBucketIterator-for-common-case
Histograms: optimize floatBucketIterator for common case
This commit is contained in:
commit
de1ec731c7
|
@ -767,6 +767,7 @@ func (h *FloatHistogram) floatBucketIterator(
|
||||||
},
|
},
|
||||||
targetSchema: targetSchema,
|
targetSchema: targetSchema,
|
||||||
absoluteStartValue: absoluteStartValue,
|
absoluteStartValue: absoluteStartValue,
|
||||||
|
boundReachedStartValue: absoluteStartValue == 0,
|
||||||
}
|
}
|
||||||
if positive {
|
if positive {
|
||||||
i.spans = h.PositiveSpans
|
i.spans = h.PositiveSpans
|
||||||
|
@ -824,6 +825,32 @@ func (i *floatBucketIterator) Next() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if i.schema == i.targetSchema {
|
||||||
|
// Fast path for the common case.
|
||||||
|
span := i.spans[i.spansIdx]
|
||||||
|
if i.bucketsIdx == 0 {
|
||||||
|
// Seed origIdx for the first bucket.
|
||||||
|
i.currIdx = span.Offset
|
||||||
|
} else {
|
||||||
|
i.currIdx++
|
||||||
|
}
|
||||||
|
|
||||||
|
for i.idxInSpan >= span.Length {
|
||||||
|
// We have exhausted the current span and have to find a new
|
||||||
|
// one. We even handle pathologic spans of length 0 here.
|
||||||
|
i.idxInSpan = 0
|
||||||
|
i.spansIdx++
|
||||||
|
if i.spansIdx >= len(i.spans) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
span = i.spans[i.spansIdx]
|
||||||
|
i.currIdx += span.Offset
|
||||||
|
}
|
||||||
|
|
||||||
|
i.currCount = i.buckets[i.bucketsIdx]
|
||||||
|
i.idxInSpan++
|
||||||
|
i.bucketsIdx++
|
||||||
|
} else {
|
||||||
// Copy all of these into local variables so that we can forward to the
|
// Copy all of these into local variables so that we can forward to the
|
||||||
// next bucket and then roll back if needed.
|
// next bucket and then roll back if needed.
|
||||||
origIdx, spansIdx, idxInSpan := i.origIdx, i.spansIdx, i.idxInSpan
|
origIdx, spansIdx, idxInSpan := i.origIdx, i.spansIdx, i.idxInSpan
|
||||||
|
@ -853,7 +880,7 @@ mergeLoop: // Merge together all buckets from the original schema that fall into
|
||||||
span = i.spans[spansIdx]
|
span = i.spans[spansIdx]
|
||||||
origIdx += span.Offset
|
origIdx += span.Offset
|
||||||
}
|
}
|
||||||
currIdx := i.targetIdx(origIdx)
|
currIdx := targetIdx(origIdx, i.schema, i.targetSchema)
|
||||||
switch {
|
switch {
|
||||||
case firstPass:
|
case firstPass:
|
||||||
i.currIdx = currIdx
|
i.currIdx = currIdx
|
||||||
|
@ -873,6 +900,8 @@ mergeLoop: // Merge together all buckets from the original schema that fall into
|
||||||
break mergeLoop
|
break mergeLoop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Skip buckets before absoluteStartValue.
|
// Skip buckets before absoluteStartValue.
|
||||||
// TODO(beorn7): Maybe do something more efficient than this recursive call.
|
// TODO(beorn7): Maybe do something more efficient than this recursive call.
|
||||||
if !i.boundReachedStartValue && getBound(i.currIdx, i.targetSchema) <= i.absoluteStartValue {
|
if !i.boundReachedStartValue && getBound(i.currIdx, i.targetSchema) <= i.absoluteStartValue {
|
||||||
|
@ -882,17 +911,6 @@ mergeLoop: // Merge together all buckets from the original schema that fall into
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// targetIdx returns the bucket index within i.targetSchema for the given bucket
|
|
||||||
// index within i.schema.
|
|
||||||
func (i *floatBucketIterator) targetIdx(idx int32) int32 {
|
|
||||||
if i.schema == i.targetSchema {
|
|
||||||
// Fast path for the common case. The below would yield the same
|
|
||||||
// result, just with more effort.
|
|
||||||
return idx
|
|
||||||
}
|
|
||||||
return ((idx - 1) >> (i.schema - i.targetSchema)) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
type reverseFloatBucketIterator struct {
|
type reverseFloatBucketIterator struct {
|
||||||
baseBucketIterator[float64, float64]
|
baseBucketIterator[float64, float64]
|
||||||
idxInSpan int32 // Changed from uint32 to allow negative values for exhaustion detection.
|
idxInSpan int32 // Changed from uint32 to allow negative values for exhaustion detection.
|
||||||
|
|
Loading…
Reference in a new issue