Roaring bitmaps in C (and C++)

Overview

CRoaring Build Status Build status Build Status

Portable Roaring bitmaps in C (and C++) with full support for your favorite compiler (GNU GCC, LLVM's clang, Visual Studio). Included in the Awesome C list of open source C software.

Introduction

Bitsets, also called bitmaps, are commonly used as fast data structures. Unfortunately, they can use too much memory. To compensate, we often use compressed bitmaps.

Roaring bitmaps are compressed bitmaps which tend to outperform conventional compressed bitmaps such as WAH, EWAH or Concise. They are used by several major systems such as Apache Lucene and derivative systems such as Solr and Elasticsearch, Metamarkets' Druid, LinkedIn Pinot, Netflix Atlas, Apache Spark, OpenSearchServer, Cloud Torrent, Whoosh, InfluxDB, Pilosa, Bleve, Microsoft Visual Studio Team Services (VSTS), and eBay's Apache Kylin. The CRoaring library is used in several systems such as Apache Doris. The YouTube SQL Engine, Google Procella, uses Roaring bitmaps for indexing.

We published a peer-reviewed article on the design and evaluation of this library:

  • Roaring Bitmaps: Implementation of an Optimized Software Library, Software: Practice and Experience 48 (4), 2018 arXiv:1709.07821

Roaring bitmaps are found to work well in many important applications:

Use Roaring for bitmap compression whenever possible. Do not use other bitmap compression methods (Wang et al., SIGMOD 2017)

There is a serialized format specification for interoperability between implementations. Hence, it is possible to serialize a Roaring Bitmap from C++, read it in Java, modify it, serialize it back and read it in Go and Python.

Objective

The primary goal of the CRoaring is to provide a high performance low-level implementation that fully take advantage of the latest hardware. Roaring bitmaps are already available on a variety of platform through Java, Go, Rust... implementations. CRoaring is a library that seeks to achieve superior performance by staying close to the latest hardware.

(c) 2016-... The CRoaring authors.

Requirements

  • Linux, macOS, FreeBSD, Windows (MSYS2 and Microsoft Visual studio).
  • We test the library with ARM, x64/x86 and POWER processors. We only support little endian systems (big endian systems are vanishingly rare).
  • Recent C compiler supporting the C11 standard (GCC 4.8 or better or clang), there is also an optional C++ class that requires a C++ compiler supporting the C++11 standard.
  • CMake (to contribute to the project, users can rely on amalgamation/unity builds if they do not wish to use CMake).

Using a CMake subdirectory

If you like CMake, you can just drop CRoaring in your project as a subdirectory and get going. See our demonstration for further details.

Amalgamation/Unity Build

The CRoaring library can be amalgamated into a single source file that makes it easier for integration into other projects. Moreover, by making it possible to compile all the critical code into one compilation unit, it can improve the performance. For the rationale, please see the SQLite documentation, https://www.sqlite.org/amalgamation.html, or the corresponding Wikipedia entry (https://en.wikipedia.org/wiki/Single_Compilation_Unit). Users who choose this route, do not need to rely on CRoaring's build system (based on CMake).

We maintain pre-generated amalgamated files at https://github.com/lemire/CRoaringUnityBuild for your convenience.

To generate the amalgamated files yourself, you can invoke a bash script...

./amalgamation.sh

(Bash shells are standard under Linux and macOS. Bash shells are available under Windows as part of the  GitHub Desktop under the name Git Shell. So if you have cloned the CRoaring GitHub repository from within the GitHub Desktop, you can right-click on CRoaring, select Git Shell and then enter the above commands.)

It is not necessary to invoke the script in the CRoaring directory. You can invoke it from any directory where you want the amalgamation files to be written.

It will generate three files for C users: roaring.h, roaring.c and amalgamation_demo.c... as well as some brief instructions. The amalgamation_demo.c file is a short example, whereas roaring.h and roaring.c are "amalgamated" files (including all source and header files for the project). This means that you can simply copy the files roaring.h and roaring.c into your project and be ready to go! No need to produce a library! See the amalgamation_demo.c file.

For example, you can use the C code as follows:

#include <stdio.h>
#include "roaring.c"
int main() {
  roaring_bitmap_t *r1 = roaring_bitmap_create();
  for (uint32_t i = 100; i < 1000; i++) roaring_bitmap_add(r1, i);
  printf("cardinality = %d\n", (int) roaring_bitmap_get_cardinality(r1));
  roaring_bitmap_free(r1);
  return 0;
}

The script will also generate C++ files for C++ users, including an example. You can use the C++ as follows.

#include <iostream>

#include "roaring.hh"
#include "roaring64map.hh"

using namespace roaring;
int main() {
    Roaring r1;
    for (uint32_t i = 100; i < 1000; i++) {
        r1.add(i);
    }
    std::cout << "cardinality = " << r1.cardinality() << std::endl;

    Roaring64Map r2;
    for (uint64_t i = 18000000000000000100ull; i < 18000000000000001000ull;
         i++) {
        r2.add(i);
    }
    std::cout << "cardinality = " << r2.cardinality() << std::endl;
    return EXIT_SUCCESS;
}

If you prefer a silent output, you can use the following command to redirect stdout :

./amalgamation.sh > /dev/null

API

The C interface is found in the file include/roaring/roaring.h. We have C++ interface at cpp/roaring.hh.

Dealing with large volumes

Some users have to deal with large volumes of data. It may be important for these users to be aware of the addMany (C++) roaring_bitmap_or_many (C) functions as it is much faster and economical to add values in batches when possible. Furthermore, calling periodically the runOptimize (C++) or roaring_bitmap_run_optimize (C) functions may help.

Example (C)

#include <roaring/roaring.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

bool roaring_iterator_sumall(uint32_t value, void *param) {
    *(uint32_t *)param += value;
    return true;  // iterate till the end
}

int main() {
    // create a new empty bitmap
    roaring_bitmap_t *r1 = roaring_bitmap_create();
    // then we can add values
    for (uint32_t i = 100; i < 1000; i++) roaring_bitmap_add(r1, i);
    // check whether a value is contained
    assert(roaring_bitmap_contains(r1, 500));
    // compute how many bits there are:
    uint32_t cardinality = roaring_bitmap_get_cardinality(r1);
    printf("Cardinality = %d \n", cardinality);

    // if your bitmaps have long runs, you can compress them by calling
    // run_optimize
    uint32_t expectedsizebasic = roaring_bitmap_portable_size_in_bytes(r1);
    roaring_bitmap_run_optimize(r1);
    uint32_t expectedsizerun = roaring_bitmap_portable_size_in_bytes(r1);
    printf("size before run optimize %d bytes, and after %d bytes\n",
           expectedsizebasic, expectedsizerun);

    // create a new bitmap containing the values {1,2,3,5,6}
    roaring_bitmap_t *r2 = roaring_bitmap_of(5, 1, 2, 3, 5, 6);
    roaring_bitmap_printf(r2);  // print it

    // we can also create a bitmap from a pointer to 32-bit integers
    uint32_t somevalues[] = {2, 3, 4};
    roaring_bitmap_t *r3 = roaring_bitmap_of_ptr(3, somevalues);

    // we can also go in reverse and go from arrays to bitmaps
    uint64_t card1 = roaring_bitmap_get_cardinality(r1);
    uint32_t *arr1 = (uint32_t *)malloc(card1 * sizeof(uint32_t));
    assert(arr1 != NULL);
    roaring_bitmap_to_uint32_array(r1, arr1);
    roaring_bitmap_t *r1f = roaring_bitmap_of_ptr(card1, arr1);
    free(arr1);
    assert(roaring_bitmap_equals(r1, r1f));  // what we recover is equal
    roaring_bitmap_free(r1f);

    // we can go from arrays to bitmaps from "offset" by "limit"
    size_t offset = 100;
    size_t limit = 1000;
    uint32_t *arr3 = (uint32_t *)malloc(limit * sizeof(uint32_t));
    assert(arr3 != NULL);
    roaring_bitmap_range_uint32_array(r1, offset, limit, arr3);
    free(arr3);

    // we can copy and compare bitmaps
    roaring_bitmap_t *z = roaring_bitmap_copy(r3);
    assert(roaring_bitmap_equals(r3, z));  // what we recover is equal
    roaring_bitmap_free(z);

    // we can compute union two-by-two
    roaring_bitmap_t *r1_2_3 = roaring_bitmap_or(r1, r2);
    roaring_bitmap_or_inplace(r1_2_3, r3);

    // we can compute a big union
    const roaring_bitmap_t *allmybitmaps[] = {r1, r2, r3};
    roaring_bitmap_t *bigunion = roaring_bitmap_or_many(3, allmybitmaps);
    assert(
        roaring_bitmap_equals(r1_2_3, bigunion));  // what we recover is equal
    // can also do the big union with a heap
    roaring_bitmap_t *bigunionheap =
        roaring_bitmap_or_many_heap(3, allmybitmaps);
    assert(roaring_bitmap_equals(r1_2_3, bigunionheap));

    roaring_bitmap_free(r1_2_3);
    roaring_bitmap_free(bigunion);
    roaring_bitmap_free(bigunionheap);

    // we can compute intersection two-by-two
    roaring_bitmap_t *i1_2 = roaring_bitmap_and(r1, r2);
    roaring_bitmap_free(i1_2);

    // we can write a bitmap to a pointer and recover it later
    uint32_t expectedsize = roaring_bitmap_portable_size_in_bytes(r1);
    char *serializedbytes = malloc(expectedsize);
    roaring_bitmap_portable_serialize(r1, serializedbytes);
    roaring_bitmap_t *t = roaring_bitmap_portable_deserialize(serializedbytes);
    assert(roaring_bitmap_equals(r1, t));  // what we recover is equal
    roaring_bitmap_free(t);
    // we can also check whether there is a bitmap at a memory location without
    // reading it
    size_t sizeofbitmap =
        roaring_bitmap_portable_deserialize_size(serializedbytes, expectedsize);
    assert(sizeofbitmap ==
           expectedsize);  // sizeofbitmap would be zero if no bitmap were found
    // we can also read the bitmap "safely" by specifying a byte size limit:
    t = roaring_bitmap_portable_deserialize_safe(serializedbytes, expectedsize);
    assert(roaring_bitmap_equals(r1, t));  // what we recover is equal
    roaring_bitmap_free(t);

    free(serializedbytes);

    // we can iterate over all values using custom functions
    uint32_t counter = 0;
    roaring_iterate(r1, roaring_iterator_sumall, &counter);

    // we can also create iterator structs
    counter = 0;
    roaring_uint32_iterator_t *i = roaring_create_iterator(r1);
    while (i->has_value) {
        counter++;  // could use    i->current_value
        roaring_advance_uint32_iterator(i);
    }
    // you can skip over values and move the iterator with
    // roaring_move_uint32_iterator_equalorlarger(i,someintvalue)

    roaring_free_uint32_iterator(i);
    // roaring_bitmap_get_cardinality(r1) == counter

    // for greater speed, you can iterate over the data in bulk
    i = roaring_create_iterator(r1);
    uint32_t buffer[256];
    while (1) {
        uint32_t ret = roaring_read_uint32_iterator(i, buffer, 256);
        for (uint32_t j = 0; j < ret; j++) {
            counter += buffer[j];
        }
        if (ret < 256) {
            break;
        }
    }
    roaring_free_uint32_iterator(i);

    roaring_bitmap_free(r1);
    roaring_bitmap_free(r2);
    roaring_bitmap_free(r3);
    return EXIT_SUCCESS;
}

Example (C++)

#include <iostream>

#include "roaring.hh"

using namespace roaring;

int main() {
    Roaring r1;
    for (uint32_t i = 100; i < 1000; i++) {
        r1.add(i);
    }

    // check whether a value is contained
    assert(r1.contains(500));

    // compute how many bits there are:
    uint32_t cardinality = r1.cardinality();

    // if your bitmaps have long runs, you can compress them by calling
    // run_optimize
    uint32_t size = r1.getSizeInBytes();
    r1.runOptimize();

    // you can enable "copy-on-write" for fast and shallow copies
    r1.setCopyOnWrite(true);

    uint32_t compact_size = r1.getSizeInBytes();
    std::cout << "size before run optimize " << size << " bytes, and after "
              << compact_size << " bytes." << std::endl;

    // create a new bitmap with varargs
    Roaring r2 = Roaring::bitmapOf(5, 1, 2, 3, 5, 6);

    r2.printf();
    printf("\n");

    // we can also create a bitmap from a pointer to 32-bit integers
    const uint32_t values[] = {2, 3, 4};
    Roaring r3(3, values);

    // we can also go in reverse and go from arrays to bitmaps
    uint64_t card1 = r1.cardinality();
    uint32_t *arr1 = new uint32_t[card1];
    r1.toUint32Array(arr1);
    Roaring r1f(card1, arr1);
    delete[] arr1;

    // bitmaps shall be equal
    assert(r1 == r1f);

    // we can copy and compare bitmaps
    Roaring z(r3);
    assert(r3 == z);

    // we can compute union two-by-two
    Roaring r1_2_3 = r1 | r2;
    r1_2_3 |= r3;

    // we can compute a big union
    const Roaring *allmybitmaps[] = {&r1, &r2, &r3};
    Roaring bigunion = Roaring::fastunion(3, allmybitmaps);
    assert(r1_2_3 == bigunion);

    // we can compute intersection two-by-two
    Roaring i1_2 = r1 & r2;

    // we can write a bitmap to a pointer and recover it later
    uint32_t expectedsize = r1.getSizeInBytes();
    char *serializedbytes = new char[expectedsize];
    r1.write(serializedbytes);
    Roaring t = Roaring::read(serializedbytes);
    assert(r1 == t);
    delete[] serializedbytes;

    // we can iterate over all values using custom functions
    uint32_t counter = 0;
    r1.iterate(
        [](uint32_t value, void *param) {
            *(uint32_t *)param += value;
            return true;
        },
        &counter);

    // we can also iterate the C++ way
    counter = 0;
    for (Roaring::const_iterator i = t.begin(); i != t.end(); i++) {
        ++counter;
    }
    // counter == t.cardinality()

    // we can move iterators to skip values
    const uint32_t manyvalues[] = {2, 3, 4, 7, 8};
    Roaring rogue(5, manyvalues);
    Roaring::const_iterator j = rogue.begin();
    j.equalorlarger(4);  // *j == 4
    return EXIT_SUCCESS;
}

Building with cmake (Linux and macOS, Visual Studio users should see below)

CRoaring follows the standard cmake workflow. Starting from the root directory of the project (CRoaring), you can do:

mkdir -p build
cd build
cmake ..
make
# follow by 'make test' if you want to test.
# you can also type 'make install' to install the library on your system
# C header files typically get installed to /usr/local/include/roaring
# whereas C++ header files get installed to /usr/local/include/roaring

(You can replace the build directory with any other directory name.) You can generate a static library by adding -DROARING_BUILD_STATIC=ON to the command line. By default all tests are built on all platforms, to skip building and running tests add -DENABLE_ROARING_TESTS=OFF to the command line.

As with all cmake projects, you can specify the compilers you wish to use by adding (for example) -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ to the cmake command line.

If you wish to build an x64 version while disabling AVX2 and BMI2 support at the expense of performance, you can do the following :

mkdir -p buildnoavx
cd buildnoavx
cmake -DROARING_DISABLE_AVX=ON ..
make

The reverse is also possible. Some compilers may not enable AVX2 support, but you can force it in the following manner:

mkdir -p buildwithavx
cd buildwithavx
cmake -DFORCE_AVX=ON ..
make

If you have x64 hardware, but you wish to disable all x64-specific optimizations (including AVX), then you can do the following...

mkdir -p buildnox64
cd buildnoavx
cmake -DROARING_DISABLE_X64=ON ..
make

We tell the compiler to target the architecture of the build machine by using the march=native flag. This give the compiler the freedom to use instructions that your CPU support, but can be dangerous if you are going to use the built binaries on different machines. For example, you could get a SIGILL crash if you run the code on an older machine which does not have some of the instructions (e.g. POPCOUNT). There are two ways to deal with this:

First, you can disable this feature altogether by specifying -DROARING_DISABLE_NATIVE=OFF:

mkdir -p buildnonative
cd buildnoavx
cmake -DROARING_DISABLE_NATIVE=ON ..
make

Second, you can specify the architecture by specifying -DROARING_ARCH=arch. For example, if you have many servers but the oldest server is running the Intel westmere architecture, you can specify -DROARING_ARCH=westmere. You can find out the list of valid architecture values by typing man gcc. If -DROARING_DISABLE_NATIVE=on is specified, then this option has no effect.

mkdir -p build_westmere
cd build_westmere
cmake -DROARING_ARCH=westmere ..
make

For a debug release, starting from the root directory of the project (CRoaring), try

mkdir -p debug
cd debug
cmake -DCMAKE_BUILD_TYPE=Debug -DROARING_SANITIZE=ON ..
make

(Again, you can use the -DROARING_DISABLE_AVX=ON flag if you need it.)

(Of course you can replace the debug directory with any other directory name.)

To run unit tests (you must first run make):

make test

The detailed output of the tests can be found in Testing/Temporary/LastTest.log.

To run real-data benchmark

./real_bitmaps_benchmark ../benchmarks/realdata/census1881

where you must adjust the path "../benchmarks/realdata/census1881" so that it points to one of the directories in the benchmarks/realdata directory.

To check that your code abides by the style convention (make sure that clang-format is installed):

./tools/clang-format-check.sh

To reformat your code according to the style convention (make sure that clang-format is installed):

./tools/clang-format.sh

Building (Visual Studio under Windows)

We are assuming that you have a common Windows PC with at least Visual Studio 2015, and an x64 processor.

To build with at least Visual Studio 2015 from the command line:

  • Grab the CRoaring code from GitHub, e.g., by cloning it using GitHub Desktop.
  • Install CMake. When you install it, make sure to ask that cmake be made available from the command line.
  • Create a subdirectory within CRoaring, such as VisualStudio.
  • Using a shell, go to this newly created directory. For example, within GitHub Desktop, you can right-click on  CRoaring in your GitHub repository list, and select Open in Git Shell, then type cd VisualStudio in the newly created shell.
  • Type cmake -DCMAKE_GENERATOR_PLATFORM=x64 .. in the shell while in the VisualStudio repository. (Alternatively, if you want to build a static library, you may use the command line cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DROARING_BUILD_STATIC=ON ...)
  • This last command created a Visual Studio solution file in the newly created directory (e.g., RoaringBitmap.sln). Open this file in Visual Studio. You should now be able to build the project and run the tests. For example, in the Solution Explorer window (available from the View menu), right-click ALL_BUILD and select Build. To test the code, still in the Solution Explorer window, select RUN_TESTS and select Build.

To build with at least Visual Studio 2017 directly in the IDE:

  • Grab the CRoaring code from GitHub, e.g., by cloning it using GitHub Desktop.
  • Select the Visual C++ tools for CMake optional component when installing the C++ Development Workload within Visual Studio.
  • Within Visual Studio use File > Open > Folder... to open the CRoaring folder.
  • Right click on CMakeLists.txt in the parent directory within Solution Explorer and select Build to build the project.
  • For testing, in the Standard toolbar, drop the Select Startup Item... menu and choose one of the tests. Run the test by pressing the button to the left of the dropdown.

We have optimizations specific to AVX2 in the code, and they are turned only if the __AVX2__ macro is defined. In turn, these optimizations should only be enabled if you know that your target machines will support AVX2. Given that all recent Intel and AMD processors support AVX2, you may want to make this assumption. Thankfully, Visual Studio does define the __AVX2__ macro whenever the /arch:AVX2 compiler option is set. Unfortunately, this option might not be set by default. Thankfully, you can enable it with CMake by adding the -DFORCE_AVX=ON flag (e.g., type cmake -DFORCE_AVX=ON -DCMAKE_GENERATOR_PLATFORM=x64 .. instead of cmake -DCMAKE_GENERATOR_PLATFORM=x64 ..). If you are building directly in the IDE (with at least Visual Studio 2017 and the Visual C++ tools for CMake component), then right click on CMakeLists.txt and select "Change CMake Settings". This opens a JSON file called CMakeSettings.json. This file allows you to add CMake flags by editing the "cmakeCommandArgs" keys. E.g., you can modify the lines that read "cmakeCommandArgs" : "" so that they become "cmakeCommandArgs" : "-DFORCE_AVX=ON". The relevant part of the JSON file might look at follows:

  {
    "name": "x64-Debug",
    "generator": "Visual Studio 15 2017 Win64",
    "configurationType": "Debug",
    "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
    "cmakeCommandArgs": "-DFORCE_AVX=ON",
    "buildCommandArgs": "-m -v:minimal"
  },
  {
    "name": "x64-Release",
    "generator": "Visual Studio 15 2017 Win64",
    "configurationType" : "Release",
    "buildRoot":  "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
    "cmakeCommandArgs":  "-DFORCE_AVX=ON",
    "buildCommandArgs": "-m -v:minimal"
   }

After this modification, the output of CMake should include a line such as this one:

   CMAKE_C_FLAGS:   /arch:AVX2  -Wall

You must understand that this implies that the produced binaries will not run on hardware that does not support AVX2. However, you might get better performance.

We have additionnal optimizations that use inline assembly. However, Visual Studio does not support inline assembly so you cannot benefit from these optimizations under Visual Studio.

Usage (Using conan)

You can install the library using the conan package manager:

$ echo -e "[requires]\nroaring/0.2.66" > conanfile.txt
$ conan install .

Usage (Using vcpkg on Windows, Linux and macOS)

vcpkg users on Windows, Linux and macOS can download and install roaring with one single command from their favorite shell.

On Linux and macOS:

$ ./vcpkg install roaring

will build and install roaring as a static library.

On Windows (64-bit):

.\vcpkg.exe install roaring:x64-windows

will build and install roaring as a shared library.

.\vcpkg.exe install roaring:x64-windows-static  

will build and install roaring as a static library.

These commands will also print out instructions on how to use the library from MSBuild or CMake-based projects.

If you find the version of roaring shipped with vcpkg is out-of-date, feel free to report it to vcpkg community either by submiting an issue or by creating a PR.

AVX2-related throttling

Our AVX2 code does not use floating-point numbers or multiplications, so it is not subject to turbo frequency throttling on many-core Intel processors.

Thread safety

Like, for example, STL containers or Java's default data structures, the CRoaring library has no built-in thread support. Thus whenever you modify a bitmap in one thread, it is unsafe to query it in others. It is safe however to query bitmaps (without modifying them) from several distinct threads, as long as you do not use the copy-on-write attribute. For example, you can safely copy a bitmap and use both copies in concurrently. One should probably avoid the use of the copy-on-write attribute in a threaded environment.

How to best aggregate bitmaps?

Suppose you want to compute the union (OR) of many bitmaps. How do you proceed? There are many different strategies.

You can use roaring_bitmap_or_many(bitmapcount, bitmaps) or roaring_bitmap_or_many_heap(bitmapcount, bitmaps) or you may even roll your own aggregation:

roaring_bitmap_t *answer  = roaring_bitmap_copy(bitmaps[0]);
for (size_t i = 1; i < bitmapcount; i++) {
  roaring_bitmap_or_inplace(answer, bitmaps[i]);
}

All of them will work but they have different performance characteristics. The roaring_bitmap_or_many_heap should probably only be used if, after benchmarking, you find that it is faster by a good margin: it uses more memory.

The roaring_bitmap_or_many is meant as a good default. It works by trying to delay work as much as possible. However, because it delays computations, it also does not optimize the format as the computation runs. It might thus fail to see some useful pattern in the data such as long consecutive values.

The approach based on repeated calls to roaring_bitmap_or_inplace is also fine, and might even be faster in some cases. You can expect it to be faster if, after a few calls, you get long sequences of consecutive values in the answer. That is, if the final answer is all integers in the range [0,1000000), and this is apparent quickly, then the later roaring_bitmap_or_inplace will be very fast.

You should benchmark these alternatives on your own data to decide what is best.

Python Wrapper

Tom Cornebize wrote a Python wrapper available at https://github.com/Ezibenroc/PyRoaringBitMap Installing it is as easy as typing...

pip install pyroaring

JavaScript Wrapper

Salvatore Previti wrote a Node/JavaScript wrapper available at https://github.com/SalvatorePreviti/roaring-node Installing it is as easy as typing...

npm install roaring

Swift Wrapper

Jérémie Piotte wrote a Swift wrapper.

C# Wrapper

Brandon Smith wrote a C# wrapper available at https://github.com/RogueException/CRoaring.Net (works for Windows and Linux under x64 processors)

Go (golang) Wrapper

There is a Go (golang) wrapper available at https://github.com/RoaringBitmap/gocroaring

Rust Wrapper

Saulius Grigaliunas wrote a Rust wrapper available at https://github.com/saulius/croaring-rs

D Wrapper

Yuce Tekol wrote a D wrapper available at https://github.com/yuce/droaring

Redis Module

Antonio Guilherme Ferreira Viggiano wrote a Redis Module available at https://github.com/aviggiano/redis-roaring

Mailing list/discussion group

https://groups.google.com/forum/#!forum/roaring-bitmaps

References about Roaring

  • Daniel Lemire, Owen Kaser, Nathan Kurz, Luca Deri, Chris O'Hara, François Saint-Jacques, Gregory Ssi-Yan-Kai, Roaring Bitmaps: Implementation of an Optimized Software Library, Software: Practice and Experience (to appear) arXiv:1709.07821
  • Samy Chambi, Daniel Lemire, Owen Kaser, Robert Godin, Better bitmap performance with Roaring bitmaps, Software: Practice and Experience Volume 46, Issue 5, pages 709–719, May 2016 http://arxiv.org/abs/1402.6407 This paper used data from http://lemire.me/data/realroaring2014.html
  • Daniel Lemire, Gregory Ssi-Yan-Kai, Owen Kaser, Consistently faster and smaller compressed bitmaps with Roaring, Software: Practice and Experience (accepted in 2016, to appear) http://arxiv.org/abs/1603.06549
  • Samy Chambi, Daniel Lemire, Robert Godin, Kamel Boukhalfa, Charles Allen, Fangjin Yang, Optimizing Druid with Roaring bitmaps, IDEAS 2016, 2016. http://r-libre.teluq.ca/950/
Issues
  • Custom memory management

    Custom memory management

    The first change is the addition of a roaring_options_t struct that can be passed in as a pointer to new variants of most of the existing externals.

    Some examples...

    roaring_bitmap_t *roaring_bitmap_create(void);
    roaring_bitmap_t *roaring_bitmap_create_with_opts(roaring_options_t *options);
    
    roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1,
                                         const roaring_bitmap_t *x2);
    roaring_bitmap_t *roaring_bitmap_and_with_opts(const roaring_bitmap_t *x1,
                                                   const roaring_bitmap_t *x2,
                                                   roaring_options_t *options);
    

    The roaring_options_t struct is intended to be an extendable struct that is accessible and common at all levels of the bitmap. When provided, memory is allocated and held by the roaring_bitmap_t instance, which owns the lifetime of this struct. A pointer to this memory is passed down to the roaring_array_t, and all the containers therein. This option struct is entirely optional and can be set to NULL.

    The design of this change was intended to have the least external impact possible, meaning that for the most part, tests of the externals have remained unchanged. Changes were required on some of the lower-level test cases, for example, the ones that created and manipulated containers directly.

    The roaring_options_t struct is defined in include/roaring/options.h as...

    struct roaring_options_s {
        roaring_memory_t* memory;
    };
    

    Within roaring_options_t there is another new struct, roaring_memory_t. This contains function pointers to custom memory management functions and a void pointer that can be used by the caller to pass down additional inputs to these functions.

    The roaring_memory_t struct is defined in include/roaring/memory.h as...

    struct roaring_memory_s {
        p_roaring_malloc malloc;
        p_roaring_realloc realloc;
        p_roaring_calloc calloc;
        p_roaring_free free;
        p_roaring_aligned_malloc aligned_malloc;
        p_roaring_aligned_free aligned_free;
        void* payload;
    };
    

    Finally, each call to the built-in memory management function that existed previously in the code has been replaced with shims that consume an option struct pointer. For example...

    void* roaring_malloc(roaring_options_t* options, size_t n) {
        if (options != NULL && options->memory != NULL &&
            options->memory->malloc != NULL) {
            return options->memory->malloc(n, options->memory->payload);
        }
        return malloc(n);
    }
    

    If the options, memory, and function pointer are all non-null, it will use the user-provided memory function. Otherwise, it will use the built-ins as before. Examples of how this is employed can be found in tests/memory_unit.cpp.

    This code is far from perfect, but it satisfies the requirements for my team's project. It has survived the reversion of mass reformatting and a fairly significant merge up to master as we were using an older release tag for development. I hope that this will provide a solid starting point for this new feature, and am looking forward to working with this community in its development.

    opened by olanmatt-ibm 59
  • Compilation on windows?

    Compilation on windows?

    Are there any plans to support windows 64bit, preferably with the visual studio compiler or else mingw/cygwin?

    I got it to compile and run on VS2015 (unit test still fail) but this already required quite some changes to the code.

    help wanted 
    opened by mrboojum 56
  • Implement a dispatched model for compilation of the CPU capabilities

    Implement a dispatched model for compilation of the CPU capabilities

    Since we have a range of optimizations currently available and likely to add more/improve over time, the complexity of building with the appropriate configuration can be tricky.

    Ideally, we would have a dispatching library that is able to select our optimal implementation at run time based on the CPU feature set. Most libraries I’ve seen that offer dispatched models use environment variables to disable or adjust things on the fly.

    For GCC we could use https://gcc.gnu.org/wiki/FunctionMultiVersioning, MSVC is a bit more complicated and it may be inevitable that there will be some function pointers to glue things together, I’ve started with some intrinsic routines to get going also and will be evaluating some options in keeping with pure C (no templates). It'd be nice to have a simple pattern that worked for all compiler/build targets.

    help wanted 
    opened by K2 36
  • Embedded C object inside C++ object

    Embedded C object inside C++ object

    changed the pointer to a CRoaring object inside of the C++ class into the object itself, avoiding a malloc and free in the constructor/destructor, this makes little difference to the 32-bit C++ class but speeds up adds to the 64-bit class by a few percent, also did a few minor things

    Bug fix: C++ iterators now stop at max uint32_t instead of comparing equal to the end iterator at this point Performance: removed virtual keyword for C++ iterator class's destructor since it will cause most compilers to generate a vtable for each object, bloating it up (the lack of the keyword is safe in this case unless a user subclasses the iterator and that subclass is deleted through a pointer with the type of the base class) Feature: added a toString member function to the C++ classes which makes STL strings

    opened by ksajme 19
  • roaring_bitmap_run_optimize() issue

    roaring_bitmap_run_optimize() issue

    Hi Lemire,

    I confuse on function roaring_bitmap_run_optimize(). Could you please help to explain me?? I made a simple test (please see below code), then run in 2 contexts:

    1. Build and run below code normally.
    2. Comment function // roaring_bitmap_run_optimize(reg). Then build and run again

    While program is looping, i measure memory usage by command "pmap (pid_of_program)" I found that size of "reg" reduces much however total memory gotten from pmap is not changed.

    I think memory gotten from pmap should reduce after run optimize, is it correct?

    int main(int argc, char** argv) 
    {
        int i, k, n;
        roaring_bitmap_t *reg;
        uint32_t count;
    
        reg = roaring_bitmap_create();
        for (i=0; i<320000000; i++) {
            roaring_bitmap_add(reg, i);
        }
        count = roaring_bitmap_portable_size_in_bytes(reg);
        printf("size before optimize: %d\n", count);
        roaring_bitmap_run_optimize(reg);  // comment this function to run second test
        count = roaring_bitmap_portable_size_in_bytes(reg);
        printf("size after optimize: %d\n", count);
        while (1);
    }
    
    opened by ancuop 19
  • Implemented copy-on-write (CoW) at the container level.

    Implemented copy-on-write (CoW) at the container level.

    This seems to pass our unit tests and it seems to generally help our performance without doing any harm.

    Basically, it helps the performance when you would otherwise have to clone containers needlessly. It is not just the copying, but also the memory usage, the memory allocation and so forth. How often this happens depends very much on the data. However, avoiding even just one container copy can maybe be worth it.

    To see how much it can help, here is the number of cycles per input value for successive unions in the census1881 data set.

    Before (no CoW): 1.24 After (with CoW): 0.49

    To my surprise, there is at least one instance where we can double the performance with CoW.

    It does, however, slightly increase the code complexity. Maybe surprisingly, however, it is not so difficult to implement. It is possible however that I introduced memory leaks and so forth. Thread safety might be diminished as well, maybe. I don't know.

    Before merging this with master, I'd like for at least on fresh pair of eyes to review it. Indeed, if we adopt this, then it will have long-term consequences regarding maintenance and future work.

    It is likely, also, that I have let bugs slip.

    c.c. @owenkaser @fsaintjacques

    help wanted new feature performance 
    opened by lemire 18
  • Faster contains operation in array containers

    Faster contains operation in array containers

    Here is the change you can check out: https://github.com/WojciechMula/CRoaring/commit/6541bc70757351e7cc61e78df339a89eabf071d8

    As the commit messages states, it's a mix of binary search and linear search. Whenever range in binary search becomes smaller than some (small) threshold, we use linear search on this range. My experiments show that for relative small tables linear search wins. You can take a look at my old article http://0x80.pl/articles/simd-search.html#binary-search-with-fallback-to-linear-search-new.

    Below are benchmarks from Skylake and my old Westmere CPU. On Skylake we get nearly 1.5 speedup, on Westmere -- 1.2.

    Original
    
    Skylake:  contains_test(B):  147.80 cycles per operation
    Westmere: contains_test(B):  128.83 cycles per operation
    
    Modified (threshold = 8 items)
    
    Skylake:  contains_test(B):  101.91 cycles per operation
    Westmere: contains_test(B):  105.50 cycles per operation
    
    Modified (threshold = 16 items)
    
    Skylake:  contains_test(B):   97.91 cycles per operation
    Westmere: contains_test(B):  104.34 cycles per operation
    
    opened by WojciechMula 17
  • support customized memory hook

    support customized memory hook

    Hi guys, Found the discussion in #284 and this is a lightweight implementation of customized allocator. We try to use it in our project and it works. For general users, default allocator would be used. For other users, allocator could be defined.

    opened by DarrenJiang13 16
  • Implement shift operations

    Implement shift operations

    For #186 and #144. It's still a work in progress. It's close to finished but I still need to sort out some bugs in the bitset implementation and clean up the tests, which are currently a bit of a mess and don't really convey enough information on failure.

    UPDATE: ready for review.

    opened by Oppen 16
  • CMake build system

    CMake build system

    This replaces the current makefile with a CMake build system.

    There's a new make install target that installs headers and a shared library, which means that you can include the library using LDFLAGS=-lroaring and #include <roaring/roaring.h>.

    All you need to do now is to run cmake [OPTIONS] . once to build the Makefile, followed by the usual make [target]. More CMake info here: http://llvm.org/releases/3.8.0/docs/CMake.html

    For example, to install the library without AVX tuning, and to show the build commands:

    $ cmake -DAVX_TUNING=0 .
    $ make VERBOSE=1
    $ make install
    

    I've preserved everything as best I could. The tests and benchmarks are compiled into the top-level directory and can be invoked in exactly the same way, and the run_unit.sh script still works.

    opened by chriso 16
  • Implement ordered_writer and roaring_bitmap_add_ordered for faster construction

    Implement ordered_writer and roaring_bitmap_add_ordered for faster construction

    This tries to port OrderedWriter from Java implementation (the behaviour of ordered_writer.c is quite the same). This implements a new function on roaring.h: roaring_bitmap_add_ordered which allocates and uses an ordered_writer.

    There is still work to do which I need help (this is my first C "serious" code, so there might be other mistakes).

    opened by telles-simbiose 15
  • Add roaring_bitmap_add_bulk

    Add roaring_bitmap_add_bulk

    roaring_bitmap_add_bulk is a generalization of roaring_bitmap_add_many, caching the container for the last inserted item, and avoiding looking the container up if another item is inserted in the same container.

    Use the new function in the implementation of roaring_bitmap_add_many and roaring_bitmap_of

    opened by Dr-Emann 4
  • Should we standardize the output of our benchmarks?

    Should we standardize the output of our benchmarks?

    I've been toying with the idea of making CRoaring benchmarks print their results in a format compatible with the benchstat tool we use in the Go world for comparing different runs. I think it would help us make more informed decisions when optimizing.

    Related to this, after we tackle #284 we should be able to use an allocator that keeps track of how many allocations we make and how much memory we use for a given operation, which could further aid comparison.

    enhancement question 
    opened by Oppen 1
  • Should we implement standard `free` semantics for deallocation functions?

    Should we implement standard `free` semantics for deallocation functions?

    The standard library specifies free to act as a nop when its argument is a NULL pointer. This gives us programmers a big convenience for error handling and similar situations where we don't need to add so many explicit branches, but rather count on free doing the right thing for us. Point being, my proposal is not exactly something new.
    Tackling #182 in particular would create (or, rather, acknowledge) many new error paths that will require these free analogs to be called, and the current implementations also mean explicitly checking for NULL each time on each call place. This can become tedious, error prone and a cognitive load rapidly.
    It can also help brevity on existing code.

    Counterarguments would probably go around the extra branching: sometimes we simply know for certain our pointer won't be NULL. IMO this case shouldn't matter that much for performance for the following reasons:

    1. Where operation count matters the most is in tight loops, and it's folklore already that you shouldn't allocate too much if you'll be discarding the buffer there. If you allocate it should be because you do need to keep the value afterwards, so how fast the free function is is not the central concern.
    2. Particularly error paths, and specifically those related to memory allocation, should be considered exceptional, and thus have low priority when it comes to performance. You'd rather optimize for the more common case, specially if your trade off is simply an extra branch.

    Thoughts?

    enhancement question 
    opened by Oppen 1
  • New function proposal:

    New function proposal: "roaring_move_uint32_iterator_equalorsmaller"

    Basically like roaring_move_uint32_iterator_equalorlarger, except that its purpose is to move through elements in descending order instead of ascending order.

    For use in conjunction with roaring_init_iterator_last.

    Seem like an oversight that the proposed function is missing. For now my foreign function wrappers call roaring_init_iterator_last and then roaring_previous_uint32_iterator until the desired values are found, but this is not efficient.

    opened by dtenny 2
  • Foreign function friendly iterator request/note

    Foreign function friendly iterator request/note

    I've been working on a set of foreign function APIs for Common Lisp using the CRoaring API.

    Just a note that the iterator interface is a bit problematic and would benefit from a couple of additional functions that encapsulate access to the has_value and current_value members of roaring_uint32_iterator_t.

    Without a function to access those members, I'm forced to write lisp structures which must model an accurate structure of the C header file declarations so that the offsets of the member variables can be correctly determined. Fortunately lisp let's me do this, but unless I add some kind of header file groveling logic on every use of the lisp API, any change to the C structure could result in improper accesses to the desired member variables.

    Note that inline functions (which might be an 'accessor' temptation), mean that there is no symbol in the shared library that can be called, so would not be useful for languages calling CRoaring as a foreign API, so if accessor functions are ever provided they should not be declared 'inline'.

    opened by dtenny 1
