Skip to content

Commit

Permalink
Use atomic.OrUint32 to set bits in SyncFilter (#11)
Browse files Browse the repository at this point in the history
This is only enabled for Go 1.24 and up. It would work on 1.23, but then
atomic.OrUint32 would not get intrinsified on amd64, causing a pretty
big performance hit on that platform. Tested and benchmarked with
go1.24-2bffb8b3fb.

Checking whether the bit is already set, i.e.,

	if atomic.LoadUint32(p)&bit == 0 {
		atomic.OrUint32(p, bit)
	}

gives a 3× speedup for tiny Bloom filters (capacity 1e5) but slows
things down at capacity 1e7. We optimize for the larger filters, at
least for now.

    goos: linux
    goarch: amd64
    pkg: github.com/greatroar/blobloom/benchmarks
    cpu: Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
                 │ benchmarks/old │          benchmarks/check          │           benchmarks/new            │
                 │     sec/op     │   sec/op     vs base               │   sec/op     vs base                │
    Add1e5_1e2-8      14.81n ± 0%   13.99n ± 1%   -5.60% (p=0.002 n=6)   43.58n ± 1%  +194.13% (p=0.002 n=6)
    Add1e6_1e2-8      19.21n ± 1%   19.89n ± 1%   +3.51% (p=0.002 n=6)   47.74n ± 0%  +148.54% (p=0.002 n=6)
    Add1e7_1e2-8     103.70n ± 1%   89.63n ± 1%  -13.56% (p=0.002 n=6)   75.22n ± 2%   -27.47% (p=0.002 n=6)
    Add1e8_1e2-8      135.2n ± 0%   114.8n ± 1%  -15.16% (p=0.002 n=6)   100.9n ± 1%   -25.36% (p=0.002 n=6)
    Add1e5_1e3-8      21.14n ± 1%   19.50n ± 0%   -7.76% (p=0.002 n=6)   66.45n ± 0%  +214.28% (p=0.002 n=6)
    Add1e6_1e3-8      27.99n ± 0%   28.79n ± 1%   +2.82% (p=0.002 n=6)   70.29n ± 0%  +151.06% (p=0.002 n=6)
    Add1e7_1e3-8      149.0n ± 2%   122.3n ± 2%  -17.89% (p=0.002 n=6)   106.1n ± 2%   -28.83% (p=0.002 n=6)
    Add1e8_1e3-8      179.2n ± 0%   141.3n ± 0%  -21.20% (p=0.002 n=6)   125.3n ± 1%   -30.10% (p=0.002 n=6)
    geomean           53.09n        47.91n        -9.76%                 74.80n        +40.90%
  • Loading branch information
greatroar authored Sep 29, 2024
1 parent 14d1600 commit b9da674
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
test:
strategy:
matrix:
goversion: [1.16, 1.19]
goversion: [1.16, 1.19, 1.23]
runs-on: ubuntu-latest

steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.goversion }}
- name: Checkout
Expand All @@ -39,7 +39,7 @@ jobs:

steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19
- name: Install QEMU
Expand Down
16 changes: 0 additions & 16 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,3 @@ func getbitAtomic(b *block, i uint32) bool {
x := atomic.LoadUint32(&(*b)[(i/wordSize)%blockWords])
return x&bit != 0
}

// setbit sets bit (i modulo BlockBits) of b, atomically.
func setbitAtomic(b *block, i uint32) {
bit := uint32(1) << (i % wordSize)
p := &(*b)[(i/wordSize)%blockWords]

for {
old := atomic.LoadUint32(p)
if old&bit != 0 {
// Checking here instead of checking the return value from
// the CAS is between 50% and 80% faster on the benchmark.
return
}
atomic.CompareAndSwapUint32(p, old, old|bit)
}
}
27 changes: 27 additions & 0 deletions sync_go124.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 the Blobloom authors
//
// 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.

//go:build go1.24
// +build go1.24

package blobloom

import "sync/atomic"

// setbit sets bit (i modulo BlockBits) of b, atomically.
func setbitAtomic(b *block, i uint32) {
bit := uint32(1) << (i % wordSize)
p := &(*b)[(i/wordSize)%blockWords]
atomic.OrUint32(p, bit)
}
36 changes: 36 additions & 0 deletions sync_nogo124.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 the Blobloom authors
//
// 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.

//go:build !go1.24
// +build !go1.24

package blobloom

import "sync/atomic"

// setbit sets bit (i modulo BlockBits) of b, atomically.
func setbitAtomic(b *block, i uint32) {
bit := uint32(1) << (i % wordSize)
p := &(*b)[(i/wordSize)%blockWords]

for {
old := atomic.LoadUint32(p)
if old&bit != 0 {
// Checking here instead of checking the return value from
// the CAS is between 50% and 80% faster on the benchmark.
return
}
atomic.CompareAndSwapUint32(p, old, old|bit)
}
}

0 comments on commit b9da674

Please sign in to comment.