Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13680#discussion_r79562772
  
    --- Diff: 
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeArrayWriter.java
 ---
    @@ -33,134 +36,213 @@
       // The offset of the global buffer where we start to write this array.
       private int startingOffset;
     
    -  public void initialize(BufferHolder holder, int numElements, int 
fixedElementSize) {
    -    // We need 4 bytes to store numElements and 4 bytes each element to 
store offset.
    -    final int fixedSize = 4 + 4 * numElements;
    +  // The number of elements in this array
    +  private int numElements;
    +
    +  private int headerInBytes;
    +
    +  private void assertIndexIsValid(int index) {
    +    assert index >= 0 : "index (" + index + ") should >= 0";
    +    assert index < numElements : "index (" + index + ") should < " + 
numElements;
    +  }
    +
    +  public void initialize(BufferHolder holder, int numElements, int 
elementSize) {
    +    // We need 8 bytes to store numElements in header
    +    this.numElements = numElements;
    +    this.headerInBytes = calculateHeaderPortionInBytes(numElements);
     
         this.holder = holder;
         this.startingOffset = holder.cursor;
     
    -    holder.grow(fixedSize);
    -    Platform.putInt(holder.buffer, holder.cursor, numElements);
    -    holder.cursor += fixedSize;
    +    // Grows the global buffer ahead for header and fixed size data.
    +    int fixedPartInBytes = ((elementSize * numElements + 7) / 8) * 8;
    +    holder.grow(headerInBytes + fixedPartInBytes);
    +
    +    // Write numElements and clear out null bits to header
    +    Platform.putLong(holder.buffer, startingOffset, numElements);
    +    for (int i = 8; i < headerInBytes; i += 8) {
    +      Platform.putLong(holder.buffer, startingOffset + i, 0L);
    +    }
    +
    +    // fill 0 into reminder part of 8-bytes alignment in unsafe array
    +    if ((fixedPartInBytes - elementSize * numElements) != 0) {
    +      for (int i = elementSize * numElements; i < fixedPartInBytes; i++) {
    +        Platform.putByte(holder.buffer, startingOffset + headerInBytes + 
i, (byte) 0);
    +      }
    +    }
    +    holder.cursor += (headerInBytes + fixedPartInBytes);
    +  }
    +
    +  private void zeroOutPaddingBytes(int numBytes) {
    +    if ((numBytes & 0x07) > 0) {
    +      Platform.putLong(holder.buffer, holder.cursor + ((numBytes >> 3) << 
3), 0L);
    +    }
    +  }
    +
    +  private long getElementOffset(int ordinal, int elementSize) {
    +    return startingOffset + headerInBytes + ordinal * elementSize;
    +  }
    +
    +  public void setOffsetAndSize(int ordinal, long currentCursor, long size) 
{
    --- End diff --
    
    similar to `setNullBit`, should we also call `assertIndexIsValid(ordinal)` 
in this method?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to