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 // Does not properly account for overflow.
111 inline unsigned div_round_up(unsigned val, unsigned div)
113 return (val + div - 1) / div;
116 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
118 return div_round_up(num * bit_width, CHAR_BIT);
121 // Constant block. Layout:
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)
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);
134 Docid *prev_out = out - 1;
135 for (unsigned i = 0; i < num; ++i) {
136 out[i] = val + prev_out[i] + 1;
138 return in + div_round_up(bit_width, 8);
141 // FOR block (ie., PFor without exceptions). Layout:
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)
148 const unsigned bit_width = *in++ & 0x3f;
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;
155 return in + bytes_for_packed_bits(num, bit_width);
158 // PFor block with bitmap exceptions. Layout:
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)
168 memset(out, 0, num * sizeof(Docid));
170 const unsigned bit_width = *in++ & 0x3f;
171 const unsigned exception_bit_width = *in++;
173 // Decode exceptions.
175 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
176 in += div_round_up(num, 8);
178 int num_exceptions = 0;
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);
184 // We've read some bytes past the end, so clear out the junk bits.
185 exceptions &= (1ULL << (num - i)) - 1;
187 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
188 unsigned idx = (ffsll(exceptions) - 1) + i;
189 out[idx] = bs.read() << bit_width;
192 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
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;
201 return in + bytes_for_packed_bits(num, bit_width);
204 // PFor block with variable-byte exceptions. Layout:
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)
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)
216 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
218 const unsigned bit_width = *in++ & 0x3f;
219 unsigned num_exceptions = *in++;
221 // Decode the base values.
222 BitReader bs(in, bit_width);
223 for (unsigned i = 0; i < num; ++i) {
226 in += bytes_for_packed_bits(num, bit_width);
228 // Decode exceptions.
229 Docid exceptions[BlockSize];
232 for (unsigned i = 0; i < num_exceptions; ++i) {
233 exceptions[i] = read_le<Docid>(in);
237 for (unsigned i = 0; i < num_exceptions; ++i) {
238 in = read_vb(in, &exceptions[i]);
242 for (unsigned i = 0; i < num_exceptions; ++i) {
243 unsigned idx = *in++;
244 out[idx] |= exceptions[i] << bit_width;
248 Docid *prev_out = out - 1;
249 for (unsigned i = 0; i < num; ++i) {
250 out[i] = out[i] + prev_out[i] + 1;
263 template<unsigned BlockSize, class Docid>
264 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, Docid *out)
269 in = read_baseval(in, out++);
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) {
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);
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);
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);
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);
296 #endif // !defined(_TURBOPFOR_H)