From 1c4c1c831def46468196a80ff2a96464c4811817 Mon Sep 17 00:00:00 2001 From: Stefan Vodita <41467371+stefanvodita@users.noreply.github.com> Date: Fri, 8 Dec 2023 17:41:58 +0000 Subject: [PATCH] Quick exit for non-zero slice buffers (#12812) --- lucene/CHANGES.txt | 2 ++ .../lucene/index/memory/MemoryIndex.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index f1987fbacd5..cf60620494b 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -178,6 +178,8 @@ Improvements * GITHUB#12870: Tighten synchronized loop in DirectoryTaxonomyReader#getOrdinal. (Stefan Vodita) +* GITHUB#12812: Avoid overflows and false negatives in int slice buffer filled-with-zeros assertion. (Stefan Vodita) + Optimizations --------------------- (No changes) diff --git a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java index c66770fda5b..09a521c880d 100644 --- a/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java +++ b/lucene/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java @@ -179,6 +179,19 @@ public class MemoryIndex { super(allocator); } + /** + * For slices, buffers must be filled with zeros, so that we can find a slice's end based on a + * non-zero final value. + */ + private static boolean assertSliceBuffer(int[] buffer) { + for (int value : buffer) { + if (value != 0) { + return false; + } + } + return true; + } + /** * Creates a new int slice with the given starting size and returns the slices offset in the * pool. @@ -197,14 +210,6 @@ public class MemoryIndex { return upto; } - private static boolean assertSliceBuffer(int[] buffer) { - int count = 0; - for (int i = 0; i < buffer.length; i++) { - count += buffer[i]; // for slices the buffer must only have 0 values - } - return count == 0; - } - /** Allocates a new slice from the given offset */ private int allocSlice(final int[] slice, final int sliceOffset) { final int level = slice[sliceOffset] & 15;