]> git.sesse.net Git - plocate/blob - turbopfor.h
Small refactoring to reduce code duplication.
[plocate] / turbopfor.h
1 #ifndef _TURBOPFOR_H
2 #define _TURBOPFOR_H 1
3
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.
10 //
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
13 // TurboPFor code.
14
15 #include <assert.h>
16 #include <endian.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <limits.h>
20
21 #include <algorithm>
22
23 template<class Docid>
24 Docid read_le(const void *in)
25 {
26         Docid val;
27         memcpy(&val, in, sizeof(val));
28         if constexpr (sizeof(Docid) == 8) {
29                 return le64toh(val);
30         } else if constexpr (sizeof(Docid) == 4) {
31                 return le32toh(val);
32         } else if constexpr (sizeof(Docid) == 2) {
33                 return le16toh(val);
34         } else if constexpr (sizeof(Docid) == 1) {
35                 return val;
36         } else {
37                 assert(false);
38         }
39 }
40
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.
43 template<class Docid>
44 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
45 {
46         //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
47         if (*in < 128) {
48                 *out = *in;
49                 return in + 1;
50         } else if (*in < 192) {
51                 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
52                 return in + 2;
53         } else if (*in < 224) {
54                 *out = ((uint32_t(in[0]) << 16) |
55                         (uint32_t(in[2]) << 8) |
56                         (uint32_t(in[1]))) & 0x1fffff;
57                 return in + 3;
58         } else {
59                 assert(false);  // Not implemented.
60         }
61 }
62
63 template<class Docid>
64 const unsigned char *read_vb(const unsigned char *in, Docid *out)
65 {
66         if (*in <= 176) {
67                 *out = *in;
68                 return in + 1;
69         } else if (*in <= 240) {
70                 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
71                 return in + 2;
72         } else if (*in <= 248) {
73                 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
74                 return in + 3;
75         } else if (*in == 249) {
76                 *out = (uint32_t(in[1])) |
77                         (uint32_t(in[2]) << 8) |
78                         (uint32_t(in[3]) << 16);
79                 return in + 4;
80         } else if (*in == 250) {
81                 *out = read_le<uint32_t>(in + 1);
82                 return in + 5;
83         } else {
84                 assert(false);
85         }
86 }
87
88 struct BitReader {
89 public:
90         BitReader(const unsigned char *in, unsigned bits)
91                 : in(in), bits(bits), mask((1U << bits) - 1) {}
92         uint32_t read()
93         {
94                 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
95
96                 bits_used += bits;
97                 in += bits_used / 8;
98                 bits_used %= 8;
99
100                 return val;
101         }
102
103 private:
104         const unsigned char *in;
105         const unsigned bits;
106         const unsigned mask;
107         unsigned bits_used = 0;
108 };
109
110 template<unsigned NumStreams>
111 struct InterleavedBitReader {
112 public:
113         InterleavedBitReader(const unsigned char *in, unsigned bits)
114                 : in(in), bits(bits), mask((1U << bits) - 1) {}
115         uint32_t read()
116         {
117                 uint32_t val;
118                 if (bits_used + bits > 32) {
119                         val = (read_le<uint32_t>(in) >> bits_used) | (read_le<uint32_t>(in + Stride) << (32 - bits_used));
120                 } else {
121                         val = (read_le<uint32_t>(in) >> bits_used);
122                 }
123
124                 bits_used += bits;
125                 in += Stride * (bits_used / 32);
126                 bits_used %= 32;
127
128                 return val & mask;
129         }
130
131 private:
132         static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
133         const unsigned char *in;
134         const unsigned bits;
135         const unsigned mask;
136         unsigned bits_used = 0;
137 };
138
139 // Does not properly account for overflow.
140 inline unsigned div_round_up(unsigned val, unsigned div)
141 {
142         return (val + div - 1) / div;
143 }
144
145 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
146 {
147         return div_round_up(num * bit_width, CHAR_BIT);
148 }
149
150 // Constant block. Layout:
151 //
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)
156 {
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);
161         }
162
163         Docid prev_val = out[-1];
164         for (unsigned i = 0; i < num; ++i) {
165                 out[i] = prev_val = val + prev_val + 1;
166         }
167         return in + div_round_up(bit_width, 8);
168 }
169
170 // FOR block (ie., PFor without exceptions). Layout:
171 //
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)
176 {
177         const unsigned bit_width = *in++ & 0x3f;
178
179         Docid prev_val = out[-1];
180         BitReader bs(in, bit_width);
181         for (unsigned i = 0; i < num; ++i) {
182                 prev_val = out[i] = bs.read() + prev_val + 1;
183         }
184         return in + bytes_for_packed_bits(num, bit_width);
185 }
186
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)
191 {
192         const unsigned bit_width = *in++ & 0x3f;
193
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();
203         }
204         Docid prev_val = out[-1];
205         for (unsigned i = 0; i < BlockSize; ++i) {
206                 out[i] = prev_val = out[i] + prev_val + 1;
207         }
208         return in + bytes_for_packed_bits(BlockSize, bit_width);
209 }
210
211 template<class Docid>
212 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, unsigned bit_width, Docid *out)
213 {
214         const unsigned exception_bit_width = *in++;
215         const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
216         in += div_round_up(num, 8);
217
218         int num_exceptions = 0;
219
220         BitReader bs(in, exception_bit_width);
221         for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
222                 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
223                 if (num - i < 64) {
224                         // We've read some bytes past the end, so clear out the junk bits.
225                         exceptions &= (1ULL << (num - i)) - 1;
226                 }
227                 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
228                         unsigned idx = (ffsll(exceptions) - 1) + i;
229                         out[idx] = bs.read() << bit_width;
230                 }
231         }
232         in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
233         return in;
234 }
235
236 // PFor block with bitmap exceptions. Layout:
237 //
238 //  - Bit width (6 bits) | type << 6
239 //  - Exception bit width (8 bits)
240 //  - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
241 //  - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
242 //  - Base values (<num> values of <bits> bits, rounded up to a byte)
243 template<class Docid>
244 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
245 {
246         memset(out, 0, num * sizeof(Docid));
247
248         const unsigned bit_width = *in++ & 0x3f;
249
250         in = decode_pfor_bitmap_exceptions(in, num, bit_width, out);
251
252         // Decode the base values, and delta-decode.
253         Docid prev_val = out[-1];
254         BitReader bs(in, bit_width);
255         for (unsigned i = 0; i < num; ++i) {
256                 out[i] = prev_val = (out[i] | bs.read()) + prev_val + 1;
257         }
258         return in + bytes_for_packed_bits(num, bit_width);
259 }
260
261 // Like decode_pfor_bitmap(), but the base values are organized in four
262 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
263 template<unsigned BlockSize, class Docid>
264 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
265 {
266         memset(out, 0, BlockSize * sizeof(Docid));
267
268         const unsigned bit_width = *in++ & 0x3f;
269
270         in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
271
272         // Decode the base values.
273         InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
274         InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
275         InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
276         InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
277         for (unsigned i = 0; i < BlockSize / 4; ++i) {
278                 out[i * 4 + 0] |= bs0.read();
279                 out[i * 4 + 1] |= bs1.read();
280                 out[i * 4 + 2] |= bs2.read();
281                 out[i * 4 + 3] |= bs3.read();
282         }
283
284         // Delta-decode.
285         Docid prev_val = out[-1];
286         for (unsigned i = 0; i < BlockSize; ++i) {
287                 out[i] = prev_val = out[i] + prev_val + 1;
288         }
289         return in + bytes_for_packed_bits(BlockSize, bit_width);
290 }
291
292 // PFor block with variable-byte exceptions. Layout:
293 //
294 //  - Bit width (6 bits) | type << 6
295 //  - Number of exceptions (8 bits)
296 //  - Base values (<num> values of <bits> bits, rounded up to a byte)
297 //  - Exceptions:
298 //    - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
299 //    - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
300 //  - Indexes of exceptions (<num_exc> bytes).
301 template<unsigned BlockSize, class Docid>
302 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
303 {
304         //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
305
306         const unsigned bit_width = *in++ & 0x3f;
307         unsigned num_exceptions = *in++;
308
309         // Decode the base values.
310         BitReader bs(in, bit_width);
311         for (unsigned i = 0; i < num; ++i) {
312                 out[i] = bs.read();
313         }
314         in += bytes_for_packed_bits(num, bit_width);
315
316         // Decode exceptions.
317         Docid exceptions[BlockSize];
318         if (*in == 255) {
319                 ++in;
320                 for (unsigned i = 0; i < num_exceptions; ++i) {
321                         exceptions[i] = read_le<Docid>(in);
322                         in += sizeof(Docid);
323                 }
324         } else {
325                 for (unsigned i = 0; i < num_exceptions; ++i) {
326                         in = read_vb(in, &exceptions[i]);
327                 }
328         }
329         // Apply exceptions.
330         for (unsigned i = 0; i < num_exceptions; ++i) {
331                 unsigned idx = *in++;
332                 out[idx] |= exceptions[i] << bit_width;
333         }
334
335         // Delta-decode.
336         Docid prev_val = out[-1];
337         for (unsigned i = 0; i < num; ++i) {
338                 out[i] = prev_val = out[i] + prev_val + 1;
339         }
340
341         return in;
342 }
343
344 // Like decode_pfor_vb(), but the base values are organized in four
345 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
346 template<unsigned BlockSize, class Docid>
347 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
348 {
349         const unsigned bit_width = *in++ & 0x3f;
350         unsigned num_exceptions = *in++;
351
352         // Decode the base values.
353         InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
354         InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
355         InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
356         InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
357         for (unsigned i = 0; i < BlockSize / 4; ++i) {
358                 out[i * 4 + 0] = bs0.read();
359                 out[i * 4 + 1] = bs1.read();
360                 out[i * 4 + 2] = bs2.read();
361                 out[i * 4 + 3] = bs3.read();
362         }
363         in += bytes_for_packed_bits(BlockSize, bit_width);
364
365         // Decode exceptions.
366         Docid exceptions[BlockSize];
367         if (*in == 255) {
368                 ++in;
369                 for (unsigned i = 0; i < num_exceptions; ++i) {
370                         exceptions[i] = read_le<Docid>(in);
371                         in += sizeof(Docid);
372                 }
373         } else {
374                 for (unsigned i = 0; i < num_exceptions; ++i) {
375                         in = read_vb(in, &exceptions[i]);
376                 }
377         }
378
379         // Apply exceptions.
380         for (unsigned i = 0; i < num_exceptions; ++i) {
381                 unsigned idx = *in++;
382                 out[idx] |= exceptions[i] << bit_width;
383         }
384
385         // Delta-decode.
386         Docid prev_val = out[-1];
387         for (unsigned i = 0; i < BlockSize; ++i) {
388                 out[i] = prev_val = out[i] + prev_val + 1;
389         }
390
391         return in;
392 }
393
394 enum BlockType {
395         FOR = 0,
396         PFOR_VB = 1,
397         PFOR_BITMAP = 2,
398         CONSTANT = 3
399 };
400
401 template<unsigned BlockSize, class Docid>
402 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
403 {
404         if (num == 0) {
405                 return in;
406         }
407         in = read_baseval(in, out++);
408
409         for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
410                 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
411                 switch (in[0] >> 6) {
412                 case BlockType::FOR:
413                         if (interleaved && num_this_block == BlockSize) {
414                                 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
415                                 in = decode_for_interleaved<BlockSize>(in, out);
416                         } else {
417                                 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
418                                 in = decode_for(in, num_this_block, out);
419                         }
420                         break;
421                 case BlockType::PFOR_VB:
422                         if (interleaved && num_this_block == BlockSize) {
423                                 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
424                                 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
425                         } else {
426                                 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
427                                 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
428                         }
429                         break;
430                 case BlockType::PFOR_BITMAP:
431                         if (interleaved && num_this_block == BlockSize) {
432                                 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
433                                 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
434                         } else {
435                                 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
436                                 in = decode_pfor_bitmap(in, num_this_block, out);
437                         }
438                         break;
439                 case BlockType::CONSTANT:
440                         dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
441                         in = decode_constant(in, num_this_block, out);
442                         break;
443                 }
444         }
445
446         return in;
447 }
448
449 #endif  // !defined(_TURBOPFOR_H)