rjha1-godaddy / go-bloom-filter-fork

Simple bloom filter implementation for the GoLang programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GoLang Bloom Filter

Simple bloom filter implementation for the GoLang programming language

Installing

go get github.com/rjha-gd/go-bloom-filter

Importing

import "github.com/rjha-gd/go-bloom-filter"

Creating a new optimal filter

estimatedNumberOfItems, maxFailureRate := 3000, 0.01
bf := bloomfilter.NewFromEstimate(estimatedNumberOfItems, maxFailureRate)

Creating a new filter

sizeOfFilter, numHashFns := 128, 3
bf := bloomfilter.New(sizeOfFilter, numHashFns)

Handling values

Adding a value to the filter

valueToAdd := "https://maliciousurl.xyz"
bf.Add(valueToAdd)
valueToAdd := []byte("https://maliciousurl.xyz")
bf.AddBytes(valueToAdd) // zero memory allocation

Checking if a value has been set

valueToCheck := "https://maliciousurl.xyz"
bf.Check(valueToCheck) // -> true

bf.Check("value that has not been set") // -> false
valueToCheck := []byte("https://maliciousurl.xyz")
bf.CheckBytes(valueToCheck) // zero memory allocation -> true

bf.Check([]byte("value that has not been set")) // zero memory allocation -> false

Benchmarks

goos: darwin
goarch: amd64
pkg: github.com/rjha-gd/go-bloom-filter
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkMakeBloomFilter
BenchmarkMakeBloomFilter-16                            	13588377	        82.84 ns/op	     120 B/op	       4 allocs/op
BenchmarkMakeOptimalBloomFilter
BenchmarkMakeOptimalBloomFilter-16                     	    3560	    332353 ns/op	 3596465 B/op	       4 allocs/op
BenchmarkMakeFilterUnder32
BenchmarkMakeFilterUnder32-16                          	 9978793	       109.6 ns/op	      48 B/op	       5 allocs/op
BenchmarkMakeFilterUnder32WithBytes
BenchmarkMakeFilterUnder32WithBytes-16                 	15447684	        72.59 ns/op	      32 B/op	       3 allocs/op
BenchmarkAddToOptimalBloomFilter
BenchmarkAddToOptimalBloomFilter-16                    	12881869	        91.50 ns/op	      48 B/op	       1 allocs/op
BenchmarkAddBytesToOptimalBloomFilter
BenchmarkAddBytesToOptimalBloomFilter-16               	16374772	        66.39 ns/op	       0 B/op	       0 allocs/op
BenchmarkFillOptimalBloomFilter
BenchmarkFillOptimalBloomFilter-16                     	      10	 110197420 ns/op	       0 B/op	       0 allocs/op
BenchmarkFillOptimalBloomFilterWithBytes
BenchmarkFillOptimalBloomFilterWithBytes-16            	      13	  84416704 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckAllValuesInOptimalBloomFilter
BenchmarkCheckAllValuesInOptimalBloomFilter-16         	       4	 276212327 ns/op	96000048 B/op	 2000001 allocs/op
BenchmarkCheckBytesAllValuesInOptimalBloomFilter
BenchmarkCheckBytesAllValuesInOptimalBloomFilter-16    	       7	 176934254 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckInOptimalBloomFilter
BenchmarkCheckInOptimalBloomFilter-16                  	 2962209	       417.4 ns/op	      48 B/op	       1 allocs/op
BenchmarkCheckBytesInOptimalBloomFilter
BenchmarkCheckBytesInOptimalBloomFilter-16             	 3480291	       346.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckInMap
BenchmarkCheckInMap-16                                 	 3989307	       292.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkAddToFilter
BenchmarkAddToFilter-16                                	32656656	        38.32 ns/op	       8 B/op	       1 allocs/op
BenchmarkAddBytesToFilter
BenchmarkAddBytesToFilter-16                           	72005247	        16.24 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckFilter
BenchmarkCheckFilter-16                                	35740797	        33.23 ns/op	       8 B/op	       1 allocs/op
BenchmarkCheckBytesFilter
BenchmarkCheckBytesFilter-16                           	74309869	        16.20 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckFilterFromOptimal
BenchmarkCheckFilterFromOptimal-16                     	20064470	        61.63 ns/op	       8 B/op	       1 allocs/op
BenchmarkCheckBytesFilterFromOptimal
BenchmarkCheckBytesFilterFromOptimal-16                	27106400	        43.90 ns/op	       0 B/op	       0 allocs/op
BenchmarkEstimateOptimalSize
BenchmarkEstimateOptimalSize-16                        	26948253	        43.01 ns/op	       0 B/op	       0 allocs/op
BenchmarkEstimateOptimalHashFns
BenchmarkEstimateOptimalHashFns-16                     	47074424	        22.14 ns/op	       0 B/op	       0 allocs/op
BenchmarkEstimateOptimalBloomSize
BenchmarkEstimateOptimalBloomSize-16                   	17109001	        68.19 ns/op	       0 B/op	       0 allocs/op

About

Simple bloom filter implementation for the GoLang programming language

License:MIT License


Languages

Language:Go 100.0%