xxhash

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2016 License: Apache-2.0 Imports: 4 Imported by: 613

README

xxhash GoDoc Build Status

--

This is a CGO wrapper and a native Go implementation of the excellent xxhash* algorithm, an extremely fast non-cryptographic Hash algorithm, working at speeds close to RAM limits.

  • The C implementation is (Copyright (c) 2012-2014, Yann Collet)

Install

Install the recommended pure-go version (much faster with shorter inputs):

go get github.com/OneOfOne/xxhash/native

Or to install the CGO wrapper over the original C code (only recommended if hashing huge slices at a time):

go get github.com/OneOfOne/xxhash

Features

  • The native version is optimized and is as fast as you can get in pure Go.
  • The native version falls back to a less optimized version on appengine (it uses unsafe).
  • Both the native version and the cgo version supports 64bit and 32bit versions of the algorithm.
  • When using the cgo version, it will automatically fallback to the native version if cgo isn't available, you can check the xxhash.Backend const.

Benchmark

go test github.com/OneOfOne/xxhash -bench=. -benchmem
Core i7-4790 @ 3.60GHz, Linux 4.4.1 (64bit), Go dev.ssa (+d3f15ff 2016-02-25)
BenchmarkXXChecksum32-8                   5000000               499 ns/op
BenchmarkXXChecksum32Cgo-8                5000000               601 ns/op

BenchmarkXXChecksumString32-8             5000000               492 ns/op
BenchmarkXXChecksumString32Cgo-8          5000000               607 ns/op

BenchmarkXXChecksum64-8                  10000000               262 ns/op
BenchmarkXXChecksum64Cgo-8               10000000               447 ns/op

BenchmarkXXChecksumString64-8            10000000               274 ns/op
BenchmarkXXChecksumString64Cgo-8         10000000               459 ns/op

BenchmarkXXChecksum64Short-8            300000000              10.3 ns/op
BenchmarkXXChecksum64ShortCgo-8          10000000               285 ns/op

BenchmarkXXChecksumString64Short-8      200000000              12.2 ns/op
BenchmarkXXChecksumString64CgoShort-8    10000000               295 ns/op

BenchmarkXX64MultiWrites-8                5000000               490 ns/op
BenchmarkXX64CgoMultiWrites-8             5000000               819 ns/op
Core i7-4790 @ 3.60GHz, Linux 4.0 (64bit), Go 1.5 (+433af05 2015-04-30)
BenchmarkXxhash32                2000000              1060 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash32Cgo             3000000               492 ns/op               0 B/op          0 allocs/op

BenchmarkXxhash64                2000000               762 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash64Cgo             5000000               359 ns/op               0 B/op          0 allocs/op

BenchmarkFnv32                    500000              2400 ns/op              16 B/op          1 allocs/op
BenchmarkFnv64                    500000              2427 ns/op              16 B/op          1 allocs/op
BenchmarkAdler32                 1000000              1223 ns/op               0 B/op          0 allocs/op
BenchmarkCRC32IEEE                300000              5354 ns/op               0 B/op          0 allocs/op

BenchmarkXxhash64VeryShort     100000000              18.6 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash64CgoVeryShort   10000000               168 ns/op               0 B/op          0 allocs/op

BenchmarkFnv64VeryShort         20000000              98.4 ns/op              32 B/op          2 allocs/op

BenchmarkXxhash64MultiWrites     2000000               724 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash64CgoMultiWrites  5000000               331 ns/op               0 B/op          0 allocs/op

BenchmarkFnv64MultiWrites        1000000              2357 ns/op               0 B/op          0 allocs/op
Core i7-4790 @ 3.60GHz, Linux 3.16.2 (64bit)
BenchmarkXxhash32                1000000              1029 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash32Cgo             3000000               456 ns/op               0 B/op          0 allocs/op

BenchmarkXxhash64                2000000               727 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash64Cgo             5000000               328 ns/op               0 B/op          0 allocs/op

BenchmarkFnv32                   300000               4930 ns/op            2816 B/op          1 allocs/op
BenchmarkFnv64                   300000               4994 ns/op            2816 B/op          1 allocs/op
BenchmarkAdler32                 1000000              1238 ns/op               0 B/op          0 allocs/op
BenchmarkCRC32IEEE               300000               5087 ns/op               0 B/op          0 allocs/op

# key = []byte("Test-key-100")
BenchmarkXxhash64VeryShort       100000000            16.8 ns/op             0 B/op          0 allocs/op
BenchmarkXxhash64CgoVeryShort    10000000              174 ns/op               0 B/op          0 allocs/op

