]> git.sesse.net Git - plocate/blob - turbopfor.h
Start reimplementing the TurboPFor decoding functions.
[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 // Does not properly account for overflow.
111 inline unsigned div_round_up(unsigned val, unsigned div)
112 {
113         return (val + div - 1) / div;
114 }
115
116 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
117 {
118         return div_round_up(num * bit_width, CHAR_BIT);
119 }
120
121 // Constant block. Layout:
122 //
123 //  - Bit width (6 bits) | type << 6
124 //  - Base values (<bits> bits, rounded up to nearest byte)
125 template<class Docid>
126 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
127 {
128         const unsigned bit_width = *in++ & 0x3f;
129         Docid val = read_le<Docid>(in);
130         if (bit_width < sizeof(Docid) * 8) {
131                 val &= ((1U << bit_width) - 1);
132         }
133
134         Docid *prev_out = out - 1;
135         for (unsigned i = 0; i < num; ++i) {
136                 out[i] = val + prev_out[i] + 1;
137         }
138         return in + div_round_up(bit_width, 8);
139 }
140
141 // FOR block (ie., PFor without exceptions). Layout:
142 //
143 //  - Bit width (6 bits) | type << 6
144 //  - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
145 template<class Docid>
146 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
147 {
148         const unsigned bit_width = *in++ & 0x3f;
149
150         Docid *prev_out = out - 1;
151         BitReader bs(in, bit_width);
152         for (unsigned i = 0; i < num; ++i) {
153                 out[i] = bs.read() + prev_out[i] + 1;
154         }
155         return in + bytes_for_packed_bits(num, bit_width);
156 }
157
158 // PFor block with bitmap exceptions. Layout:
159 //
160 //  - Bit width (6 bits) | type << 6
161 //  - Exception bit width (8 bits)
162 //  - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
163 //  - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
164 //  - Base values (<num> values of <bits> bits, rounded up to a byte)
165 template<class Docid>
166 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
167 {
168         memset(out, 0, num * sizeof(Docid));
169
170         const unsigned bit_width = *in++ & 0x3f;
171         const unsigned exception_bit_width = *in++;
172
173         // Decode exceptions.
174         {
175                 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
176                 in += div_round_up(num, 8);
177
178                 int num_exceptions = 0;
179
180                 BitReader bs(in, exception_bit_width);
181                 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
182                         uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
183                         if (num - i < 64) {
184                                 // We've read some bytes past the end, so clear out the junk bits.
185                                 exceptions &= (1ULL << (num - i)) - 1;
186                         }
187                         for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
188                                 unsigned idx = (ffsll(exceptions) - 1) + i;
189                                 out[idx] = bs.read() << bit_width;
190                         }
191                 }
192                 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
193         }
194
195         // Decode the base values, and delta-decode.
196         Docid *prev_out = out - 1;
197         BitReader bs(in, bit_width);
198         for (unsigned i = 0; i < num; ++i) {
199                 out[i] = (out[i] | bs.read()) + prev_out[i] + 1;
200         }
201         return in + bytes_for_packed_bits(num, bit_width);
202 }
203
204 // PFor block with variable-byte exceptions. Layout:
205 //
206 //  - Bit width (6 bits) | type << 6
207 //  - Number of exceptions (8 bits)
208 //  - Base values (<num> values of <bits> bits, rounded up to a byte)
209 //  - Exceptions:
210 //    - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
211 //    - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
212 //  - Indexes of exceptions (<num_exc> bytes).
213 template<unsigned BlockSize, class Docid>
214 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
215 {
216         //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
217
218         const unsigned bit_width = *in++ & 0x3f;
219         unsigned num_exceptions = *in++;
220
221         // Decode the base values.
222         BitReader bs(in, bit_width);
223         for (unsigned i = 0; i < num; ++i) {
224                 out[i] = bs.read();
225         }
226         in += bytes_for_packed_bits(num, bit_width);
227
228         // Decode exceptions.
229         Docid exceptions[BlockSize];
230         if (*in == 255) {
231                 ++in;
232                 for (unsigned i = 0; i < num_exceptions; ++i) {
233                         exceptions[i] = read_le<Docid>(in);
234                         in += sizeof(Docid);
235                 }
236         } else {
237                 for (unsigned i = 0; i < num_exceptions; ++i) {
238                         in = read_vb(in, &exceptions[i]);
239                 }
240         }
241         // Apply exceptions.
242         for (unsigned i = 0; i < num_exceptions; ++i) {
243                 unsigned idx = *in++;
244                 out[idx] |= exceptions[i] << bit_width;
245         }
246
247         // Delta-decode.
248         Docid *prev_out = out - 1;
249         for (unsigned i = 0; i < num; ++i) {
250                 out[i] = out[i] + prev_out[i] + 1;
251         }
252
253         return in;
254 }
255
256 enum BlockType {
257         FOR = 0,
258         PFOR_VB = 1,
259         PFOR_BITMAP = 2,
260         CONSTANT = 3
261 };
262
263 template<unsigned BlockSize, class Docid>
264 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, Docid *out)
265 {
266         if (num == 0) {
267                 return in;
268         }
269         in = read_baseval(in, out++);
270
271         for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
272                 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
273                 switch (in[0] >> 6) {
274                 case BlockType::FOR:
275                         dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
276                         in = decode_for(in, num_this_block, out);
277                         break;
278                 case BlockType::PFOR_VB:
279                         dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
280                         in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
281                         break;
282                 case BlockType::PFOR_BITMAP:
283                         dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
284                         in = decode_pfor_bitmap(in, num_this_block, out);
285                         break;
286                 case BlockType::CONSTANT:
287                         dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
288                         in = decode_constant(in, num_this_block, out);
289                         break;
290                 }
291         }
292
293         return in;
294 }
295
296 #endif  // !defined(_TURBOPFOR_H)