Skip to content

Commit 8711167

Browse files
authored
better documentation for 64-bit frozen. (#758)
1 parent af94900 commit 8711167

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(RoaringBitmap
33
DESCRIPTION "Roaring bitmaps in C (and C++)"
44
LANGUAGES CXX C
55
)
6+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
67
set (CMAKE_C_STANDARD 11)
78
set (CMAKE_CXX_STANDARD 11)
89

cpp/roaring/roaring64map.hh

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,9 +1250,11 @@ class Roaring64Map {
12501250
}
12511251

12521252
/**
1253-
* For advanced users only.
1253+
* For advanced users only. This function is unsafe. You must ensure that
1254+
* the provided buffer is 32-byte aligned.
12541255
*/
12551256
static const Roaring64Map frozenView(const char *buf) {
1257+
// We do not check that buf is 32-byte aligned. Caller is responsible.
12561258
// size of bitmap buffer and key
12571259
const size_t metadata_size = sizeof(size_t) + sizeof(uint32_t);
12581260

@@ -1311,16 +1313,24 @@ class Roaring64Map {
13111313
return result;
13121314
}
13131315

1316+
/**
1317+
* For advanced users only. Offered on a best-effort basis.
1318+
* If you use this function in production, you are responsible for
1319+
* testing it on your target platforms. This function is unsafe.
1320+
*/
13141321
// As with serialized 64-bit bitmaps, 64-bit frozen bitmaps are serialized
13151322
// by concatenating one or more Roaring::write output buffers with the
1316-
// preceeding map key. Unlike standard bitmap serialization, frozen bitmaps
1317-
// must be 32-byte aligned and requires a buffer length to parse. As a
1318-
// result, each concatenated output of Roaring::writeFrozen is preceeded by
1319-
// padding, the buffer size (size_t), and the map key (uint32_t). The
1320-
// padding is used to ensure 32-byte alignment, but since it is followed by
1321-
// the buffer size and map key, it actually pads to `(x - sizeof(size_t) +
1322-
// sizeof(uint32_t)) mod 32` to leave room for the metadata.
1323+
// preceeding map key. Like the 32-bit bitmaps, it expects that the provided
1324+
// buffer is 32-byte aligned. The caller is responsible to check the
1325+
// alignment. Unlike standard bitmap serialization, frozen bitmaps must be
1326+
// 32-byte aligned and requires a buffer length to parse. As a result, each
1327+
// concatenated output of Roaring::writeFrozen is preceeded by padding, the
1328+
// buffer size (size_t), and the map key (uint32_t). The padding is used to
1329+
// ensure 32-byte alignment, but since it is followed by the buffer size and
1330+
// map key, it actually pads to `(x - sizeof(size_t) + sizeof(uint32_t)) mod
1331+
// 32` to leave room for the metadata.
13231332
void writeFrozen(char *buf) const {
1333+
// We do not check that buf is 32-byte aligned. Caller is responsible.
13241334
// size of bitmap buffer and key
13251335
const size_t metadata_size = sizeof(size_t) + sizeof(uint32_t);
13261336

@@ -1349,6 +1359,9 @@ class Roaring64Map {
13491359
}
13501360
}
13511361

1362+
/**
1363+
* For advanced users only. This function is unsafe.
1364+
*/
13521365
size_t getFrozenSizeInBytes() const {
13531366
// size of bitmap size and map key
13541367
const size_t metadata_size = sizeof(size_t) + sizeof(uint32_t);

microbenchmarks/synthetic_bench.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ static void cppFrozenSerialize(benchmark::State& state) {
417417
r.add(val);
418418
}
419419
size_t size = r.getFrozenSizeInBytes();
420-
// TODO: there seems to be a bug in writeFrozen that causes writes beyond
421-
// getFrozenSizeInBytes()
422-
std::vector<char> buf(size * 2);
420+
// writeFrozen requires a 32-byte aligned buffer. It is inconvenient to
421+
// enforce that here, so we just overallocate.
422+
std::vector<char> buf(size * 2 + 32);
423423
for (auto _ : state) {
424424
r.writeFrozen(buf.data());
425425
benchmark::DoNotOptimize(buf);
@@ -504,9 +504,9 @@ static void cppFrozenDeserialize(benchmark::State& state) {
504504
r1.add(val);
505505
}
506506
size_t size = r1.getFrozenSizeInBytes();
507-
// TODO: there seems to be a bug in writeFrozen that causes writes beyond
508-
// getFrozenSizeInBytes()
509-
std::vector<char> buf(size * 2);
507+
// writeFrozen requires a 32-byte aligned buffer. It is inconvenient to
508+
// enforce that here, so we just overallocate.
509+
std::vector<char> buf(size * 2 + 32);
510510
r1.writeFrozen(buf.data());
511511
for (auto _ : state) {
512512
auto r2 = Roaring64Map::frozenView(buf.data());

0 commit comments

Comments
 (0)