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;
56 assert(false); // Not implemented.
60 // Writes a varbyte-encoded exception.
62 unsigned char *write_vb(Docid val, unsigned char *out)
67 } else if (val <= 16560) {
69 *out++ = (val >> 8) + 177;
72 } else if (val <= 540848) {
74 *out = (val >> 16) + 241;
75 write_le<uint16_t>(val & 0xffff, out + 1);
77 } else if (val <= 16777215) {
79 write_le<uint32_t>(val, out + 1);
83 write_le<uint32_t>(val, out + 1);
89 inline unsigned num_bits(Docid x)
94 return sizeof(Docid) * CHAR_BIT - __builtin_clz(x);
100 BitWriter(unsigned char *out, unsigned bits)
101 : out(out), bits(bits) {}
102 void write(uint32_t val)
104 cur_val |= val << bits_used;
105 write_le<uint32_t>(cur_val, out);
108 cur_val >>= (bits_used / 8) * 8;
109 out += bits_used / 8;
116 unsigned bits_used = 0;
117 unsigned cur_val = 0;
120 template<unsigned NumStreams>
121 struct InterleavedBitWriter {
123 InterleavedBitWriter(unsigned char *out, unsigned bits)
124 : out(out), bits(bits) {}
125 void write(uint32_t val)
127 cur_val |= uint64_t(val) << bits_used;
128 if (bits_used + bits >= 32) {
129 write_le<uint32_t>(cur_val & 0xffffffff, out);
132 bits_used -= 32; // Underflow, but will be fixed below.
134 write_le<uint32_t>(cur_val, out);
139 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
142 unsigned bits_used = 0;
143 uint64_t cur_val = 0;
146 // Bitpacks a set of values (making sure the top bits are lopped off).
147 // If interleaved is set, makes SSE2-compatible interleaving (this is
148 // only allowed for full blocks).
149 template<class Docid>
150 unsigned char *encode_bitmap(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
152 unsigned mask = mask_for_bits(bit_width);
154 InterleavedBitWriter<4> bs0(out + 0 * sizeof(uint32_t), bit_width);
155 InterleavedBitWriter<4> bs1(out + 1 * sizeof(uint32_t), bit_width);
156 InterleavedBitWriter<4> bs2(out + 2 * sizeof(uint32_t), bit_width);
157 InterleavedBitWriter<4> bs3(out + 3 * sizeof(uint32_t), bit_width);
158 assert(num % 4 == 0);
159 for (unsigned i = 0; i < num / 4; ++i) {
160 bs0.write(in[i * 4 + 0] & mask);
161 bs1.write(in[i * 4 + 1] & mask);
162 bs2.write(in[i * 4 + 2] & mask);
163 bs3.write(in[i * 4 + 3] & mask);
166 BitWriter bs(out, bit_width);
167 for (unsigned i = 0; i < num; ++i) {
168 bs.write(in[i] & mask);
171 return out + bytes_for_packed_bits(num, bit_width);
174 // See decode_for() for the format.
175 template<class Docid>
176 unsigned char *encode_for(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
178 return encode_bitmap(in, num, bit_width, interleaved, out);
181 // See decode_pfor_bitmap() for the format.
182 template<class Docid>
183 unsigned char *encode_pfor_bitmap(const Docid *in, unsigned num, unsigned bit_width, unsigned exception_bit_width, bool interleaved, unsigned char *out)
185 *out++ = exception_bit_width;
187 // Bitmap of exceptions.
189 BitWriter bs(out, 1);
190 for (unsigned i = 0; i < num; ++i) {
191 bs.write((in[i] >> bit_width) != 0);
193 out += bytes_for_packed_bits(num, 1);
198 BitWriter bs(out, exception_bit_width);
199 unsigned num_exceptions = 0;
200 for (unsigned i = 0; i < num; ++i) {
201 if ((in[i] >> bit_width) != 0) {
202 bs.write(in[i] >> bit_width);
206 out += bytes_for_packed_bits(num_exceptions, exception_bit_width);
210 out = encode_bitmap(in, num, bit_width, interleaved, out);
215 // See decode_pfor_vb() for the format.
216 template<class Docid>
217 unsigned char *encode_pfor_vb(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
219 unsigned num_exceptions = 0;
220 for (unsigned i = 0; i < num; ++i) {
221 if ((in[i] >> bit_width) != 0) {
225 *out++ = num_exceptions;
228 out = encode_bitmap(in, num, bit_width, interleaved, out);
231 for (unsigned i = 0; i < num; ++i) {
232 unsigned val = in[i] >> bit_width;
234 out = write_vb(val, out);
238 // Exception indexes.
239 for (unsigned i = 0; i < num; ++i) {
240 unsigned val = in[i] >> bit_width;
249 // Find out which block type would be the smallest for the given data.
250 template<class Docid>
251 BlockType decide_block_type(const Docid *in, unsigned num, unsigned *bit_width, unsigned *exception_bit_width)
253 // Check if the block is constant.
254 bool constant = true;
255 for (unsigned i = 1; i < num; ++i) {
256 if (in[i] != in[0]) {
262 *bit_width = num_bits(in[0]);
263 return BlockType::CONSTANT;
266 // Build up a histogram of bit sizes.
267 unsigned histogram[sizeof(Docid) * CHAR_BIT + 1] = { 0 };
268 unsigned max_bits = 0;
269 for (unsigned i = 0; i < num; ++i) {
270 unsigned bits = num_bits(in[i]);
272 max_bits = std::max(max_bits, bits);
276 unsigned best_cost = bytes_for_packed_bits(num, max_bits);
277 unsigned best_bit_width = max_bits;
279 // Try PFOR with bitmap exceptions.
280 const unsigned bitmap_cost = bytes_for_packed_bits(num, 1);
281 unsigned num_exceptions = 0;
282 for (unsigned exception_bit_width = 1; exception_bit_width <= max_bits; ++exception_bit_width) {
283 unsigned test_bit_width = max_bits - exception_bit_width;
284 num_exceptions += histogram[test_bit_width + 1];
286 // 1 byte for signaling exception bit width, then the bitmap,
287 // then the base values, then the exceptions.
288 unsigned cost = 1 + bitmap_cost + bytes_for_packed_bits(num, test_bit_width) +
289 bytes_for_packed_bits(num_exceptions, exception_bit_width);
290 if (cost < best_cost) {
292 best_bit_width = test_bit_width;
296 // Try PFOR with varbyte exceptions.
297 bool best_is_varbyte = false;
298 for (unsigned test_bit_width = 0; test_bit_width < max_bits; ++test_bit_width) {
299 // 1 byte for signaling number of exceptions, plus the base values,
300 // and then we count up the varbytes and indexes. (This is precise
302 unsigned cost = 1 + bytes_for_packed_bits(num, test_bit_width);
303 for (unsigned i = 0; i < num && cost < best_cost; ++i) {
304 unsigned val = in[i] >> test_bit_width;
306 // Not stored, and then also no index.
307 } else if (val <= 176) {
309 } else if (val <= 16560) {
311 } else if (val <= 540848) {
313 } else if (val <= 16777215) {
319 if (cost < best_cost) {
321 best_bit_width = test_bit_width;
322 best_is_varbyte = true;
326 // TODO: Consider the last-resort option of just raw storage (255).
328 if (best_is_varbyte) {
329 *bit_width = best_bit_width;
330 return BlockType::PFOR_VB;
331 } else if (best_bit_width == max_bits) {
332 *bit_width = max_bits;
333 return BlockType::FOR;
335 *bit_width = best_bit_width;
336 *exception_bit_width = max_bits - best_bit_width;
337 return BlockType::PFOR_BITMAP;
341 // The basic entry point. Takes one block of integers (which already must
342 // be delta-minus-1-encoded) and packs it into TurboPFor format.
343 // interleaved corresponds to the interleaved parameter in decode_pfor_delta1()
344 // or the ā128vā infix in the reference code's function names; such formats
345 // are much faster to decode, so for full blocks, you probably want it.
346 // The interleaved flag isn't stored anywhere; it's implicit whether you
347 // want to use it for full blocks or not.
349 // The first value must already be written using write_baseval() (so the delta
350 // coding starts from the second value). Returns the end of the string.
351 // May write 4 bytes past the end.
352 template<unsigned BlockSize, class Docid>
353 unsigned char *encode_pfor_single_block(const Docid *in, unsigned num, bool interleaved, unsigned char *out)
357 assert(num == BlockSize);
360 unsigned bit_width, exception_bit_width;
361 BlockType block_type = decide_block_type(in, num, &bit_width, &exception_bit_width);
362 *out++ = (block_type << 6) | bit_width;
364 switch (block_type) {
365 case BlockType::CONSTANT: {
366 unsigned bit_width = num_bits(in[0]);
367 write_le<Docid>(in[0], out);
368 return out + div_round_up(bit_width, 8);
371 return encode_for(in, num, bit_width, interleaved, out);
372 case BlockType::PFOR_BITMAP:
373 return encode_pfor_bitmap(in, num, bit_width, exception_bit_width, interleaved, out);
374 case BlockType::PFOR_VB:
375 return encode_pfor_vb(in, num, bit_width, interleaved, out);
381 #endif // !defined(_TURBOPFOR_ENCODE_H)