# MultiWrites uses h.Write multiple times
BenchmarkXxhash64MultiWrites     2000000               815 ns/op               0 B/op          0 allocs/op
BenchmarkXxhash64CgoMultiWrites  5000000               342 ns/op               0 B/op          0 allocs/op

Usage

h := xxhash.New64()
// r, err := os.Open("......")
// defer f.Close()
r := strings.NewReader(F)
io.Copy(h, r)
fmt.Println("xxhash.Backend:", xxhash.Backend)
fmt.Println("File checksum:", h.Sum64())

playground

License

Apache v2.0

Copyright 2015-2016 Ahmed <OneOfOne> W.

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

Documentation

Index

Constants

View Source
const Backend = "CGO"

Backend returns the current version of xxhash being used.

Variables

This section is empty.

Functions

func Checksum32

func Checksum32(in []byte) uint32

Checksum32 returns the checksum of the input data with the seed set to 0

func Checksum32S

func Checksum32S(in []byte, seed uint32) uint32

Checksum32S returns the checksum of the input data with the specific seed.

func Checksum64

func Checksum64(in []byte) uint64

Checksum64 returns the checksum of the input data with the seed set to 0.

func Checksum64S

func Checksum64S(in []byte, seed uint64) uint64

Checksum64S returns the checksum of the input bytes with the specific seed.

func ChecksumString32

func ChecksumString32(s string) uint32

ChecksumString32 returns the checksum of the input data, without creating a copy, with the seed set to 0.

func ChecksumString32S

func ChecksumString32S(s string, seed uint32) uint32

ChecksumString32S returns the checksum of the input data, without creating a copy, with the specific seed.

func ChecksumString64

func ChecksumString64(s string) uint64

ChecksumString64 returns the checksum of the input data, without creating a copy, with the seed set to 0.

func ChecksumString64S

func ChecksumString64S(s string, seed uint64) uint64

ChecksumString64S returns the checksum of the input data, without creating a copy, with the specific seed.

Types

type XXHash32

type XXHash32 struct {
	// contains filtered or unexported fields
}

func New32

func New32() *XXHash32

New32 creates a new hash.Hash32 computing the 32bit xxHash checksum starting with the seed set to 0.

func NewS32

func NewS32(seed uint32) *XXHash32

NewS32 creates a new hash.Hash32 computing the 32bit xxHash checksum starting with the specific seed.

func (*XXHash32) BlockSize

func (xx *XXHash32) BlockSize() int

BlockSize returns the hash's underlying block size. The Write method must be able to accept any amount of data, but it may operate more efficiently if all writes are a multiple of the block size.

func (*XXHash32) Reset

func (xx *XXHash32) Reset()

Reset resets the Hash to its initial state.

func (*XXHash32) Size

func (xx *XXHash32) Size() int

Size returns the number of bytes Sum will return.

func (*XXHash32) Sum

func (xx *XXHash32) Sum(in []byte) []byte

Sum appends the current hash to b and returns the resulting slice. It does not change the underlying hash state.

func (*XXHash32) Sum32

func (xx *XXHash32) Sum32() uint32

func (*XXHash32) Write

func (xx *XXHash32) Write(p []byte) (n int, err error)

func (*XXHash32) WriteString

func (xx *XXHash32) WriteString(s string) (int, error)

type XXHash64

type XXHash64 struct {
	// contains filtered or unexported fields
}

func New64

func New64() *XXHash64

New64 creates a new hash.Hash64 computing the 64bit xxHash checksum starting with the seed set to 0.

func NewS64

func NewS64(seed uint64) *XXHash64

NewS64 creates a new hash.Hash64 computing the 64bit xxHash checksum starting with the specific seed.

func (*XXHash64) BlockSize

func (xx *XXHash64) BlockSize() int

BlockSize returns the hash's underlying block size. The Write method must be able to accept any amount of data, but it may operate more efficiently if all writes are a multiple of the block size.

func (*XXHash64) Reset

func (xx *XXHash64) Reset()

Reset resets the Hash to its initial state.

func (*XXHash64) Size

func (xx *XXHash64) Size() int

Size returns the number of bytes Sum will return.

func (*XXHash64) Sum

func (xx *XXHash64) Sum(in []byte) []byte

Sum appends the current hash to b and returns the resulting slice. It does not change the underlying hash state.

func (*XXHash64) Sum64

func (xx *XXHash64) Sum64() uint64

func (*XXHash64) Write

func (xx *XXHash64) Write(p []byte) (n int, err error)

func (*XXHash64) WriteString

func (xx *XXHash64) WriteString(s string) (int, error)

Directories

Path Synopsis
cmd
xxhsum command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL