RamakrishnaChilaka commented on code in PR #15140:
URL: https://github.com/apache/lucene/pull/15140#discussion_r2315534894


##########
lucene/CHANGES.txt:
##########
@@ -130,6 +130,8 @@ API Changes
   instance instead of a Bits instance to identify document IDs to filter.
   (Shubham Chaudhary, Adrien Grand)
 
+* GITHUB#15140: Optimize TopScoreDocCollector with TernaryLongHeap for 
improved performance over Binary-LongHeap. (Ramakrishna Chilaka)

Review Comment:
   Rebased and moved to the 10.4.0 section.



##########
lucene/core/src/java/org/apache/lucene/util/TernaryLongHeap.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.util;
+
+import java.util.Arrays;
+
+/**
+ * A ternary min heap that stores longs; a primitive priority queue that like 
all priority queues
+ * maintains a partial ordering of its elements such that the least element 
can always be found in
+ * constant time. Put()'s and pop()'s require log_3(size). This heap provides 
unbounded growth via
+ * {@link #push(long)}, and bounded-size insertion based on its nominal 
maxSize via {@link
+ * #insertWithOverflow(long)}. The heap is a min heap, meaning that the top 
element is the lowest
+ * value of the heap. TernaryLongHeap implements 3-ary heap.
+ *
+ * @lucene.internal
+ */
+public final class TernaryLongHeap {
+
+  private final int maxSize;
+
+  private long[] heap;
+  private int size = 0;
+
+  /**
+   * Constructs a heap with specified size and initializes all elements with 
the given value.
+   *
+   * @param size the number of elements to initialize in the heap.
+   * @param initialValue the value to fill the heap with.
+   */
+  public TernaryLongHeap(int size, long initialValue) {
+    this(size <= 0 ? 1 : size);
+    Arrays.fill(heap, 1, size + 1, initialValue);
+    this.size = size;
+  }
+
+  /**
+   * Create an empty priority queue of the configured initial size.
+   *
+   * @param maxSize the maximum size of the heap, or if negative, the initial 
size of an unbounded
+   *     heap
+   */
+  public TernaryLongHeap(int maxSize) {
+    if (maxSize < 1 || maxSize >= ArrayUtil.MAX_ARRAY_LENGTH) {
+      // Throw exception to prevent confusing OOME:
+      throw new IllegalArgumentException(
+          "maxSize must be > 0 and < " + (ArrayUtil.MAX_ARRAY_LENGTH - 1) + "; 
got: " + maxSize);
+    }
+    // NOTE: we add +1 because all access to heap is 1-based not 0-based.  
heap[0] is unused.
+    final int heapSize = maxSize + 1;
+    this.maxSize = maxSize;
+    this.heap = new long[heapSize];
+  }
+
+  /**
+   * Adds a value in log(size) time. Grows unbounded as needed to accommodate 
new values.
+   *
+   * @return the new 'top' element in the queue.
+   */
+  public long push(long element) {
+    size++;
+    if (size == heap.length) {
+      heap = ArrayUtil.grow(heap, (size * 3 + 1) / 2);
+    }
+    heap[size] = element;
+    LongHeap.upHeap(heap, size, 3);
+    return heap[1];
+  }

Review Comment:
   I agree it's odd that ctor takes a `maxSize` parameter but doesn't enforce 
it. Enforcing it would be a behavioural change i.e., rejecting `push` could 
break existing code that relies on unbounded growth. For instance, 
`NeighborQueue` initializes LongHeap with initialSize (in maxSize Parameter), 
but its add method can insert more elements, effectively treating it as 
unbounded.
   
   Possible improvements:
   * Rename `maxSize` parameter to `initialSize`/`initialCapacity` to better 
reflect its behaviour.
   * Add an overloaded ctor with `initialSize`, `maxSize` and `bounded` to 
support bounded heaps explicitly. 
   * Introduce `bounded` flag for the users to opt-in to the strict size 
enforcement.



##########
lucene/core/src/java/org/apache/lucene/util/TernaryLongHeap.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.util;
+
+import java.util.Arrays;
+
+/**
+ * A ternary min heap that stores longs; a primitive priority queue that like 
all priority queues
+ * maintains a partial ordering of its elements such that the least element 
can always be found in
+ * constant time. Put()'s and pop()'s require log_3(size). This heap provides 
unbounded growth via
+ * {@link #push(long)}, and bounded-size insertion based on its nominal 
maxSize via {@link
+ * #insertWithOverflow(long)}. The heap is a min heap, meaning that the top 
element is the lowest
+ * value of the heap. TernaryLongHeap implements 3-ary heap.
+ *
+ * @lucene.internal
+ */
+public final class TernaryLongHeap {
+
+  private final int maxSize;
+
+  private long[] heap;
+  private int size = 0;
+
+  /**
+   * Constructs a heap with specified size and initializes all elements with 
the given value.
+   *
+   * @param size the number of elements to initialize in the heap.
+   * @param initialValue the value to fill the heap with.
+   */
+  public TernaryLongHeap(int size, long initialValue) {
+    this(size <= 0 ? 1 : size);
+    Arrays.fill(heap, 1, size + 1, initialValue);
+    this.size = size;
+  }
+
+  /**
+   * Create an empty priority queue of the configured initial size.
+   *
+   * @param maxSize the maximum size of the heap, or if negative, the initial 
size of an unbounded
+   *     heap
+   */
+  public TernaryLongHeap(int maxSize) {
+    if (maxSize < 1 || maxSize >= ArrayUtil.MAX_ARRAY_LENGTH) {
+      // Throw exception to prevent confusing OOME:
+      throw new IllegalArgumentException(
+          "maxSize must be > 0 and < " + (ArrayUtil.MAX_ARRAY_LENGTH - 1) + "; 
got: " + maxSize);
+    }
+    // NOTE: we add +1 because all access to heap is 1-based not 0-based.  
heap[0] is unused.
+    final int heapSize = maxSize + 1;
+    this.maxSize = maxSize;
+    this.heap = new long[heapSize];
+  }
+
+  /**
+   * Adds a value in log(size) time. Grows unbounded as needed to accommodate 
new values.
+   *
+   * @return the new 'top' element in the queue.
+   */
+  public long push(long element) {
+    size++;
+    if (size == heap.length) {
+      heap = ArrayUtil.grow(heap, (size * 3 + 1) / 2);
+    }
+    heap[size] = element;
+    LongHeap.upHeap(heap, size, 3);
+    return heap[1];
+  }
+
+  /**
+   * Adds a value to an TernaryLongHeap in log(size) time. If the number of 
values would exceed the
+   * heap's maxSize, the least value is discarded.
+   *
+   * @return whether the value was added (unless the heap is full, or the new 
value is less than the
+   *     top value)
+   */
+  public boolean insertWithOverflow(long value) {
+    if (size >= maxSize) {
+      if (value < heap[1]) {
+        return false;
+      }
+      updateTop(value);
+      return true;
+    }
+    push(value);
+    return true;
+  }
+
+  /**
+   * Returns the least element of the TernaryLongHeap in constant time. It is 
up to the caller to
+   * verify that the heap is not empty; no checking is done, and if no 
elements have been added, 0
+   * is returned.
+   */
+  public long top() {
+    return heap[1];
+  }
+
+  /**
+   * Removes and returns the least element of the PriorityQueue in log(size) 
time.
+   *
+   * @throws IllegalStateException if the TernaryLongHeap is empty.
+   */
+  public long pop() {
+    if (size > 0) {
+      long result = heap[1]; // save first value
+      heap[1] = heap[size]; // move last to first
+      size--;
+      LongHeap.downHeap(heap, 1, size, 3); // adjust heap

Review Comment:
   Got it, Introduced a new constant `ARITY=3`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to