Apache Lucene open-source search software
LUCENE-4022: Offline Sorter wrongly uses MIN_BUFFER_SIZE if there is more memory available
Simon Willnauer0b698c15af76
diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.javaindex 8d9e5e3724a..6735c5e6e33 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/Sort.java@@ -80,29 +80,35 @@ public final class Sort {/*** Approximately half of the currently available free heap, but no less- * than {@link #MIN_BUFFER_SIZE_MB}. However if current heap allocation- * is insufficient for sorting consult with max allowed heap size.+ * than {@link #ABSOLUTE_MIN_SORT_BUFFER_SIZE}. However if current heap allocation+ * is insufficient or if there is a large portion of unallocated heap-space available+ * for sorting consult with max allowed heap size.*/public static BufferSize automatic() {Runtime rt = Runtime.getRuntime();// take sizes in "conservative" order- long max = rt.maxMemory();- long total = rt.totalMemory();- long free = rt.freeMemory();-+ final long max = rt.maxMemory(); // max allocated+ final long total = rt.totalMemory(); // currently allocated+ final long free = rt.freeMemory(); // unused portion of currently allocated+ final long totalAvailableBytes = max - total + free;+// by free mem (attempting to not grow the heap for this)- long half = free/2;- if (half >= ABSOLUTE_MIN_SORT_BUFFER_SIZE) {- return new BufferSize(Math.min(MIN_BUFFER_SIZE_MB * MB, half));+ long sortBufferByteSize = free/2;+ final long minBufferSizeBytes = MIN_BUFFER_SIZE_MB*MB;+ if (sortBufferByteSize < minBufferSizeBytes+ || totalAvailableBytes > 10 * minBufferSizeBytes) { // lets see if we need/should to grow the heap+ if (totalAvailableBytes/2 > minBufferSizeBytes){ // there is enough mem for a reasonable buffer+ sortBufferByteSize = totalAvailableBytes/2; // grow the heap+ } else {+ //heap seems smallish lets be conservative fall back to the free/2+ sortBufferByteSize = Math.max(ABSOLUTE_MIN_SORT_BUFFER_SIZE, sortBufferByteSize);+ }}-- // by max mem (heap will grow)- half = (max - total) / 2;- return new BufferSize(Math.min(MIN_BUFFER_SIZE_MB * MB, half));+ return new BufferSize(Math.min(Integer.MAX_VALUE, sortBufferByteSize));}}-+/*** Sort info (debugging mostly).*/@@ -490,4 +496,4 @@ public final class Sort {public Comparator<BytesRef> getComparator() {return comparator;}-}\ No newline at end of file+}