4 // A reimplementation of parts of the TurboPFor codecs, using the same
5 // storage format. These are not as fast as the reference implementation
6 // (about 1/3 of the performance), and do not support the same breadth of
7 // codecs (in particular, only delta-plus-1 is implemented, and only 32-bit
8 // docids are tested), but aim to be more portable and easier-to-understand.
9 // In particular, they will compile on x86 without SSE4.1 or AVX support.
11 // The main reference is https://michael.stapelberg.ch/posts/2019-02-05-turbopfor-analysis/,
12 // although some implementation details have been worked out by studying the
24 Docid read_le(const void *in)
27 memcpy(&val, in, sizeof(val));
28 if constexpr (sizeof(Docid) == 8) {
30 } else if constexpr (sizeof(Docid) == 4) {
32 } else if constexpr (sizeof(Docid) == 2) {
34 } else if constexpr (sizeof(Docid) == 1) {
41 // Reads a single value with an encoding that looks a bit like PrefixVarint.
42 // It's unclear why this doesn't use the varbyte encoding.
44 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
46 //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
50 } else if (*in < 192) {
51 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
53 } else if (*in < 224) {
54 *out = ((uint32_t(in[0]) << 16) |
55 (uint32_t(in[2]) << 8) |
56 (uint32_t(in[1]))) & 0x1fffff;
59 assert(false); // Not implemented.
64 const unsigned char *read_vb(const unsigned char *in, Docid *out)
69 } else if (*in <= 240) {
70 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
72 } else if (*in <= 248) {
73 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
75 } else if (*in == 249) {
76 *out = (uint32_t(in[1])) |
77 (uint32_t(in[2]) << 8) |
78 (uint32_t(in[3]) << 16);
80 } else if (*in == 250) {
81 *out = read_le<uint32_t>(in + 1);
90 BitReader(const unsigned char *in, unsigned bits)
91 : in(in), bits(bits), mask((1U << bits) - 1) {}
94 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
104 const unsigned char *in;
107 unsigned bits_used = 0;
110 template<unsigned NumStreams>
111 struct InterleavedBitReader {
113 InterleavedBitReader(const unsigned char *in, unsigned bits)
114 : in(in), bits(bits), mask((1U << bits) - 1) {}
118 if (bits_used + bits > 32) {
119 val = (read_le<uint32_t>(in) >> bits_used) | (read_le<uint32_t>(in + Stride) << (32 - bits_used));
121 val = (read_le<uint32_t>(in) >> bits_used);
125 in += Stride * (bits_used / 32);
132 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
133 const unsigned char *in;
136 unsigned bits_used = 0;
139 // Does not properly account for overflow.
140 inline unsigned div_round_up(unsigned val, unsigned div)
142 return (val + div - 1) / div;
145 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
147 return div_round_up(num * bit_width, CHAR_BIT);
150 // Constant block. Layout:
152 // - Bit width (6 bits) | type << 6
153 // - Base values (<bits> bits, rounded up to nearest byte)
154 template<class Docid>
155 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
157 const unsigned bit_width = *in++ & 0x3f;
158 Docid val = read_le<Docid>(in);
159 if (bit_width < sizeof(Docid) * 8) {
160 val &= ((1U << bit_width) - 1);
163 Docid *prev_out = out - 1;
164 for (unsigned i = 0; i < num; ++i) {
165 out[i] = val + prev_out[i] + 1;
167 return in + div_round_up(bit_width, 8);
170 // FOR block (ie., PFor without exceptions). Layout:
172 // - Bit width (6 bits) | type << 6
173 // - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
174 template<class Docid>
175 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
177 const unsigned bit_width = *in++ & 0x3f;
179 Docid *prev_out = out - 1;
180 BitReader bs(in, bit_width);
181 for (unsigned i = 0; i < num; ++i) {
182 out[i] = bs.read() + prev_out[i] + 1;
184 return in + bytes_for_packed_bits(num, bit_width);
187 // Like decode_for(), but the values are organized in four independent streams,
188 // for SIMD (presumably SSE2). Supports a whole block only.
189 template<unsigned BlockSize, class Docid>
190 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
192 const unsigned bit_width = *in++ & 0x3f;
194 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
195 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
196 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
197 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
198 for (unsigned i = 0; i < BlockSize / 4; ++i) {
199 out[i * 4 + 0] = bs0.read();
200 out[i * 4 + 1] = bs1.read();
201 out[i * 4 + 2] = bs2.read();
202 out[i * 4 + 3] = bs3.read();
204 Docid *prev_out = out - 1;
205 for (unsigned i = 0; i < BlockSize; ++i) {
206 out[i] += prev_out[i] + 1;
208 return in + bytes_for_packed_bits(BlockSize, bit_width);
211 // PFor block with bitmap exceptions. Layout:
213 // - Bit width (6 bits) | type << 6
214 // - Exception bit width (8 bits)
215 // - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
216 // - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
217 // - Base values (<num> values of <bits> bits, rounded up to a byte)
218 template<class Docid>
219 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
221 memset(out, 0, num * sizeof(Docid));
223 const unsigned bit_width = *in++ & 0x3f;
224 const unsigned exception_bit_width = *in++;
226 // Decode exceptions.
228 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
229 in += div_round_up(num, 8);
231 int num_exceptions = 0;
233 BitReader bs(in, exception_bit_width);
234 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
235 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
237 // We've read some bytes past the end, so clear out the junk bits.
238 exceptions &= (1ULL << (num - i)) - 1;
240 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
241 unsigned idx = (ffsll(exceptions) - 1) + i;
242 out[idx] = bs.read() << bit_width;
245 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
248 // Decode the base values, and delta-decode.
249 Docid *prev_out = out - 1;
250 BitReader bs(in, bit_width);
251 for (unsigned i = 0; i < num; ++i) {
252 out[i] = (out[i] | bs.read()) + prev_out[i] + 1;
254 return in + bytes_for_packed_bits(num, bit_width);
257 // Like decode_pfor_bitmap(), but the base values are organized in four
258 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
259 template<unsigned BlockSize, class Docid>
260 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
262 memset(out, 0, BlockSize * sizeof(Docid));
264 const unsigned bit_width = *in++ & 0x3f;
265 const unsigned exception_bit_width = *in++;
267 // Decode exceptions.
269 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
270 in += div_round_up(BlockSize, 8);
272 int num_exceptions = 0;
274 BitReader bs(in, exception_bit_width);
275 for (unsigned i = 0; i < BlockSize; i += 64, ++exception_bitmap_ptr) {
276 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
277 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
278 unsigned idx = (ffsll(exceptions) - 1) + i;
279 out[idx] = bs.read() << bit_width;
282 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
285 // Decode the base values, and delta-decode.
286 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
287 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
288 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
289 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
290 for (unsigned i = 0; i < BlockSize / 4; ++i) {
291 out[i * 4 + 0] |= bs0.read();
292 out[i * 4 + 1] |= bs1.read();
293 out[i * 4 + 2] |= bs2.read();
294 out[i * 4 + 3] |= bs3.read();
296 Docid *prev_out = out - 1;
297 for (unsigned i = 0; i < BlockSize; ++i) {
298 out[i] += prev_out[i] + 1;
300 return in + bytes_for_packed_bits(BlockSize, bit_width);
303 // PFor block with variable-byte exceptions. Layout:
305 // - Bit width (6 bits) | type << 6
306 // - Number of exceptions (8 bits)
307 // - Base values (<num> values of <bits> bits, rounded up to a byte)
309 // - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
310 // - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
311 // - Indexes of exceptions (<num_exc> bytes).
312 template<unsigned BlockSize, class Docid>
313 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
315 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
317 const unsigned bit_width = *in++ & 0x3f;
318 unsigned num_exceptions = *in++;
320 // Decode the base values.
321 BitReader bs(in, bit_width);
322 for (unsigned i = 0; i < num; ++i) {
325 in += bytes_for_packed_bits(num, bit_width);
327 // Decode exceptions.
328 Docid exceptions[BlockSize];
331 for (unsigned i = 0; i < num_exceptions; ++i) {
332 exceptions[i] = read_le<Docid>(in);
336 for (unsigned i = 0; i < num_exceptions; ++i) {
337 in = read_vb(in, &exceptions[i]);
341 for (unsigned i = 0; i < num_exceptions; ++i) {
342 unsigned idx = *in++;
343 out[idx] |= exceptions[i] << bit_width;
347 Docid *prev_out = out - 1;
348 for (unsigned i = 0; i < num; ++i) {
349 out[i] = out[i] + prev_out[i] + 1;
355 // Like decode_pfor_vb(), but the base values are organized in four
356 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
357 template<unsigned BlockSize, class Docid>
358 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
360 const unsigned bit_width = *in++ & 0x3f;
361 unsigned num_exceptions = *in++;
363 // Decode the base values.
364 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
365 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
366 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
367 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
368 for (unsigned i = 0; i < BlockSize / 4; ++i) {
369 out[i * 4 + 0] = bs0.read();
370 out[i * 4 + 1] = bs1.read();
371 out[i * 4 + 2] = bs2.read();
372 out[i * 4 + 3] = bs3.read();
374 in += bytes_for_packed_bits(BlockSize, bit_width);
376 // Decode exceptions.
377 Docid exceptions[BlockSize];
380 for (unsigned i = 0; i < num_exceptions; ++i) {
381 exceptions[i] = read_le<Docid>(in);
385 for (unsigned i = 0; i < num_exceptions; ++i) {
386 in = read_vb(in, &exceptions[i]);
391 for (unsigned i = 0; i < num_exceptions; ++i) {
392 unsigned idx = *in++;
393 out[idx] |= exceptions[i] << bit_width;
397 Docid *prev_out = out - 1;
398 for (unsigned i = 0; i < BlockSize; ++i) {
399 out[i] = out[i] + prev_out[i] + 1;
412 template<unsigned BlockSize, class Docid>
413 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
418 in = read_baseval(in, out++);
420 for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
421 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
422 switch (in[0] >> 6) {
424 if (interleaved && num_this_block == BlockSize) {
425 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
426 in = decode_for_interleaved<BlockSize>(in, out);
428 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
429 in = decode_for(in, num_this_block, out);
432 case BlockType::PFOR_VB:
433 if (interleaved && num_this_block == BlockSize) {
434 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
435 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
437 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
438 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
441 case BlockType::PFOR_BITMAP:
442 if (interleaved && num_this_block == BlockSize) {
443 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
444 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
446 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
447 in = decode_pfor_bitmap(in, num_this_block, out);
450 case BlockType::CONSTANT:
451 dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
452 in = decode_constant(in, num_this_block, out);
460 #endif // !defined(_TURBOPFOR_H)