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

    https://github.com/apache/drill/pull/1060#discussion_r164509658
  
    --- Diff: 
exec/memory/base/src/main/java/org/apache/drill/exec/util/MemoryUtils.java ---
    @@ -0,0 +1,186 @@
    +/**
    + * 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.drill.exec.util;
    +
    +import java.lang.reflect.Field;
    +import java.nio.ByteOrder;
    +
    +import sun.misc.Unsafe;
    +
    +/** Exposes advanced Memory Access APIs for Little-Endian / Unaligned 
platforms */
    +@SuppressWarnings("restriction")
    +public final class MemoryUtils {
    +
    +  // Ensure this is a little-endian hardware */
    +  static {
    +    if (ByteOrder.nativeOrder() != ByteOrder.LITTLE_ENDIAN) {
    +      throw new IllegalStateException("Drill only runs on LittleEndian 
systems.");
    +    }
    +  }
    +
    +  /** Java's unsafe object */
    +  private static Unsafe UNSAFE;
    +
    +  static {
    +    try {
    +      Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
    +      theUnsafe.setAccessible(true);
    +      UNSAFE = (Unsafe) theUnsafe.get(null);
    +    }
    +    catch (Exception e) {
    +      throw new RuntimeException(e);
    +    }
    +  }
    +
    +  /** Byte arrays offset */
    +  private static final long BYTE_ARRAY_OFFSET = 
UNSAFE.arrayBaseOffset(byte[].class);
    +
    +  /** Number of bytes in a long */
    +  public static final int LONG_NUM_BYTES  = 8;
    +  /** Number of bytes in an int */
    +  public static final int INT_NUM_BYTES   = 4;
    +  /** Number of bytes in a short */
    +  public static final int SHORT_NUM_BYTES = 2;
    +
    
+//----------------------------------------------------------------------------
    +// APIs
    
+//----------------------------------------------------------------------------
    +
    +  /**
    +   * @param data source byte array
    +   * @param index index within the byte array
    +   * @return short value starting at data+index
    +   */
    +  public static short getShort(byte[] data, int index) {
    --- End diff --
    
    Vlad,
    
    - The reader has loaded data from direct memory into CPU level-1 cache 
through DrillBuf
    - The data is stored (4k at a time) in a Java byte[]
    - The only reason for using MemoryUtil is to perform 64bit processing (I 
have previously indicated that many other processing intensive applications 
such as Snappy and Hadoop Comparison use the same pattern)
    - I solely added this logic after noticing the Hotspot was generating 8bit 
instructions 
    
    Again, the intent is not to access data from DrillBuf (the data has been 
already loaded into CPU cache); this is mainly an optimization step.  


---

Reply via email to