-
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
14 changed files
with
492 additions
and
110 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
/* | ||
* 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 <T> The type of the underlying buffer. | ||
*/ | ||
public interface ReusableBuffer<T> { | ||
/** | ||
* Get the number of valid bytes in the data. | ||
* | ||
* @return the number of valid bytes in the data | ||
*/ | ||
long getLength(); | ||
|
||
/** | ||
* Get the buffer backing this ReusableBuffer. | ||
*/ | ||
T 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 srcBytes the data to copy from | ||
* @param start the first position of the new data | ||
* @param len the number of bytes of the new data | ||
*/ | ||
void set(ArrowBuf srcBytes, long start, long 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
130 changes: 130 additions & 0 deletions
130
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,130 @@ | ||
/* | ||
* 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 java.util.Base64; | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* Get the number of bytes in the byte array. | ||
* | ||
* @return the number of bytes in the byte array | ||
*/ | ||
@Override | ||
public long getLength() { | ||
return length; | ||
} | ||
|
||
@Override | ||
public byte[] getBuffer() { | ||
return bytes; | ||
} | ||
|
||
@Override | ||
public void set(ArrowBuf srcBytes, long start, long len) { | ||
setCapacity((int) len, false); | ||
srcBytes.getBytes(start, bytes, 0, (int) len); | ||
length = (int) len; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == this) { | ||
return true; | ||
} else if (o == null) { | ||
return false; | ||
} | ||
if (!(o instanceof ReusableByteArray)) { | ||
return false; | ||
} | ||
|
||
final ReusableByteArray that = (ReusableByteArray) o; | ||
if (this.getLength() != that.getLength()) { | ||
return false; | ||
} | ||
|
||
// copied from Arrays.equals so we don'thave to copy the byte arrays | ||
for (int i = 0; i < length; i++) { | ||
if (bytes[i] != that.bytes[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Copied from Arrays.hashCode so we don't have to copy the byte array. | ||
* | ||
* @return hashCode | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
if (bytes == null) { | ||
return 0; | ||
} | ||
|
||
int result = 1; | ||
for (int i = 0; i < length; i++) { | ||
result = 31 * result + bytes[i]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Base64.getEncoder().encodeToString(Arrays.copyOfRange(bytes, 0, length)); | ||
} | ||
|
||
/** | ||
* 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]; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.