RoaringBitmap
A better compressed bitset in Java: used by Apache Spark, Netflix Atlas, Apache Pinot, Tablesaw, and many others
- 4k
- Java
- Apache-2.0
- 9 d ago
Specification of the compressed-bitmap Roaring format
Repo RoaringBitmap/RoaringFormatSpec, read from GitHub 1 min ago. RoaringBitmap has not joined Take My Repo.
RoaringBitmap/RoaringFormatSpec is a public repo on GitHub by RoaringBitmap. Specification of the compressed-bitmap Roaring format. It declares the Apache-2.0 license on GitHub. It is written mainly in Python, has 193 stars and last changed on 2026-11-10. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read from GitHub on 2026-11-23.
In the maintainer's words. Shown from GitHub; Take My Repo does not keep a copy.
This specification is also available as a web site: https://roaringbitmap.github.io/RoaringFormatSpec/
Roaring bitmaps are used by several important systems:
Many of these systems use the following interoperable format.
This specification assumes that you are familiar with Roaring bitmaps. Please refer to the following paper for details on the design rationale:
Let us recap that Roaring bitmaps are designed to store sets of 32-bit (unsigned) integers. Thus a Roaring bitmap can contain up to 4294967296 integers. They are made of three types of 16-bit containers: array, bitset and run containers. There are between 1 and 65536 (inclusively) containers. Each container has a cardinality (value in [1, 65536]), and it has a 16-bit most significant value (also called "key") in [0,65536). All containers are non-empty.
All words are written using little endian encoding.
The layout is designed so that random access to the data is possible without materializing the bitmap in memory while being storage efficient.
Throughout, we use the following constants :
The diagram below summarizes the two possible layouts, depending on the cookie, and the encoding of each type of container. Parts with a dashed outline are optional.
The cookie header spans either 64 bits or 32 bits followed by a variable number of bytes.
size be the number of containers. Then we store (size + 7) / 8 bytes, following the initial 32 bits, as a bitset to indicate whether each of the containers is a run container (bit set to 1) or not (bit set to 0). The first (least significant) bit of the first byte corresponds to the first stored container and so forth. In this scenario, the cookie header uses 32 bits followed by (size + 7) / 8 bytes.Thus it follows that the least significant 16 bits of the first 32 bits of a serialized bitmap should either have the value SERIAL_COOKIE_NO_RUNCONTAINER or the value SERIAL_COOKIE. In other cases, we should abort the decoding.
After scanning the cookie header, we know how many containers are present in the bitmap.
The cookie header is followed by a descriptive header. For each container, we store the key (16 most significant bits) along with the cardinality minus 1, using 16 bits for each value (for a total of 32 bits per container).
Thus, if there are x containers, the descriptive header will contain 32 x bits or 4 x bytes.
After scanning the descriptive header, we know the type of each container. Indeed, if the cookie took value SERIAL_COOKIE, then we had a bitset telling us which containers are run containers; otherwise, we know that there are no run containers. For the containers that are not run containers, then we use the cardinality to determine the type: a cardinality of up to and including 4096 indicates an array container whereas a cardinality above 4096 indicates a bitset container.
If and only if one of these is true
then we store (using a 32-bit value) the location (in bytes) of the container from the beginning of the stream (starting with the cookie) for each container.
The containers are then stored one after the other.
Consider the set made of the values 5, 10 and 1000, together with all 100 values in [131072, 131172), that is, from 2 × 65536 to 2 × 65536 + 99. The first three values share the key 0 and form an array container. The other values share the key 2 and form a run container made of a single run. With run containers, we use the SERIAL_COOKIE cookie. Since there are only two containers (fewer than NO_OFFSET_THRESHOLD), there is no offset header. The bitmap is serialized in 25 bytes:
3b 30 01 00 cookie: 12347 | ((2 - 1) << 16)
02 run flags: container 1 is a run container
00 00 02 00 key 0, cardinality - 1 = 2
02 00 63 00 key 2, cardinality - 1 = 99
05 00 0a 00 e8 03 array container: 5, 10, 1000
01 00 00 00 63 00 run container: 1 run, start 0, length - 1 = 99
A bitmap without run container can be stored in two different manners, with the SERIAL_COOKIE_NO_RUNCONTAINER cookie or with the SERIAL_COOKIE cookie. This means that you may find two different serialization for the exact same bitmap. Thus, in general, you may load a bitmap from disk, reading X bytes, and then write it back, using Y bytes, and X and Y could be different.
In practice, implementations can ensure isomorphism by, for example, always serializing bitmaps without run containers with the SERIAL_COOKIE_NO_RUNCONTAINER cookie. Yet if you load serialized bitmaps from another source, you should pay attention.
At a minimum, all Roaring implementations should be able to parse the two files in the testdata directory, see the associated README.md file found in the directory.
int startOffset = 0; boolean hasrun = hasRunContainer(); if (hasrun) { out.writeInt(Integer.reverseBytes(SERIAL_COOKIE | ((size - 1) << 16))); byte[] bitmapOfRunContainers = new byte[(size + 7) / 8]; for (int i = 0; i < size; ++i) { if (this.values[i] instanceof RunContainer) { bitmapOfRunContainers[i / 8] |= (1 << (i % 8)); } } out.write(bitmapOfRunContainers); if (this.size < NO_OFFSET_THRESHOLD) { startOffset = 4 + 4 * this.size + bitmapOfRunContainers.length; } else { startOffset = 4 + 8 * this.size + bitmapOfRunContainers.length; } } else { // backwards compatibility out.writeInt(Integer.reverseBytes(SERIAL_COOKIE_NO_RUNCONTAINER)); out.writeInt(Integer.reverseBytes(size)); startOffset = 4 + 4 + 4 * this.size + 4 * this.size; } for (int k = 0; k < size; ++k) { out.writeShort(Short.reverseBytes(this.keys[k])); out.writeShort(Short.reverseBytes((short) (this.values[k].getCardinality() - 1))); } if ((!hasrun) || (this.size >= NO_OFFSET_THRESHOLD)) { // writing the containers offsets for (int k = 0; k < this.size; k++) { out.writeInt(Integer.reverseBytes(startOffset)); startOffset = startOffset + this.values[k].getArraySizeInBytes(); } } for (int k = 0; k < size; ++k) { values[k].writeArray(out); }
char *initbuf = buf; uint32_t startOffset = 0; bool hasrun = ra_has_run_container(ra); if (hasrun) { uint32_t cookie = SERIAL_COOKIE | ((ra->size - 1) << 16); memcpy(buf, &cookie, sizeof(cookie)); buf += sizeof(cookie); uint32_t s = (ra->size + 7) / 8; uint8_t *bitmapOfRunContainers = (uint8_t *)calloc(s, 1); assert(bitmapOfRunContainers != NULL); // todo: handle for (int32_t i = 0; i < ra->size; ++i) { if (get_container_type(ra->containers[i], ra->typecodes[i]) == RUN_CONTAINER_TYPE_CODE) { bitmapOfRunContainers[i / 8] |= (1 << (i % 8)); } } memcpy(buf, bitmapOfRunContainers, s); buf += s; free(bitmapOfRunContainers); if (ra->size < NO_OFFSET_THRESHOLD) { startOffset = 4 + 4 * ra->size + s; } else { startOffset = 4 + 8 * ra->size + s; } } else { // backwards compatibility uint32_t cookie = SERIAL_COOKIE_NO_RUNCONTAINER; memcpy(buf, &cookie, sizeof(cookie)); buf += sizeof(cookie); memcpy(buf, &ra->size, sizeof(ra->size)); buf += sizeof(ra->size); startOffset = 4 + 4 + 4 * ra->size + 4 * ra->size; } for (int32_t k = 0; k < ra->size; ++k) { memcpy(buf, &ra->keys[k], sizeof(ra->keys[k])); buf += sizeof(ra->keys[k]); uint16_t card = container_get_cardinality(ra->containers[k], ra->typecodes[k]) - 1; memcpy(buf, &card, sizeof(card)); buf += sizeof(card); } if ((!hasrun) || (ra->size >= NO_OFFSET_THRESHOLD)) { // writing the containers offsets for (int32_t k = 0; k < ra->size; k++) { memcpy(buf, &startOffset, sizeof(startOffset)); buf += sizeof(startOffset); startOffset = startOffset + container_size_in_bytes(ra->containers[k], ra->typecodes[k]); } } for (int32_t k = 0; k < ra->size; ++k) { buf += container_write(ra->containers[k], ra->typecodes[k], buf); }
Java lacks native unsigned integers, but integers are still considered to be unsigned within Roaring, and ordered according to Integer.compareUnsigned.
For 32-bit integers in [0,2147483647], the unsigned and signed integers are indistinguishable since Java relies on a 32-bit two's complement format for its int type.
Some Roaring bitmap implementations may offer a 64-bit implementation. This section proposes a portable format, compatible with some (but not all) 64-bit implementations. This format is naturally compatible with implementations based on a conventional red-black-tree (as the serialization format is similar to the in-memory layout). The keys would be 32-bit integers representing the most significant 32 bits of elements whereas the values of the tree are 32-bit Roaring bitmaps. The 32-bit Roaring bitmaps represent the least significant bits of a set of elements.
All words are written using little endian encoding.
Java lacks native unsigned longs, but longs are still considered to be unsigned within Roaring, and ordered according to Long.compareUnsigned.
For 64-bit integers in [0,9223372036854775807], the unsigned and signed longs are indistinguishable since Java relies on a 64-bit two's complement format for its long type.
Java Roaring bitmaps implementation offers an ART-based 64-bit implementation. It may reach better performances (compression and/or computation). But as of 2022-11, it is not compatible with this Serialization format.
Java Roaring bitmaps implementation offers an Map-based 64-bit implementation handling signed longs.
It is not compatible with this serialization format (which does not handle signed keys).
See roaringbitmap.ksy and roaringbitmap64.ksy for the Kaitai Struct definitions of the Roaring bitmap format for 32-bit and 64-bit implementations respectively. (Offered on a best effort basis at this time.)
We recommend testing your data files and our Kaitai struct definitions in the Kaitai Web IDE.
RoaringBitmap/RoaringFormatSpec is a public repo on GitHub by RoaringBitmap. Specification of the compressed-bitmap Roaring format.
It declares the Apache-2.0 license on GitHub.
Its maker has not tagged a system, so it is unknown here. Its README says what it needs.
Open "Take it" on its page: Take My Repo writes an install plan for your machine and hands it to your AI agent (Claude Code, Codex or Cursor), which asks you before each step. Take My Repo has not tested it on any machine.
Its last change on GitHub was on 2026-11-10. GitHub counts 2 open issues and pull requests. Take My Repo does not judge from that whether it is maintained.
Nobody has reviewed it on Take My Repo yet, which says nothing about its quality.
A better compressed bitset in Java: used by Apache Spark, Netflix Atlas, Apache Pinot, Tablesaw, and many others

Roaring bitmaps in Go (golang), used by InfluxDB, Bleve, DataDog
Roaring bitmaps in C (and C++), with SIMD (AVX2, AVX-512 and NEON) optimizations: used by Apache Doris, ClickHouse, Alibaba Tair, Redpanda, YDB and StarRocks
A better compressed bitset in Swift http://roaringbitmap.org/
Take My Repo has not tested this on your machine. Requirements come from the project's own documentation.