You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/02/04 23:01:05 UTC

[GitHub] [druid] gianm commented on a change in pull request #9308: Add MemoryOpenHashTable, a table similar to ByteBufferHashTable.

gianm commented on a change in pull request #9308: Add MemoryOpenHashTable, a table similar to ByteBufferHashTable.
URL: https://github.com/apache/druid/pull/9308#discussion_r374972811
 
 

 ##########
 File path: processing/src/main/java/org/apache/druid/query/groupby/epinephelinae/collection/HashTableUtils.java
 ##########
 @@ -0,0 +1,162 @@
+/*
+ * 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.druid.query.groupby.epinephelinae.collection;
+
+import org.apache.datasketches.memory.Memory;
+
+public class HashTableUtils
+{
+  private HashTableUtils()
+  {
+    // No instantiation.
+  }
+
+  /**
+   * Computes the previous power of two less than or equal to a given "n".
+   *
+   * The integer should be between 1 (inclusive) and {@link Integer#MAX_VALUE} for best results. Other parameters will
+   * return {@link Integer#MIN_VALUE}.
+   */
+  public static int previousPowerOfTwo(final int n)
+  {
+    if (n > 0) {
+      return Integer.highestOneBit(n);
+    } else {
+      return Integer.MIN_VALUE;
+    }
+  }
+
+  /**
+   * Compute a simple, fast hash code of some memory range.
+   *
+   * @param memory   a region of memory
+   * @param position position within the memory region
+   * @param length   length of memory to hash, starting at the position
+   */
+  public static int hashMemory(final Memory memory, final long position, final int length)
+  {
+    // Special cases for small, common key sizes to speed them up: e.g. one int key, two int keys, one long key, etc.
+    // The plus-one sizes (9, 13) are for nullable dimensions. The specific choices of special cases were chosen based
+    // on benchmarking (see MemoryBenchmark) on a Skylake-based cloud instance.
+
+    switch (length) {
+      case 4:
+        return 31 + memory.getInt(position);
+
+      case 8:
+        return 31 * (31 + memory.getInt(position)) + memory.getInt(position + Integer.BYTES);
 
 Review comment:
   I was doing the same kind of calculation that `Arrays.hashCode` would do:
   
   ```java
           int result = 1;
           for (int element : a)
               result = 31 * result + element;
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org