-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-38254: [Java] Add reusable buffer getters to char/binary vectors
Add a reusable buffer interface that can be populated by character and binary vectors to avoid allocations when consuming vector content. Optimize getObject() on VarCharVector/LargeVarCharVector to avoid an extra allocation of a byte array (copy from ArrowBuf directly to the resulting Text).
- Loading branch information
Showing
9 changed files
with
248 additions
and
34 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
java/memory/memory-core/src/main/java/org/apache/arrow/memory/ReusableBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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.arrow.memory; | ||
|
||
/** | ||
* A lightweight, automatically expanding container for holding byte data. | ||
* @param <ReturnTypeT> The type of the underlying buffer. | ||
*/ | ||
public interface ReusableBuffer<ReturnTypeT> { | ||
|
||
/** | ||
* Get the buffer backing this ReusableBuffer. | ||
*/ | ||
ReturnTypeT getBuffer(); | ||
|
||
/** | ||
* Set the buffer to the contents of the given ArrowBuf. | ||
* The internal buffer must resize if it cannot fit the contents | ||
* of the data. | ||
* | ||
* @param bytes the data to copy from | ||
* @param start the first position of the new string | ||
* @param len the number of bytes of the new string | ||
*/ | ||
void set(ArrowBuf bytes, long start, int len); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
java/vector/src/main/java/org/apache/arrow/vector/util/ReusableArrowBuf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.arrow.vector.util; | ||
|
||
import org.apache.arrow.memory.ArrowBuf; | ||
import org.apache.arrow.memory.ReusableBuffer; | ||
|
||
/** | ||
* A writable text buffer backed by an Arrow buffer. | ||
*/ | ||
public class ReusableArrowBuf implements ReusableBuffer<ArrowBuf> { | ||
private ArrowBuf buffer; | ||
|
||
/** | ||
* Creates a new instance that attaches to the given ArrowBuf. | ||
* The caller is responsible for managing the ArrowBuf. | ||
* @param buffer The buffer to attach. | ||
*/ | ||
public ReusableArrowBuf(ArrowBuf buffer) { | ||
this.buffer = buffer; | ||
} | ||
|
||
@Override | ||
public ArrowBuf getBuffer() { | ||
return buffer; | ||
} | ||
|
||
@Override | ||
public void set(ArrowBuf utf8, long start, int len) { | ||
buffer = buffer.reallocIfNeeded(len); | ||
buffer.getBytes(0, utf8, 0, (int) buffer.writerIndex()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
java/vector/src/main/java/org/apache/arrow/vector/util/ReusableByteArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.arrow.vector.util; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.apache.arrow.memory.ArrowBuf; | ||
import org.apache.arrow.memory.ReusableBuffer; | ||
|
||
/** | ||
* A wrapper around byte arrays for repeated writing. | ||
*/ | ||
public class ReusableByteArray implements ReusableBuffer<byte[]> { | ||
|
||
protected static final byte[] EMPTY_BYTES = new byte[0]; | ||
|
||
protected byte[] bytes; | ||
protected int length; | ||
|
||
public ReusableByteArray() { | ||
bytes = EMPTY_BYTES; | ||
} | ||
|
||
@Override | ||
public byte[] getBuffer() { | ||
return bytes; | ||
} | ||
|
||
@Override | ||
public void set(ArrowBuf utf8, long start, int len) { | ||
setCapacity(len, false); | ||
utf8.getBytes(start, bytes, 0, len); | ||
length = len; | ||
} | ||
|
||
/** | ||
* Sets the capacity of this object to <em>at least</em> <code>len</code> bytes. If the | ||
* current buffer is longer, then the capacity and existing content of the buffer are unchanged. | ||
* If <code>len</code> is larger than the current capacity, the Text object's capacity is | ||
* increased to match. | ||
* | ||
* @param len the number of bytes we need | ||
* @param keepData should the old data be kept | ||
*/ | ||
protected void setCapacity(int len, boolean keepData) { | ||
if (bytes == null || bytes.length < len) { | ||
if (bytes != null && keepData) { | ||
bytes = Arrays.copyOf(bytes, Math.max(len, length << 1)); | ||
} else { | ||
bytes = new byte[len]; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters