Skip to content

Commit 4400685

Browse files
committed
Use thread-local randoms.
1 parent ac8f22a commit 4400685

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

src/main/java/org/elasticsearch/search/aggregations/metrics/percentile/frugal/Frugal.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.elasticsearch.search.aggregations.metrics.percentile.frugal;
22

3+
import jsr166y.ThreadLocalRandom;
34
import org.apache.lucene.util.OpenBitSet;
45
import org.apache.lucene.util.RamUsageEstimator;
56
import org.elasticsearch.common.io.stream.StreamInput;
@@ -25,7 +26,7 @@ public class Frugal extends InternalPercentiles.Estimator<Frugal> {
2526
// if we wanted to do it right... we'd need an "OpenRandom" class where we have access to the current seed
2627
// and the serialize the seed as well. In our context, it doesn't matter much as the rand is not used once
2728
// this commulate is transferred (the rand is no used in the reduce phase)
28-
this.rand = new Random();
29+
this.rand = ThreadLocalRandom.current();
2930
}
3031

3132
/**
@@ -48,7 +49,7 @@ public Frugal(double[] percents) {
4849
this.signs = new OpenBitSet(percents.length);
4950
this.signs.set(0, percents.length);
5051
Arrays.fill(this.steps, 1);
51-
this.rand = new Random();
52+
this.rand = ThreadLocalRandom.current();
5253
}
5354

5455
@Override
@@ -71,12 +72,13 @@ public void offer(double value) {
7172
return;
7273
}
7374

75+
final double randomValue = rand.nextDouble() * 100;
7476
for (int i = 0 ; i < percents.length; ++i) {
75-
offerTo(i, value);
77+
offerTo(i, value, randomValue);
7678
}
7779
}
7880

79-
private void offerTo(int index, double value) {
81+
private void offerTo(int index, double value, double randomValue) {
8082

8183
double percent = this.percents[index];
8284

@@ -92,8 +94,6 @@ private void offerTo(int index, double value) {
9294
return;
9395
}
9496

95-
final double randomValue = this.rand.nextDouble() * 100.0d;
96-
9797
if (value > estimates[index] && randomValue > (100.0d - percent)) {
9898
steps[index] += signs.get(index) ? 1 : -1;
9999

src/main/java/org/elasticsearch/search/aggregations/metrics/percentile/frugal/QuickSelect.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package org.elasticsearch.search.aggregations.metrics.percentile.frugal;
22

33

4+
import jsr166y.ThreadLocalRandom;
5+
46
import java.util.Random;
57

68
public class QuickSelect {
79

8-
private static final Random rand = new Random();
9-
1010
public static double quickSelect(double[] list, int left, int right, int k) {
1111

1212
if (left == right) {
1313
return list[left];
1414
}
1515

16+
final Random rand = ThreadLocalRandom.current();
17+
1618
while (true) {
1719
int pivot = left + rand.nextInt(right - left + 1);
1820

src/main/java/org/elasticsearch/search/aggregations/metrics/percentile/tdigest/TDigestState.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@
2222
* https://github.com/addthis/stream-lib/blob/master/src/main/java/com/clearspring/analytics/stream/quantile/TDigest.java
2323
*/
2424

25+
import com.google.common.base.Preconditions;
26+
import com.google.common.collect.Lists;
27+
import jsr166y.ThreadLocalRandom;
28+
import org.elasticsearch.common.io.stream.StreamInput;
29+
import org.elasticsearch.common.io.stream.StreamOutput;
30+
2531
import java.io.IOException;
2632
import java.nio.ByteBuffer;
2733
import java.util.Collections;
2834
import java.util.Iterator;
2935
import java.util.List;
3036
import java.util.Random;
3137
import java.util.concurrent.atomic.AtomicInteger;
32-
import com.google.common.base.Preconditions;
33-
import com.google.common.collect.Lists;
34-
import com.sun.jna.Structure;
35-
import org.elasticsearch.common.io.stream.StreamInput;
36-
import org.elasticsearch.common.io.stream.StreamOutput;
3738

3839

3940
/**
@@ -73,12 +74,8 @@ public class TDigestState {
7374
* accuracy.
7475
*/
7576
public TDigestState(double compression) {
76-
this(compression, new Random());
77-
}
78-
79-
public TDigestState(double compression, Random random) {
8077
this.compression = compression;
81-
gen = random;
78+
gen = ThreadLocalRandom.current();
8279
}
8380

8481
/**
@@ -184,7 +181,7 @@ public static TDigestState merge(double compression, Iterable<TDigestState> subD
184181
Preconditions.checkArgument(subData.iterator().hasNext(), "Can't merge 0 digests");
185182
List<TDigestState> elements = Lists.newArrayList(subData);
186183
int n = Math.max(1, elements.size() / 4);
187-
TDigestState r = new TDigestState(compression, elements.get(0).gen);
184+
TDigestState r = new TDigestState(compression);
188185
if (elements.get(0).recordAllData) {
189186
r.recordAllData();
190187
}
@@ -203,7 +200,7 @@ public void compress() {
203200
}
204201

205202
private void compress(GroupTree other) {
206-
TDigestState reduced = new TDigestState(compression, gen);
203+
TDigestState reduced = new TDigestState(compression);
207204
if (recordAllData) {
208205
reduced.recordAllData();
209206
}

0 commit comments

Comments
 (0)