Releases(v0.5.0)
Owner
Roaring bitmaps: A better compressed bitset
Roaring bitmaps are compressed bitmaps. They can be hundreds of times faster. (Picture credit: tambako)
Roaring bitmaps: A better compressed bitset
Compile and execute C "scripts" in one go!

c "There isn't much that's special about C. That's one of the reasons why it's fast." I love C for its raw speed (although it does have its drawbacks)

Ryan Jacobs 1.9k Jun 17, 2022
distributed builds for C, C++ and Objective C

distcc -- a free distributed C/C++ compiler system by Martin Pool Current Documents: https://distcc.github.io/ Formally http://distcc.org/ "pump" func

distcc 1.7k Jun 20, 2022
New generation entropy codecs : Finite State Entropy and Huff0

New Generation Entropy coders This library proposes two high speed entropy coders : Huff0, a Huffman codec designed for modern CPU, featuring OoO (Out

Yann Collet 1.1k Jun 27, 2022
Compression abstraction library and utilities

Squash - Compresion Abstraction Library

null 362 Jun 6, 2022
Multi-format archive and compression library

Welcome to libarchive! The libarchive project develops a portable, efficient C library that can read and write streaming archives in a variety of form

null 1.8k Jun 25, 2022
Easing the task of comparing code generated by cc65, vbcc, and 6502-gcc

6502 C compilers benchmark Easing the way to compare code generated by cc65, 6502-gcc, vbcc, and KickC. This repository contains scripts to: Compile t

Sylvain Gadrat 16 Dec 15, 2021
Secure ECC-based DID intersection in Go, Java and C.

SecureUnionID Secure ECC-based DID intersection. ABSTRACT This project is used to protect device ID using Elliptic Curve Cryptography algorithm. The d

Volcengine 19 Mar 25, 2022
nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines.

nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines. Tiny? The only types are: int (32-bit signed integer) char (8-

Ajay Tatachar 16 Feb 13, 2022
Smaller C is a simple and small single-pass C compiler

Smaller C is a simple and small single-pass C compiler, currently supporting most of the C language common between C89/ANSI C and C99 (minus some C89 and plus some C99 features).

Alexey Frunze 1.1k Jun 27, 2022
Microvm is a virtual machine and compiler

The aim of this project is to create a stack based language and virtual machine for microcontrollers. A mix of approaches is used. Separate memory is used for program and variable space (Harvard architecture). An interpreter, virtual machine and compiler are available. A demostration of the interpreter in action is presented below.

null 11 Jun 21, 2022
Pre-configured LLVM and ANTLR4 for C++

LLVM + ANTLR4 Starter Project Starter project for ANTLR4 and LLVM C++ project. Prerequisite LLVM 12 Java (for ANTLR4) git Install prerequisite librari

Nathanael Demacon 10 Apr 1, 2022
Aheui JIT compiler for PC and web

아희짓 개요 아희짓은 아희 언어를 위한 JIT (Just in Time) 컴파일러입니다. 어셈블러와 유틸 라이브러리외에 외부 라이브러리에 전혀 의존하지 않고 JIT을 바닥부터 구현합니다. 지원 환경 64비트 windows, mac, linux (x86 아키텍쳐) 웹어셈

Sunho Kim 27 Jan 2, 2022
Interpreter and miner for the LODA language written in C++

LODA Interpreter and Miner (C++) LODA is an assembly language, a computational model and a tool for mining integer sequences. You can use it to search

LODA Language 11 Jun 12, 2022
is a c++20 compile and runtime Struct Reflections header only library.

is a c++20 compile and runtime Struct Reflections header only library. It allows you to iterate over aggregate type's member variables.

RedSkittleFox 4 Apr 18, 2022
C++ python bytecode disassembler and decompiler

C++ python bytecode disassembler and decompiler

Michael Hansen 1.3k Jun 23, 2022
🌳 A compressed rank/select dictionary exploiting approximate linearity and repetitiveness.

The block-ε tree is a compressed rank/select dictionary that achieves new space-time trade-offs by exploiting the approximate linearity and the repeti

Giorgio Vinciguerra 10 Jun 5, 2022
A LLVM and Clang compiler toolchain built for kernel development

Cosmic-Clang Toolchain This is a LLVM and Clang compiler toolchain built for kernel development. Builds are always made from the latest LLVM sources r

Ǥђ๏ຮ₮⌁Ⲙครtє࿐ 0 Apr 12, 2022
UE4 SDF Importers / Generators for SVG and Bitmaps

RTMSDF UE4 SDF Importers / Generators for SVG and Bitmaps NOTE: This module is very much work in progress / preview and likely to change Uses MSDFGen

Richard Meredith 8 Jun 16, 2022
RdpCacheStitcher is a tool that supports forensic analysts in reconstructing useful images out of RDP cache bitmaps.

RdpCacheStitcher is a tool that supports forensic analysts in reconstructing useful images out of RDP cache bitmaps. Using raw RDP cache tile bitmaps extracted by tools like e.g. ANSSI's BMC-Tools as input, it provides a graphical user interface and several placement heuristics for stitching tiles together so that meaningful images or even full screenshots can be reconstructed.

Bundesamt für Sicherheit in der Informationstechnik 162 Jun 17, 2022
null 235 Jun 22, 2022
Triton Python and C++ client libraries and example, and client examples for go, java and scala.

Triton Client Libraries and Examples To simplify communication with Triton, the Triton project provides several client libraries and examples of how t

Triton Inference Server 141 Jun 24, 2022
Encapsulates the two protocols of OpenVpn and Ikev2, you only need to enter the server IP and port number to realize the connection and status display, and the specific situation of the connection can be displayed at the same time。

NewVpnCore 封装了OpenVpn和Ikev2两种协议,只需要输入服务器IP和端口号即可实现连接和状态显示,同时可以显示连接的具体情况。 UniteVpn Core(第一版) 1. 模块说明 unitevpn:封装了vpn的操作和统一不同协议信息的模块 ikev2:IKEV2协议的源码 op

ZFashion 3 Jun 8, 2022
PikaScript is an ultra-lightweight Python engine with zero dependencies and zero-configuration, that can run with 4KB of RAM (such as STM32G030C8 and STM32F103C8), and is very easy to deploy and expand.

PikaScript 中文页| Star please~ 1. Abstract PikaScript is an ultra-lightweight Python engine with zero dependencies and zero-configuration, that can run

Lyon 786 Jun 27, 2022
Signed - a 3D modeling and construction language based on Lua and SDFs. Signed will be available for macOS and iOS and is heavily optimized for Metal.

Signed - A 3D modeling language Abstract Signed is a Lua based 3D modeling language, it provides a unique way to create high quality 3D content for yo

Markus Moenig 88 May 26, 2022
Like libevent and libuv, libhv provides event-loop with non-blocking IO and timer, but simpler api and richer protocols.

中文版 Intro Like libevent, libev, and libuv, libhv provides event-loop with non-blocking IO and timer, but simpler api and richer protocols. Features cr

ithewei 4.2k Jun 19, 2022