Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9131#discussion_r42291076
  
    --- Diff: 
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/UnsafeMapData.java
 ---
    @@ -17,41 +17,75 @@
     
     package org.apache.spark.sql.catalyst.expressions;
     
    +import java.nio.ByteBuffer;
    +
     import org.apache.spark.sql.types.MapData;
    +import org.apache.spark.unsafe.Platform;
     
     /**
      * An Unsafe implementation of Map which is backed by raw memory instead 
of Java objects.
      *
    - * Currently we just use 2 UnsafeArrayData to represent UnsafeMapData.
    - *
    - * Note that when we write out this map, we should write out the 
`numElements` at first 4 bytes,
    - * and numBytes of key array at second 4 bytes, then follows key array 
content and value array
    - * content without `numElements` header.
    - * When we read in a map, we should read first 4 bytes as `numElements` 
and second 4 bytes as
    - * numBytes of key array, and construct unsafe key array and value array 
with these 2 information.
    + * Currently we just use 2 UnsafeArrayData to represent UnsafeMapData, 
with extra 4 bytes at head
    + * to indicate the number of bytes of the unsafe key array.
    + * [unsafe key array numBytes] [unsafe key array] [unsafe value array]
      */
    +// TODO: Use a more efficient format which doesn't depend on unsafe array.
     public class UnsafeMapData extends MapData {
     
    -  private final UnsafeArrayData keys;
    -  private final UnsafeArrayData values;
    -  // The number of elements in this array
    -  private int numElements;
    -  // The size of this array's backing data, in bytes
    +  private Object baseObject;
    +  private long baseOffset;
    +
    +  // The size of this map's backing data, in bytes.
    +  // The 4-bytes header of key array `numBytes` is also included, so it's 
actually equal to
    +  // 4 + key array numBytes + value array numBytes.
       private int sizeInBytes;
     
    +  public Object getBaseObject() { return baseObject; }
    +  public long getBaseOffset() { return baseOffset; }
       public int getSizeInBytes() { return sizeInBytes; }
     
    -  public UnsafeMapData(UnsafeArrayData keys, UnsafeArrayData values) {
    -    assert keys.numElements() == values.numElements();
    -    this.sizeInBytes = keys.getSizeInBytes() + values.getSizeInBytes();
    -    this.numElements = keys.numElements();
    -    this.keys = keys;
    -    this.values = values;
    +  private UnsafeArrayData keys;
    +  private UnsafeArrayData values;
    +
    +  /**
    +   * Construct a new UnsafeMapData. The resulting UnsafeMapData won't be 
usable until
    +   * `pointTo()` has been called, since the value returned by this 
constructor is equivalent
    +   * to a null pointer.
    +   */
    +  public UnsafeMapData() { }
    +
    +  /**
    +   * Update this UnsafeMapData to point to different backing data.
    +   *
    +   * @param baseObject the base object
    +   * @param baseOffset the offset within the base object
    +   * @param sizeInBytes the size of this map's backing data, in bytes
    +   */
    +  public void pointTo(Object baseObject, long baseOffset, int sizeInBytes) 
{
    +    // Read the numBytes of key array from the first 4 bytes.
    +    final int keyArraySize = Platform.getInt(baseObject, baseOffset);
    +    final int valueArraySize = sizeInBytes - keyArraySize - 4;
    +    assert keyArraySize >= 0 : "keyArraySize (" + keyArraySize + ") should 
>= 0";
    +    assert valueArraySize >= 0 : "valueArraySize (" + valueArraySize + ") 
should >= 0";
    +
    +    final UnsafeArrayData keyArray = new UnsafeArrayData();
    --- End diff --
    
    We do not need to create these two objections for each `pointTo`, they 
should be created in constructor.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to