Skip to content

Answer short read-only queries in Go; update to CRoaring 5.5.5; detailed comparison with roaring - #34

Merged
lemire merged 7 commits into
masterfrom
go-side-queries
Nov 4, 2026
Merged

lemire merged 7 commits into
masterfrom
go-side-queries

Conversation

@lemire

@lemire lemire commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

This PR updates the bundled CRoaring from 5.5.1 to 5.5.5. It also answers Contains, Rank, Minimum, Maximum, Select and Intersect in Go, reading the CRoaring data structures directly, without a cgo call.

Go-side read-only queries

The struct types come from the bundled roaring.h through 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:

  • Rank uses Go only when at most 16 containers come before the value; it decides this up front with a key search.
  • Select uses Go only on bitmaps with at most 16 containers. Since CRoaring 5.5.2, C's Select skips containers quickly (CRoaring #954).
  • Intersect takes 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_cqueries build 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 /C sub-benchmarks call CRoaring.

Query Bitmap C Go
Contains bitset / array / run / mixed 47–87 11–34
Contains 1M values over 2^26 95 52
Minimum + Maximum any 75 4.5–5.4
Rank 3 containers (array / run) 53–56 18–27
Rank 16 bitset containers 258 220
Rank 256 to 1024 containers (falls back to C) 632–1881 641–1936
Select 3 containers (array / run) 41–60 13–30
Select 16 bitset containers 370 335
Select 256 to 1024 containers (C) 505–692 508–700
Intersect, early exit every container pair 48–67 9–18
Intersect, disjoint run-run / array-bitset 118 / 282 73 / 239
Intersect, disjoint array-array / bitset-bitset / bitset-run 1021 / 1825 / 2037 1049 / 1797 / 2111
Intersect, disjoint array-run 217 250
Intersect, disjoint tiny array vs array 124 187

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);
  • this PR with -tags gocroaring_cqueries (5.5.3, queries in C);
  • this PR.

It covers every benchmark in the package (including the new bench64_test.go) and all 58 pairs in benchmarks/compare.

  • 64-bit (bench64_test.go, new: sparse, clustered and dense bitmaps of 1M values):
    • faster: Contains on sparse −6%, appending −4 to −6%, And −7 to −12%, Or on clustered −4 to −7%;
    • unchanged otherwise, except one-by-one iteration, +1 to +3.5%.
  • 32-bit, queries in C: unchanged (benchmarks/compare geometric mean −0.6%).
  • This PR against master, 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

  • 5.5.4: faster Select in bitset containers (CRoaring #956), and the container slot fix below (#958).
  • 5.5.5: 64-bit Intersects now 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/compare grows from 58 to 190 benchmarks (compare_more_test.go):

  • 32-bit queries and updates over four shapes (small, sparse arrays, dense bitsets, runs);
  • in-place operations, OrCardinality, and zero-copy views;
  • a 64-bit suite over sparse, clustered and dense bitmaps.

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.

gocroaring faster roaring faster within 5% geometric mean
x86-64 133 45 12 1.47×
arm64 116 60 14 1.23×

Tests

queries_test.go compares every query with its C version. It covers:

  • every container type and every container pair;
  • shared (copy-on-write) containers;
  • frozen views, including a portable frozen view at an odd address;
  • empty, full and top-of-range bitmaps.

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_cqueries and 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).

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.
@lemire lemire changed the title Answer the short read-only queries in Go, without a cgo call Answer short read-only queries in Go; update to CRoaring 5.5.3 Nov 4, 2026
@lemire lemire changed the title Answer short read-only queries in Go; update to CRoaring 5.5.3 Answer short read-only queries in Go; update to CRoaring 5.5.5; detailed comparison with roaring Nov 4, 2026
@lemire
lemire merged commit 3b77d0f into master Nov 4, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant