Skip to content

Commit 95872fc

Browse files
DieBauerfstab
authored andcommitted
Extract upperbound calculation
The upperbound is constant for a given quantile. By calculating this once we save O(iterator size * quantiles size) calculations of this constant. Signed-off-by: Jens <jenskat@gmail.com>
1 parent c16d4ae commit 95872fc

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

benchmarks/src/main/java/io/prometheus/client/CKMSQuantileBenchmark.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public void setup() {
9797
for (Double l : shuffle) {
9898
ckmsQuantiles.insert(l);
9999
}
100+
// make sure we inserted all 'hanging' samples (count % 128)
101+
ckmsQuantiles.get(0);
102+
// compress everything so we have a similar samples size regardless of n.
103+
ckmsQuantiles.compress();
100104
System.out.println("Sample size is: " + ckmsQuantiles.samples.size());
101105
}
102106

simpleclient/src/main/java/io/prometheus/client/CKMSQuantiles.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ public double get(double q) {
152152

153153
int r = 0; // sum of g's left of the current sample
154154
int desiredRank = (int) Math.ceil(q * n);
155+
int upperBound = desiredRank + f(desiredRank) / 2;
155156

156157
ListIterator<Sample> iterator = samples.listIterator();
157158
while (iterator.hasNext()) {
158159
Sample sample = iterator.next();
159-
if (r + sample.g + sample.delta > desiredRank + f(desiredRank) / 2) {
160+
if (r + sample.g + sample.delta > upperBound) {
160161
iterator.previous(); // roll back the item.next() above
161162
if (iterator.hasPrevious()) {
162163
Sample result = iterator.previous();

0 commit comments

Comments
 (0)