百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

binfuse

> 编程语言
开源

用于二进制融合过滤器的 c++ 库,包括分片过滤器

99 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于二进制融合过滤器的 c++ 库,包括分片过滤器

binfuse C++ Library for Binary Fuse Filters

Binary fuse filters are a recent (2022) development in the group of Approximate Membership Query filters

Approximate membership query filters (hereafter, AMQ filters) comprise a group of space-efficient probabilistic data structures that support approximate membership queries. An approximate membership query answers whether an element is in a set or not with a false positive rate of ϵ.

Binary fuse filters are a further development of XOR filters. Both are more space efficient, and faster to build/query than traditional options like Bloom and Cuckoo filters.

This binfuse C++ library builds on the C-libary by the authors of the relevant research paper.

As well as adding a convenient C++ interface, binfuse::filter also facilitates (de-)serializing the populated filter to/from disk as well as querying it directly from disk via an mmap, with cross platform support from mio. Both in memory and "off disk" operation is supported.

One of the challenges with binary fuse filters, is that they are immutable once populated, so data cannot be added incrementally, and they consume a significant amount of memory during the populate process - 64GB of memory is recommended for populating with 500 million uint64_t keys/hashes. This has, until now, placed an upward bound on the practical application of these filters to very large datasets.

binfuse::sharded_filter allows convenient slicing of the dataset into an arbitrary number of shards which are written to disk and indexed by the N most significant bits of the uint64_t keys/hashes. Sharding is transparent to the user during queries is and still very fast with just 3 mmap accesses per query.

binfuse::sharded_filter easily controls RAM requirements during the "populate filter" process and enables datasets of 10s of billions of records with common hardware. Query speeds depend on disk hardware and cache conditions, but can be in the 50ns range.

Applications

The movitating use case was this pwned pasword db project, but there are many common applications:

Typical applications of AMQ filters are distributed systems and database systems. The AMQ filter functions as a proxy to the set of keys of a database or remote memory. Before a presumably slow query to the database or to remote memory is performed, the AMQ filter is used to give an approximate answer as to whether the key is in the database or in remote memory. The slow query is only performed when the AMQ filter returns true. Only in the case of a (hopefully rare) false positive is an unnecessary I/O or remote access performed. The applications are numerous and include package and resource routing, P2P networks, and distributed caching.

Usage examples from tests

Singular binuse::filter for in memory use:

cpp
binfuse::filter8 filter(std::vector{
    0x0000000000000000,
    0x0000000000000001, // order is not important
    0x0000000000000002,
});
EXPECT_TRUE(filter.is_populated());

EXPECT_TRUE(filter.contains(0x0000000000000000));
EXPECT_TRUE(filter.contains(0x0000000000000001));
EXPECT_TRUE(filter.contains(0x0000000000000002));

Singular binuse::filter save and load:

cpp
binfuse::filter8_sink filter_sink(std::vector{
    0x0000000000000000,
    0x0000000000000001, // order is not important
    0x0000000000000002,
});
filter_sink.save("tmp/filter8.bin");

binfuse::filter8_source filter_source;
filter_source.load("tmp/filter8.bin");

EXPECT_TRUE(filter_source.contains(0x0000000000000000));
EXPECT_TRUE(filter_source.contains(0x0000000000000001));
EXPECT_TRUE(filter_source.contains(0x0000000000000002));

Sharded filter, bulding one shard at the time:

…

or via "streaming" API:

…

The main classes are templated as follows to select underlying 8 or 16 bit filters, giving a 1/256 and 1/65536 chance of a false positive respectively. The persistent versions are also templated by the mio::mmap::access_mode to select read or write (AKA source or sink):

cpp
namespace binfuse {

template 
concept filter_type = std::same_as || std::same_as;

template 
class filter;

template 
class persistent_filter;

template 
class sharded_filter;

} // namespace binfuse

the following convenience aliases are provided:

cpp
namespace binfuse {

// binfuse::filter

using filter8  = filter;
using filter16 = filter;

using filter8_sink   = persistent_filter;
using filter8_source = persistent_filter;

using filter16_sink   = persistent_filter;
using filter16_source = persistent_filter;

// binfuse::sharded_filter

using sharded_filter8_sink = sharded_filter;
using sharded_filter8_source = sharded_filter;

using sharded_filter16_sink = sharded_filter;
using sharded_filter16_source = sharded_filter;

} // namespace binfuse

Requirements and building

POSIX systems and Windows(mingw) are supported, with a recent C++20 compiler (eg gcc 13.2, clang 18.1), but it will also build with gcc-10.2 from Debian 11.

bash
git clone https://github.com/oschonrock/binfuse.git
git submodule update --init --recursive

cmake -S . -B  build -DCMAKE_BUILD_TYPE=release 
cmake --build build

Tests are run automatically at the end of build. They can be disabled with -DBINFUSE_TEST=OFF.

Including in your project

The library, and its included upstream C-library are header only, but there is a cmake interface target available:

cmake
add_subdirectory(ext/binfuse)

add_executable(my_exe main.cpp)
target_link_libraries(my_exe PRIVATE binfuse)
cpp
// main.cpp

#include "binfuse/sharded_filter.hpp"
#include "binfuse/filter.hpp"

...

A note on file formats and tags

Two different binary formats are used for filter and sharded_filter (details). They each have different build parameters, which further affect the structure. These are fingerprint size (8 or 16bit) and, in the case of the sharded filter, the number of shards.

The file format parameters are recorded in the first 16 bytes of each file, so that files are not accidentally opened with the wrong parameters, giving bogus results. These tags are human readable and can be inspected with hexdump.

bash
$ hd filter8.bin | head -n1
00000000  73 62 69 6e 66 75 73 65  30 38 2d 30 30 36 34 00  |sbinfuse08-0064.|

$ hd filter16.bin | head -n1
00000000  73 62 69 6e 66 75 73 65  31 36 2d 30 30 36 34 00  |sbinfuse16-0064.|

binfuse will throw exceptions, if files are opened with the wrong type/params.

Benchmarks

There is a benchmark for the sharded_filter, which includes figures for the filter internally. This can be built with -DBINFUSE_BENCH=ON. Some results below for 100m keys.

It is difficult to run the binfuse_bench_large for 1billion or more keys, due to memory pressure, unless the first runs, with low shard count, are pruned out. This illustrates the precise motivation for the sharded_filter.

Edit for your needs.

…

A note on memory consumption

The first few runs of the above benchmark, with low shard count, will consume large, transient amounts of memory (1-2GB range) during filter::populate. The latter runs show the benefit of the sharded_filter, and the max memory consumption will settle at the size of the filter file (108MB/215MB for 8/16bit).

Note that even this latter memory consumption is almost all in the mmap. The OS is likely deciding to just let your process cache the mmap fully in memory. This obviously benefits performance, but it is flexible disk cache. If the machine comes under memory pressure, these pages will be evicted.

A huge dataset of 10 billion entries builds and queries just fine. The 8bit filter is 11GB and the 16bit filter is 22GB on disk. The stats below show how the 22GB, 16bit filter did not fit in the physical memory of the 16GB machine so the query time slowed to 0.7ms, but otherwise the system is quite stable.

Shard Size: 9765625  Shards: 1024  Keys: 10000000000

           gen   populate     verify        add      query       f+ve
f8       7.5ns    174.4ns     26.2ns      6.7ns    150.7ns  0.390653%
f16      8.4ns     74.7ns     30.2ns     12.6ns 682612.8ns  0.001000%

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言