Repository navigation
Answer short read-only queries in Go; update to CRoaring 5.5.5; detailed comparison with roaring - #34
Merged
Merged
Conversation
Contains, Rank, Minimum, Maximum, Select and Intersect read the CRoaring data structures directly from Go, through the struct types that cgo derives from roaring.h. A cgo call costs about 40 ns, which dominates these queries on small or early-exit inputs. Rank, Select and Intersect are faster in C once they have much work to do (many containers to skip, long arrays to merge), so they do a bounded amount of work in Go and otherwise start over in C. Portable frozen views may hold unaligned arrays: the Go code is used on amd64 and arm64 only, and the gocroaring_cqueries build tag selects the C versions everywhere. Writes still go through C. Tests compare every query with its C version over all container types, shared containers, frozen views (including unaligned ones), with the default and with unlimited work budgets. CI also runs the tests with the C versions and cross-compiles for riscv64.
…ring on large bitmaps CRoaring 5.5.2 and 5.5.3 speed up roaring_bitmap_select (skipping containers by cardinality) and the 64-bit lookups. Since C's Select is now fast, the Go-side Select only handles bitmaps with at most 16 containers. The 64-bit benchmarks cycle through many probes over sparse, clustered and dense bitmaps: Contains, Rank, Select, Remove and Add, appending, iteration, cardinality, Or, And and AndCardinality.
…ies and CRoaring 5.5.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR updates the bundled CRoaring from 5.5.1 to 5.5.5. It also answers
Contains,Rank,Minimum,Maximum,SelectandIntersectin Go, reading the CRoaring data structures directly, without a cgo call.Go-side read-only queries
The struct types come from the bundled
roaring.hthrough cgo, so the layouts always match, and a renamed field breaks the build instead of the results. A cgo call costs about 40 ns, which dominates these queries when the bitmaps are small or the answer comes early.C is faster per step on long queries, so:
Rankuses Go only when at most 16 containers come before the value; it decides this up front with a key search.Selectuses Go only on bitmaps with at most 16 containers. Since CRoaring 5.5.2, C'sSelectskips containers quickly (CRoaring #954).Intersecttakes at most 24 steps in Go (keys skipped or values compared), then starts over in C.Alignment. Portable frozen views may hold unaligned arrays. The Go code is used only on amd64 and arm64. Other architectures use the C calls, and the
gocroaring_cqueriesbuild tag forces the C versions everywhere.Writes still go through C.
Benchmarks
Xeon Gold 6548N (big4), one pinned core, medians of 6 interleaved runs, ns per call. Probes are cycled so the branch predictor can't learn the answers. Reproduce with
go test -run '^$' -bench 'Queries|Intersect'; the/Csub-benchmarks call CRoaring.Worst cases: disjoint intersections that end up in C cost at most about 65 ns more than C alone.
CRoaring 5.5.1 → 5.5.3
To separate the CRoaring update from the Go-side queries, I benchmarked three builds, interleaved:
master(CRoaring 5.5.1);-tags gocroaring_cqueries(5.5.3, queries in C);It covers every benchmark in the package (including the new
bench64_test.go) and all 58 pairs inbenchmarks/compare.bench64_test.go, new: sparse, clustered and dense bitmaps of 1M values):benchmarks/comparegeometric mean −0.6%).benchmarks/compare(CRoaring side): geometric mean −6.1%. Contains −47%; Intersects −16 to −86% except ArrayArray, which is +22% (149 → 181 ns, many sparse containers with no early match). Everything else is unchanged.CRoaring 5.5.3 → 5.5.5
Selectin bitset containers (CRoaring #956), and the container slot fix below (#958).Intersectsnow stops at the first common value. In 5.5.4 it took up to 250 ms on 1M-value bitmaps; it now takes about 75 ns. It also allows unaligned loads in more helpers that read frozen views (a UBSan report on arm64 with clang).Compared with roaring (pure Go)
benchmarks/comparegrows from 58 to 190 benchmarks (compare_more_test.go):OrCardinality, and zero-copy views;The README now has a detailed comparison: a summary by category and a collapsible table per category, with times on x86-64 (Xeon Gold 6548N, Go 1.26.10) and arm64 (Apple M4 Max, Go 1.24.3), medians of 10 runs, CRoaring 5.5.5.
Tests
queries_test.gocompares every query with its C version. It covers:Each test runs with the default budgets and with unlimited ones, so the Go code also handles the long queries. Tests pass under
-race. CI also runs the tests with-tags gocroaring_cqueriesand cross-compiles for riscv64 (C path).Found along the way
The 64-bit benchmarks showed that removing and re-adding a value on a sparse 64-bit bitmap took 184 µs: finding the next free container slot scanned the whole array. This is fixed in CRoaring #958 (5.5.4).