Skip to content

Apache Lucene open-source search software

LUCENE-10657: CopyBytes now saves one memory copy on ByteBuffersDataOutput (#1034)

luyunchenge5bf76b84304
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 6f317c84fb6..000a3e2d032 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -54,7 +54,8 @@ Improvements
Optimizations
---------------------
-(No changes)
+
+* LUCENE-10657: CopyBytes now saves one memory copy on ByteBuffersDataOutput. (luyuncheng)
Bug Fixes
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java
index 8639ac5522e..84a7b317d88 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java
@@ -309,6 +309,29 @@ public final class ByteBuffersDataOutput extends DataOutput implements Accountab
return arr;
}
+ @Override
+ public void copyBytes(DataInput input, long numBytes) throws IOException {
+ assert numBytes >= 0 : "numBytes=" + numBytes;
+ int length = (int) numBytes;
+ while (length > 0) {
+ if (!currentBlock.hasRemaining()) {
+ appendBlock();
+ }
+ if (!currentBlock.hasArray()) {
+ break;
+ }
+ int chunk = Math.min(currentBlock.remaining(), length);
+ final int pos = currentBlock.position();
+ input.readBytes(currentBlock.array(), Math.addExact(currentBlock.arrayOffset(), pos), chunk);
+ length -= chunk;
+ currentBlock.position(pos + chunk);
+ }
+ // if current block is Direct, we fall back to super.copyBytes for remaining bytes
+ if (length > 0) {
+ super.copyBytes(input, length);
+ }
+ }
+
/** Copy the current content of this object into another {@link DataOutput}. */
public void copyTo(DataOutput output) throws IOException {
for (ByteBuffer bb : blocks) {
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestByteBuffersDataOutput.java b/lucene/core/src/test/org/apache/lucene/store/TestByteBuffersDataOutput.java
index 065606c3ba3..50eefc3f3bf 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestByteBuffersDataOutput.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestByteBuffersDataOutput.java
@@ -204,6 +204,42 @@ public final class TestByteBuffersDataOutput extends BaseDataOutputTestCase<Byte
ArrayUtil.copyOfSubArray(bytes, offset, offset + len), o.toArrayCopy());
}
+ @Test
+ public void testCopyBytesOnHeap() throws IOException {
+ byte[] bytes = new byte[1024 * 8 + 10];
+ random().nextBytes(bytes);
+ int offset = TestUtil.nextInt(random(), 0, 100);
+ int len = bytes.length - offset;
+ ByteArrayDataInput in = new ByteArrayDataInput(bytes, offset, len);
+ ByteBuffersDataOutput o =
+ new ByteBuffersDataOutput(
+ ByteBuffersDataOutput.DEFAULT_MIN_BITS_PER_BLOCK,
+ ByteBuffersDataOutput.DEFAULT_MAX_BITS_PER_BLOCK,
+ ByteBuffersDataOutput.ALLOCATE_BB_ON_HEAP,
+ ByteBuffersDataOutput.NO_REUSE);
+ o.copyBytes(in, len);
+ Assert.assertArrayEquals(
+ o.toArrayCopy(), ArrayUtil.copyOfSubArray(bytes, offset, offset + len));
+ }
+
+ @Test
+ public void testCopyBytesOnDirectByteBuffer() throws IOException {
+ byte[] bytes = new byte[1024 * 8 + 10];
+ random().nextBytes(bytes);
+ int offset = TestUtil.nextInt(random(), 0, 100);
+ int len = bytes.length - offset;
+ ByteArrayDataInput in = new ByteArrayDataInput(bytes, offset, len);
+ ByteBuffersDataOutput o =
+ new ByteBuffersDataOutput(
+ ByteBuffersDataOutput.DEFAULT_MIN_BITS_PER_BLOCK,
+ ByteBuffersDataOutput.DEFAULT_MAX_BITS_PER_BLOCK,
+ ByteBuffer::allocateDirect,
+ ByteBuffersDataOutput.NO_REUSE);
+ o.copyBytes(in, len);
+ Assert.assertArrayEquals(
+ o.toArrayCopy(), ArrayUtil.copyOfSubArray(bytes, offset, offset + len));
+ }
+
@Test
public void testToBufferListReturnsReadOnlyBuffers() throws Exception {
ByteBuffersDataOutput dst = new ByteBuffersDataOutput();