This library provides a fast, memory-efficient Go implementation of roaring bitmaps, a compressed bitmap data structure for sets of 32-bit integers. It is designed for high-throughput analytics, set operations, and efficient serialization. While most of you should probably use the original, well maintained implementation, this implementation uses kelindar/bitmap for its dense implementation, and tries to optimize AND/AND NOT/OR,XOR operations.
- High Performance: Optimized for fast set operations (AND, OR, XOR, AND NOT) and iteration.
- Memory Efficient: Uses containerization and compression for sparse and dense data.
- Go Idioms: Clean, concise API with Go-style patterns and minimal dependencies.
Use When
- β You need to store and manipulate large sets of 32-bit integers efficiently.
- β You want fast set operations (union, intersection, difference, symmetric difference).
- β You want a dependency-free, pure Go implementation.
Not For:
- β If you need a mature, and interoperable implementation.
- β Sets of non-integer or non-uint32 data.
import "github.com/kelindar/roaring"
func main() {
// Create a new bitmap
bm := roaring.New()
// Add values
bm.Set(1)
bm.Set(42)
bm.Set(100000)
// Check membership
if bm.Contains(42) {
// Do something
}
// Remove a value
bm.Remove(1)
// Count values
fmt.Println("Count:", bm.Count())
// Iterate values
bm.Range(func(x uint32) {
fmt.Println(x)
})
// Set operations
bm2 := roaring.New()
bm2.Set(42)
bm2.Set(7)
bm.Or(bm2) // Union
// Serialization
data := bm.ToBytes()
bm3 := roaring.FromBytes(data)
fmt.Println(bm3.Contains(42)) // true
}Set(x uint32): Add a value.Remove(x uint32): Remove a value.Contains(x uint32) bool: Check if a value is present.Count() int: Number of values in the bitmap.Range(func(x uint32)): Iterate all values.And,Or,Xor,AndNot: Set operations.ToBytes,FromBytes,WriteTo,ReadFrom: Serialization.
name time/op ops/s allocs/op vs ref
-------------------- ------------ ------------ ------------ ------------------
set 1K (seq) 18.9 ns 52.9M 0 π° similar
set 1K (rnd) 30.4 ns 32.9M 0 π° similar
set 1K (sps) 20.0 ns 49.9M 0 π° similar
set 1K (dns) 20.7 ns 48.4M 0 π° similar
set 1M (seq) 12.0 ns 83.5M 0 β
+12%
set 1M (rnd) 19.4 ns 51.6M 0 β
+7%
set 1M (sps) 30.1 ns 33.2M 0 β
+31%
set 1M (dns) 13.1 ns 76.2M 0 β
+9%
has 1K (seq) 17.4 ns 57.5M 0 π° similar
has 1K (rnd) 25.6 ns 39.0M 0 π° similar
has 1K (sps) 13.3 ns 75.5M 0 β
+35%
has 1K (dns) 20.0 ns 50.0M 0 π° similar
has 1M (seq) 8.7 ns 115.2M 0 β
+22%
has 1M (rnd) 18.3 ns 54.7M 0 π° similar
has 1M (sps) 26.4 ns 37.9M 0 β
+31%
has 1M (dns) 12.2 ns 82.2M 0 π° similar
del 1K (seq) 6.8 ns 146.6M 0 β -13%
del 1K (rnd) 6.8 ns 146.8M 0 β -10%
del 1K (sps) 6.7 ns 148.2M 0 β -9%
del 1K (dns) 6.8 ns 147.8M 0 β -10%
del 1M (seq) 6.8 ns 146.4M 0 π° similar
del 1M (rnd) 20.4 ns 49.1M 0 β
+12%
del 1M (sps) 12.1 ns 82.6M 0 β
+86%
del 1M (dns) 38.7 ns 25.8M 0 β
+34%
and 1K (seq) 888.3 ns 1.1M 4 β -7%
and 1K (rnd) 633.3 ns 1.6M 4 π° similar
and 1K (sps) 1.4 Β΅s 710.9K 19 π° similar
and 1K (dns) 181.9 ns 5.5M 4 β -10%
and 1M (seq) 24.6 Β΅s 40.7K 19 β
+28%
and 1M (rnd) 23.5 Β΅s 42.5K 19 β
+27%
and 1M (sps) 3.8 ms 265 15.3K π° similar
and 1M (dns) 3.8 Β΅s 261.8K 5 β
+2.2x
or 1K (seq) 1.7 Β΅s 589.4K 16 β -10%
or 1K (rnd) 1.5 Β΅s 661.2K 16 β -30%
or 1K (sps) 1.8 Β΅s 552.7K 32 β
+16%
or 1K (dns) 345.9 ns 2.9M 12 β
+17%
or 1M (seq) 25.7 Β΅s 39.0K 27 β
+17%
or 1M (rnd) 24.0 Β΅s 41.7K 27 β
+17%
or 1M (sps) 3.9 ms 258 15.3K β
+9%
or 1M (dns) 2.9 Β΅s 344.9K 8 β
+273x
xor 1K (seq) 1.6 Β΅s 613.1K 16 β -37%
xor 1K (rnd) 1.1 Β΅s 947.1K 14 β -25%
xor 1K (sps) 1.8 Β΅s 566.2K 32 β
+8%
xor 1K (dns) 219.6 ns 4.6M 7 β
+14x
xor 1M (seq) 24.6 Β΅s 40.7K 27 β
+97%
xor 1M (rnd) 26.5 Β΅s 37.7K 27 β
+92%
xor 1M (sps) 4.0 ms 251 15.3K π° similar
xor 1M (dns) 2.9 Β΅s 339.7K 8 β
+9.1x
andnot 1K (seq) 898.5 ns 1.1M 4 π° similar
andnot 1K (rnd) 697.9 ns 1.4M 4 π° similar
andnot 1K (sps) 1.4 Β΅s 727.6K 19 β
+14%
andnot 1K (dns) 161.0 ns 6.2M 4 β
+30x
andnot 1M (seq) 23.8 Β΅s 42.0K 19 β
+32%
andnot 1M (rnd) 85.3 Β΅s 11.7K 19 β -61%
andnot 1M (sps) 3.7 ms 270 15.3K π° similar
andnot 1M (dns) 2.8 Β΅s 354.3K 5 β
+10.1x
range 1K (seq) 501.4 ns 2.0M 0 π° similar
range 1K (rnd) 429.1 ns 2.3M 0 π° similar
range 1K (sps) 583.6 ns 1.7M 0 β
+22%
range 1K (dns) 114.5 ns 8.7M 0 π° similar
range 1M (seq) 2.4 ms 413 0 β -37%
range 1M (rnd) 2.2 ms 461 0 β -41%
range 1M (sps) 554.2 Β΅s 1.8K 0 β
+12%
range 1M (dns) 218.2 Β΅s 4.6K 0 β
+39%Bench is MIT licensed and maintained by @kelindar. PRs and issues welcome!
