This is a creation in Article, where the information may have evolved or changed.
HashSet is a very efficient data structure, the complexity of insertion and query is O (1), basically can meet the performance requirements of most scenarios, but in some special scenarios, very high frequency calls will still become a performance bottleneck (with pprof analysis), such as the ads in the direction of logic, Filtering logic in a single request can be performed thousands of times, some of which are just some of the enumeration values, such as gender orientation, age orientation, and so on, which can be optimized with Bitset for a value that could be represented by an enumeration, with a performance improvement of more than 20 times
The essence of Bitset is also a kind of hashset, except that the hash bucket is represented by a uint64, and each of the UInt64 is used to represent whether an element exists, if 1 is present, 0 means no, and the insert and query operations become bit operations.
Bitset implementation
The implementation of Bitset is relatively easy, the following is a support only the enumeration value of not more than 64 version, of course, can extend to any length, using a UInt64 array as a hash bucket can be
type BitSet struct { bit uint64}func (bs *BitSet) Add(i uint64) { bs.bit |= 1 << i}func (bs *BitSet) Del(i uint64) { bs.bit &= ^(1 << i)}func (bs BitSet) Has(i uint64) bool { return bs.bit&(1<<i) != 0}
Performance testing
func BenchmarkSetContains(b *testing.B) { bitset := NewBitSet() hashset := map[uint64]struct{}{} for _, i := range []uint64{1, 2, 4, 10} { bitset.Add(i) hashset[i] = struct{}{} } b.Run("bitset", func(b *testing.B) { for i := 0; i < b.N; i++ { for i := uint64(0); i < uint64(10); i++ { _ = bitset.Has(i) } } }) b.Run("hashset", func(b *testing.B) { for i := 0; i < b.N; i++ { for i := uint64(0); i < uint64(10); i++ { _, _ = hashset[i] } } })}
BenchmarkSetContains/bitset-8 500000000 3.81 ns/op 0 B/op 0 allocs/opBenchmarkSetContains/hashset-8 20000000 89.4 ns/op 0 B/op 0 allocs/op
You can see that Bitset has a performance boost of more than 20 times compared to HashSet
Reference links
- Code Address: https://github.com/hatlonely/...
Reprint please indicate the source
This article link: http://www.hatlonely.com/2018 ...