From 80aa4db2f187a468d7fddaf09db1501b55a830c2 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Thu, 24 Jan 2013 08:15:45 +0000 Subject: [PATCH] improve RepeatableSampler's inner PQ git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1437885 13f79535-47bb-0310-9956-ffa450edef68 --- .../search/sampling/RepeatableSampler.java | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/sampling/RepeatableSampler.java b/lucene/facet/src/java/org/apache/lucene/facet/search/sampling/RepeatableSampler.java index 30bfe2673fd..892d2fece1c 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/search/sampling/RepeatableSampler.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/search/sampling/RepeatableSampler.java @@ -279,8 +279,13 @@ public class RepeatableSampler extends Sampler { * into a bounded PQ (retains only sampleSize highest weights). */ ScoredDocIDsIterator it = collection.iterator(); + MI mi = null; while (it.next()) { - pq.insertWithReuse((int)(it.getDocID() * PHI_32) & 0x7FFFFFFF); + if (mi == null) { + mi = new MI(); + } + mi.value = (int) (it.getDocID() * PHI_32) & 0x7FFFFFFF; + mi = pq.insertWithOverflow(mi); } if (returnTimings) { times[1] = System.currentTimeMillis(); @@ -290,18 +295,26 @@ public class RepeatableSampler extends Sampler { */ Object[] heap = pq.getHeap(); for (int si = 0; si < sampleSize; si++) { - sample[si] = (int)(((IntPriorityQueue.MI)(heap[si+1])).value * PHI_32I) & 0x7FFFFFFF; + sample[si] = (int)(((MI) heap[si+1]).value * PHI_32I) & 0x7FFFFFFF; } if (returnTimings) { times[2] = System.currentTimeMillis(); } } + + /** + * A mutable integer that lets queue objects be reused once they start overflowing. + */ + private static class MI { + MI() { } + public int value; + } /** * A bounded priority queue for Integers, to retain a specified number of * the highest-weighted values for return as a random sample. */ - private static class IntPriorityQueue extends PriorityQueue { + private static class IntPriorityQueue extends PriorityQueue { /** * Creates a bounded PQ of size size. @@ -311,17 +324,6 @@ public class RepeatableSampler extends Sampler { super(size); } - /** - * Inserts an integer with overflow and object reuse. - */ - public void insertWithReuse(int intval) { - if (this.mi == null) { - this.mi = new MI(); - } - this.mi.value = intval; - this.mi = (MI)this.insertWithOverflow(this.mi); - } - /** * Returns the underlying data structure for faster access. Extracting elements * one at a time would require N logN time, and since we want the elements sorted @@ -338,23 +340,10 @@ public class RepeatableSampler extends Sampler { * @return True if o1 weighs less than o2. */ @Override - public boolean lessThan(Object o1, Object o2) { - return ((MI)o1).value < ((MI)o2).value; + public boolean lessThan(MI o1, MI o2) { + return o1.value < o2.value; } - /** - * A mutable integer that lets queue objects be reused once they start overflowing. - */ - private static class MI { - MI() { } - public int value; - } - - /** - * The mutable integer instance for reuse after first overflow. - */ - private MI mi; - } /**