Giter Club home page Giter Club logo

scala-hashing's Introduction

Scala-Hashing

Build Status codecov.io Maven Central

Overview

Fast non-cryptographic hash functions for Scala. This library provides APIs for computing 32-bit and 64-bit hashes.

Currently implemented hash functions

Hash functions in this library can be accessed via either a standard API for hashing primitives, byte arrays, or Java ByteBuffers (direct and non-direct), or a streaming API for hashing stream-like objects such as InputStreams, Java NIO Channels, or Akka Streams. Hash functions should produce consistent output regardless of platform or endianness.

This library uses the sun.misc.Unsafe API internally. I might explore using the VarHandle API introduced in Java 9 in the future, but am currently still supporting Java 8.

Performance

Benchmarked against various other open-source implementations

  • Guava (MurmurHash3)
  • LZ4 Java (XxHash32 and XxHash64 - Includes JNI binding, pure Java, and Java+Unsafe implementations)
  • Scala (Scala's built-in scala.util.hashing.MurmurHash3)
  • Zero-Allocation-Hashing (XxHash64)

MurmurHash3_32

MurmurHash3_32

XxHash32

XxHash32

XxHash64

XxHash64

Running Locally

Benchmarks are located in the bench subproject and can be run using the sbt-jmh plugin.

To run all benchmarks with default settings

bench/jmh:run

To run a specific benchmark with custom settings

bench/jmh:run -f 2 -wi 5 -i 5 XxHash64Bench

Getting Started

libraryDependencies += "com.desmondyeung.hashing" %% "scala-hashing" % "0.1.0"

Examples

This library defines the interfaces Hash32 and StreamingHash32 for computing 32-bit hashes and Hash64 and StreamingHash64 for computing 64-bit hashes. Classes extending StreamingHash32 or StreamingHash64 are not thread-safe.

The public API for Hash64 and StreamingHash64 can be seen below

trait Hash64 {
  def hashByte(input: Byte, seed: Long): Long
  def hashInt(input: Int, seed: Long): Long
  def hashLong(input: Long, seed: Long): Long
  def hashByteArray(input: Array[Byte], seed: Long): Long
  def hashByteArray(input: Array[Byte], offset: Int, length: Int, seed: Long): Long
  def hashByteBuffer(input: ByteBuffer, seed: Long): Long
  def hashByteBuffer(input: ByteBuffer, offset: Int, length: Int, seed: Long): Long
}

trait StreamingHash64 {
  def reset(): Unit
  def value: Long
  def updateByteArray(input: Array[Byte], offset: Int, length: Int): Unit
  def updateByteBuffer(input: ByteBuffer, offset: Int, length: Int): Unit
}

Using the standard API

import com.desmondyeung.hashing.XxHash64
import java.nio.ByteBuffer

// hash a long
val hash = XxHash64.hashLong(123, seed = 0)

// hash a Array[Byte]
val hash = XxHash64.hashByteArray(Array[Byte](123), seed = 0)

// hash a ByteBuffer
val hash = XxHash64.hashByteBuffer(ByteBuffer.wrap(Array[Byte](123)), seed = 0)

Using the streaming API

import com.desmondyeung.hashing.StreamingXxHash64
import java.nio.ByteBuffer
import java.io.FileInputStream

val checksum = StreamingXxHash64(seed = 0)
val channel  = new FileInputStream("/path/to/file.txt").getChannel
val chunk    = ByteBuffer.allocate(1024)

var bytesRead = channel.read(chunk)
while (bytesRead > 0) {
  checksum.updateByteBuffer(chunk, 0, bytesRead)
  chunk.rewind
  bytesRead = channel.read(chunk)
}

val hash = checksum.value

License

Licensed under the Apache License, Version 2.0 (the "License").

scala-hashing's People

Contributors

desmondyeung avatar plokhotnyuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

scala-hashing's Issues

Support for java 9+

Currently due to usage of sun.nio.ch.DirectBuffer it is impossible to use this library with jdk9+ since it leads to lass com.desmondyeung.hashing.StreamingHash64 cannot access class sun.nio.ch.DirectBuffer (in module java.base) because module java.base does not export sun.nio.ch

How about checking for jdk version and based on that either using DirectBuffer directly (pun not intended :) ) or through var handles?

I can't actually into unsafe, so just high-level example to clarify what I mean:

trait UnsafeBytes {
  def addressOf(buffer: ByteBuffer): Int
}
object UnsafeBytes {
  private val vmSpec: Int = sys.props("java.vm.specification.version").split('.').last.toInt

  def apply: UnsafeBytes = if (vmSpec < 9) { // or we could check if there is Var/MethodHandle class present in classpath
    directDirectBuffer
  } else {
    varHandleDirectBuffer
  }

  private object directDirectBuffer extends UnsafeBytes {
    //...
  }

  private object varHandleDirectBuffer extends UnsafeBytes {
    //...
  }
}

Investigate Performance Regression with Scala 2.13.0

bench/jmh:run -i 3 -wi 3 -f1 XxHash64Bench.com_desmondyeung_hashing

2.12.9

[info] Benchmark                               (inputSize)   Mode  Cnt          Score         Error  Units
[info] XxHash64Bench.com_desmondyeung_hashing            8  thrpt    3  185778619.019 ± 1934781.573  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing          128  thrpt    3   56514066.010 ±  634268.161  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing          512  thrpt    3   23600141.441 ±  810006.785  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing         1024  thrpt    3   13299685.393 ±  717831.068  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing         1536  thrpt    3    9246722.872 ±  260972.021  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing         2048  thrpt    3    7056145.724 ±  265236.056  ops/s
[success] Total time: 184 s, completed Aug 21, 2019, 3:15:13 PM

2.13.0

[info] Benchmark                               (inputSize)   Mode  Cnt          Score         Error  Units
[info] XxHash64Bench.com_desmondyeung_hashing            8  thrpt    3  184298585.739 ± 2062166.900  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing          128  thrpt    3   52045201.983 ± 3229937.845  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing          512  thrpt    3   21511399.488 ±  311140.849  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing         1024  thrpt    3   11933307.339 ±  745263.751  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing         1536  thrpt    3    8226233.328 ±  404379.004  ops/s
[info] XxHash64Bench.com_desmondyeung_hashing         2048  thrpt    3    6321480.430 ±  312725.166  ops/s
[success] Total time: 190 s, completed Aug 21, 2019, 3:11:30 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.