1 #ifndef _TURBOPFOR_ENCODE_H
2 #define _TURBOPFOR_ENCODE_H
4 // Much like turbopfor.h (and shares all of the same caveats), except this is
5 // for encoding. It is _much_ slower than the reference implementation, but we
6 // encode only during build, and most time in build is spent in other things
7 // than encoding posting lists, so it only costs ~5-10% overall. Does not use
8 // any special character sets, and generally isn't optimized at all.
10 // It encodes about 0.01% denser than the reference encoder (averaged over
11 // a real plocate corpus), probably since it has a slower but more precise
12 // method for estimating the cost of a PFOR + varbyte block.
14 #include "turbopfor-common.h"
23 void write_le(Docid val, void *out)
25 if constexpr (sizeof(Docid) == 8) {
27 } else if constexpr (sizeof(Docid) == 4) {
29 } else if constexpr (sizeof(Docid) == 2) {
31 } else if constexpr (sizeof(Docid) == 1) {
36 memcpy(out, &val, sizeof(val));
39 // Corresponds to read_baseval.
41 unsigned char *write_baseval(Docid in, unsigned char *out)
46 } else if (in < 0x4000) {
47 out[0] = (in >> 8) | 0x80;
50 } else if (in < 0x200000) {
51 out[0] = (in >> 16) | 0xc0;
53 out[2] = (in >> 8) & 0xff;
55 } else if (in < 0x10000000) {
56 out[0] = (in >> 24) | 0xe0;
57 out[1] = (in >> 16) & 0xff;
58 out[2] = (in >> 8) & 0xff;
62 assert(false); // Not implemented.
66 // Writes a varbyte-encoded exception.
68 unsigned char *write_vb(Docid val, unsigned char *out)
73 } else if (val <= 16560) {
75 *out++ = (val >> 8) + 177;
78 } else if (val <= 540848) {
80 *out = (val >> 16) + 241;
81 write_le<uint16_t>(val & 0xffff, out + 1);
83 } else if (val <= 16777215) {
85 write_le<uint32_t>(val, out + 1);
89 write_le<uint32_t>(val, out + 1);
95 inline unsigned num_bits(Docid x)
101 return sizeof(Docid) * CHAR_BIT - __builtin_clz(x);
104 for (int i = sizeof(Docid) * CHAR_BIT; i-- > 0; ) {
105 if (x & (Docid{1} << i)) {
115 BitWriter(unsigned char *out, unsigned bits)
116 : out(out), bits(bits) {}
117 void write(uint32_t val)
119 cur_val |= val << bits_used;
120 write_le<uint32_t>(cur_val, out);
123 cur_val >>= (bits_used / 8) * 8;
124 out += bits_used / 8;
131 unsigned bits_used = 0;
132 unsigned cur_val = 0;
135 template<unsigned NumStreams>
136 struct InterleavedBitWriter {
138 InterleavedBitWriter(unsigned char *out, unsigned bits)
139 : out(out), bits(bits) {}
140 void write(uint32_t val)
142 cur_val |= uint64_t(val) << bits_used;
143 if (bits_used + bits >= 32) {
144 write_le<uint32_t>(cur_val & 0xffffffff, out);
147 bits_used -= 32; // Underflow, but will be fixed below.
149 write_le<uint32_t>(cur_val, out);
154 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
157 unsigned bits_used = 0;
158 uint64_t cur_val = 0;
161 // Bitpacks a set of values (making sure the top bits are lopped off).
162 // If interleaved is set, makes SSE2-compatible interleaving (this is
163 // only allowed for full blocks).
164 template<class Docid>
165 unsigned char *encode_bitmap(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
167 unsigned mask = mask_for_bits(bit_width);
169 InterleavedBitWriter<4> bs0(out + 0 * sizeof(uint32_t), bit_width);
170 InterleavedBitWriter<4> bs1(out + 1 * sizeof(uint32_t), bit_width);
171 InterleavedBitWriter<4> bs2(out + 2 * sizeof(uint32_t), bit_width);
172 InterleavedBitWriter<4> bs3(out + 3 * sizeof(uint32_t), bit_width);
173 assert(num % 4 == 0);
174 for (unsigned i = 0; i < num / 4; ++i) {
175 bs0.write(in[i * 4 + 0] & mask);
176 bs1.write(in[i * 4 + 1] & mask);
177 bs2.write(in[i * 4 + 2] & mask);
178 bs3.write(in[i * 4 + 3] & mask);
181 BitWriter bs(out, bit_width);
182 for (unsigned i = 0; i < num; ++i) {
183 bs.write(in[i] & mask);
186 return out + bytes_for_packed_bits(num, bit_width);
189 // See decode_for() for the format.
190 template<class Docid>
191 unsigned char *encode_for(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
193 return encode_bitmap(in, num, bit_width, interleaved, out);
196 // See decode_pfor_bitmap() for the format.
197 template<class Docid>
198 unsigned char *encode_pfor_bitmap(const Docid *in, unsigned num, unsigned bit_width, unsigned exception_bit_width, bool interleaved, unsigned char *out)
200 *out++ = exception_bit_width;
202 // Bitmap of exceptions.
204 BitWriter bs(out, 1);
205 for (unsigned i = 0; i < num; ++i) {
206 bs.write((in[i] >> bit_width) != 0);
208 out += bytes_for_packed_bits(num, 1);
213 BitWriter bs(out, exception_bit_width);
214 unsigned num_exceptions = 0;
215 for (unsigned i = 0; i < num; ++i) {
216 if ((in[i] >> bit_width) != 0) {
217 bs.write(in[i] >> bit_width);
221 out += bytes_for_packed_bits(num_exceptions, exception_bit_width);
225 out = encode_bitmap(in, num, bit_width, interleaved, out);
230 // See decode_pfor_vb() for the format.
231 template<class Docid>
232 unsigned char *encode_pfor_vb(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
234 unsigned num_exceptions = 0;
235 for (unsigned i = 0; i < num; ++i) {
236 if ((in[i] >> bit_width) != 0) {
240 *out++ = num_exceptions;
243 out = encode_bitmap(in, num, bit_width, interleaved, out);
246 for (unsigned i = 0; i < num; ++i) {
247 unsigned val = in[i] >> bit_width;
249 out = write_vb(val, out);
253 // Exception indexes.
254 for (unsigned i = 0; i < num; ++i) {
255 unsigned val = in[i] >> bit_width;
264 // Find out which block type would be the smallest for the given data.
265 template<class Docid>
266 BlockType decide_block_type(const Docid *in, unsigned num, unsigned *bit_width, unsigned *exception_bit_width)
268 // Check if the block is constant.
269 bool constant = true;
270 for (unsigned i = 1; i < num; ++i) {
271 if (in[i] != in[0]) {
277 *bit_width = num_bits(in[0]);
278 return BlockType::CONSTANT;
281 // Build up a histogram of bit sizes.
282 unsigned histogram[sizeof(Docid) * CHAR_BIT + 1] = { 0 };
283 unsigned max_bits = 0;
284 for (unsigned i = 0; i < num; ++i) {
285 unsigned bits = num_bits(in[i]);
287 max_bits = std::max(max_bits, bits);
291 unsigned best_cost = bytes_for_packed_bits(num, max_bits);
292 unsigned best_bit_width = max_bits;
294 // Try PFOR with bitmap exceptions.
295 const unsigned bitmap_cost = bytes_for_packed_bits(num, 1);
296 unsigned num_exceptions = 0;
297 for (unsigned exception_bit_width = 1; exception_bit_width <= max_bits; ++exception_bit_width) {
298 unsigned test_bit_width = max_bits - exception_bit_width;
299 num_exceptions += histogram[test_bit_width + 1];
301 // 1 byte for signaling exception bit width, then the bitmap,
302 // then the base values, then the exceptions.
303 unsigned cost = 1 + bitmap_cost + bytes_for_packed_bits(num, test_bit_width) +
304 bytes_for_packed_bits(num_exceptions, exception_bit_width);
305 if (cost < best_cost) {
307 best_bit_width = test_bit_width;
311 // Try PFOR with varbyte exceptions.
312 bool best_is_varbyte = false;
313 for (unsigned test_bit_width = 0; test_bit_width < max_bits; ++test_bit_width) {
314 // 1 byte for signaling number of exceptions, plus the base values,
315 // and then we count up the varbytes and indexes. (This is precise
317 unsigned cost = 1 + bytes_for_packed_bits(num, test_bit_width);
318 for (unsigned i = 0; i < num && cost < best_cost; ++i) {
319 unsigned val = in[i] >> test_bit_width;
321 // Not stored, and then also no index.
322 } else if (val <= 176) {
324 } else if (val <= 16560) {
326 } else if (val <= 540848) {
328 } else if (val <= 16777215) {
334 if (cost < best_cost) {
336 best_bit_width = test_bit_width;
337 best_is_varbyte = true;
341 // TODO: Consider the last-resort option of just raw storage (255).
343 if (best_is_varbyte) {
344 *bit_width = best_bit_width;
345 return BlockType::PFOR_VB;
346 } else if (best_bit_width == max_bits) {
347 *bit_width = max_bits;
348 return BlockType::FOR;
350 *bit_width = best_bit_width;
351 *exception_bit_width = max_bits - best_bit_width;
352 return BlockType::PFOR_BITMAP;
356 // The basic entry point. Takes one block of integers (which already must
357 // be delta-minus-1-encoded) and packs it into TurboPFor format.
358 // interleaved corresponds to the interleaved parameter in decode_pfor_delta1()
359 // or the ā128vā infix in the reference code's function names; such formats
360 // are much faster to decode, so for full blocks, you probably want it.
361 // The interleaved flag isn't stored anywhere; it's implicit whether you
362 // want to use it for full blocks or not.
364 // The first value must already be written using write_baseval() (so the delta
365 // coding starts from the second value). Returns the end of the string.
366 // May write 4 bytes past the end.
367 template<unsigned BlockSize, class Docid>
368 unsigned char *encode_pfor_single_block(const Docid *in, unsigned num, bool interleaved, unsigned char *out)
372 assert(num == BlockSize);
375 unsigned bit_width, exception_bit_width;
376 BlockType block_type = decide_block_type(in, num, &bit_width, &exception_bit_width);
377 *out++ = (block_type << 6) | bit_width;
379 switch (block_type) {
380 case BlockType::CONSTANT: {
381 unsigned bit_width = num_bits(in[0]);
382 write_le<Docid>(in[0], out);
383 return out + div_round_up(bit_width, 8);
386 return encode_for(in, num, bit_width, interleaved, out);
387 case BlockType::PFOR_BITMAP:
388 return encode_pfor_bitmap(in, num, bit_width, exception_bit_width, interleaved, out);
389 case BlockType::PFOR_VB:
390 return encode_pfor_vb(in, num, bit_width, interleaved, out);
396 #endif // !defined(_TURBOPFOR_ENCODE_H)