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

    https://github.com/apache/spark/pull/11209#discussion_r56972303
  
    --- Diff: 
sql/catalyst/src/test/scala/org/apache/spark/sql/HashByteArrayBenchmark.scala 
---
    @@ -0,0 +1,145 @@
    +/*
    + * 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.spark.sql
    +
    +import java.util.Random
    +
    +import org.apache.spark.sql.catalyst.expressions.XXH64
    +import org.apache.spark.unsafe.Platform
    +import org.apache.spark.unsafe.hash.Murmur3_x86_32
    +import org.apache.spark.util.Benchmark
    +
    +/**
    + * Synthetic benchmark for MurMurHash 3 and xxHash64.
    + */
    +object HashByteArrayBenchmark {
    +  def test(length: Int, seed: Long, numArrays: Int, iters: Int): Unit = {
    +    val random = new Random(seed)
    +    val arrays = Array.fill[Array[Byte]](numArrays) {
    +      val bytes = new Array[Byte](length)
    +      random.nextBytes(bytes)
    +      bytes
    +    }
    +
    +    val benchmark = new Benchmark("Hash byte arrays with length " + 
length, iters * numArrays)
    +    benchmark.addCase("Murmur3_x86_32") { _: Int =>
    +      for (_ <- 0L until iters) {
    +        var sum = 0
    +        var i = 0
    +        while (i < numArrays) {
    +          sum += Murmur3_x86_32.hashUnsafeBytes(arrays(i), 
Platform.BYTE_ARRAY_OFFSET, length, 42)
    +          i += 1
    +        }
    +      }
    +    }
    +
    +    benchmark.addCase("xxHash 64-bit") { _: Int =>
    +      for (_ <- 0L until iters) {
    +        var sum = 0L
    +        var i = 0
    +        while (i < numArrays) {
    +          sum += XXH64.hashUnsafeBytes(arrays(i), 
Platform.BYTE_ARRAY_OFFSET, length, 42)
    +          i += 1
    +        }
    +      }
    +    }
    +
    +    benchmark.run()
    +  }
    +
    +  def main(args: Array[String]): Unit = {
    +    // Add 31 to all arrays to create worse case alignment for xxHash.
    +    /*
    +    Running benchmark: Hash byte arrays with length 31
    --- End diff --
    
    I have added cases for 8, 16 & 24 bytes. Murmur is faster for 8 & 16 bytes, 
whereas xxHash is faster beyond this. The difference is probably due to the 
fact that xxHash uses a few more branches (we could get rid of a few, but then 
we need to sacrifice compatibility with other implementations).
    
    To check if branching is an issue I have also added a benchmark for single 
longs in `HashBenchmark`. In this case there are no branches, and then xxHash 
is a 'bit' faster.


---
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