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"
24 void write_le(Docid val, void *out)
26 if constexpr (sizeof(Docid) == 8) {
28 } else if constexpr (sizeof(Docid) == 4) {
30 } else if constexpr (sizeof(Docid) == 2) {
32 } else if constexpr (sizeof(Docid) == 1) {
37 memcpy(out, &val, sizeof(val));
40 // Corresponds to read_baseval.
42 unsigned char *write_baseval(Docid in, unsigned char *out)
47 } else if (in < 0x4000) {
48 out[0] = (in >> 8) | 0x80;
51 } else if (in < 0x200000) {
52 out[0] = (in >> 16) | 0xc0;
54 out[2] = (in >> 8) & 0xff;
57 assert(false); // Not implemented.
61 // Writes a varbyte-encoded exception.
63 unsigned char *write_vb(Docid val, unsigned char *out)
68 } else if (val <= 16560) {
70 *out++ = (val >> 8) + 177;
73 } else if (val <= 540848) {
75 *out = (val >> 16) + 241;
76 write_le<uint16_t>(val & 0xffff, out + 1);
78 } else if (val <= 16777215) {
80 write_le<uint32_t>(val, out + 1);
84 write_le<uint32_t>(val, out + 1);
90 inline unsigned num_bits(Docid x)
95 return sizeof(Docid) * CHAR_BIT - __builtin_clz(x);
101 BitWriter(unsigned char *out, unsigned bits)
102 : out(out), bits(bits) {}
103 void write(uint32_t val)
105 cur_val |= val << bits_used;
106 write_le<uint32_t>(cur_val, out);
109 cur_val >>= (bits_used / 8) * 8;
110 out += bits_used / 8;
117 unsigned bits_used = 0;
118 unsigned cur_val = 0;
121 template<unsigned NumStreams>
122 struct InterleavedBitWriter {
124 InterleavedBitWriter(unsigned char *out, unsigned bits)
125 : out(out), bits(bits) {}
126 void write(uint32_t val)
128 cur_val |= uint64_t(val) << bits_used;
129 if (bits_used + bits >= 32) {
130 write_le<uint32_t>(cur_val & 0xffffffff, out);
133 bits_used -= 32; // Underflow, but will be fixed below.
135 write_le<uint32_t>(cur_val, out);
140 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
143 unsigned bits_used = 0;
144 uint64_t cur_val = 0;
147 // Bitpacks a set of values (making sure the top bits are lopped off).
148 // If interleaved is set, makes SSE2-compatible interleaving (this is
149 // only allowed for full blocks).
150 template<class Docid>
151 unsigned char *encode_bitmap(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
153 unsigned mask = mask_for_bits(bit_width);
155 InterleavedBitWriter<4> bs0(out + 0 * sizeof(uint32_t), bit_width);
156 InterleavedBitWriter<4> bs1(out + 1 * sizeof(uint32_t), bit_width);
157 InterleavedBitWriter<4> bs2(out + 2 * sizeof(uint32_t), bit_width);
158 InterleavedBitWriter<4> bs3(out + 3 * sizeof(uint32_t), bit_width);
159 assert(num % 4 == 0);
160 for (unsigned i = 0; i < num / 4; ++i) {
161 bs0.write(in[i * 4 + 0] & mask);
162 bs1.write(in[i * 4 + 1] & mask);
163 bs2.write(in[i * 4 + 2] & mask);
164 bs3.write(in[i * 4 + 3] & mask);
167 BitWriter bs(out, bit_width);
168 for (unsigned i = 0; i < num; ++i) {
169 bs.write(in[i] & mask);
172 return out + bytes_for_packed_bits(num, bit_width);
175 // See decode_for() for the format.
176 template<class Docid>
177 unsigned char *encode_for(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
179 return encode_bitmap(in, num, bit_width, interleaved, out);
182 // See decode_pfor_bitmap() for the format.
183 template<class Docid>
184 unsigned char *encode_pfor_bitmap(const Docid *in, unsigned num, unsigned bit_width, unsigned exception_bit_width, bool interleaved, unsigned char *out)
186 *out++ = exception_bit_width;
188 // Bitmap of exceptions.
190 BitWriter bs(out, 1);
191 for (unsigned i = 0; i < num; ++i) {
192 bs.write((in[i] >> bit_width) != 0);
194 out += bytes_for_packed_bits(num, 1);
199 BitWriter bs(out, exception_bit_width);
200 unsigned num_exceptions = 0;
201 for (unsigned i = 0; i < num; ++i) {
202 if ((in[i] >> bit_width) != 0) {
203 bs.write(in[i] >> bit_width);
207 out += bytes_for_packed_bits(num_exceptions, exception_bit_width);
211 out = encode_bitmap(in, num, bit_width, interleaved, out);
216 // See decode_pfor_vb() for the format.
217 template<class Docid>
218 unsigned char *encode_pfor_vb(const Docid *in, unsigned num, unsigned bit_width, bool interleaved, unsigned char *out)
220 unsigned num_exceptions = 0;
221 for (unsigned i = 0; i < num; ++i) {
222 if ((in[i] >> bit_width) != 0) {
226 *out++ = num_exceptions;
229 out = encode_bitmap(in, num, bit_width, interleaved, out);
232 for (unsigned i = 0; i < num; ++i) {
233 unsigned val = in[i] >> bit_width;
235 out = write_vb(val, out);
239 // Exception indexes.
240 for (unsigned i = 0; i < num; ++i) {
241 unsigned val = in[i] >> bit_width;
250 // Find out which block type would be the smallest for the given data.
251 template<class Docid>
252 BlockType decide_block_type(const Docid *in, unsigned num, unsigned *bit_width, unsigned *exception_bit_width)
254 // Check if the block is constant.
255 bool constant = true;
256 for (unsigned i = 1; i < num; ++i) {
257 if (in[i] != in[0]) {
263 *bit_width = num_bits(in[0]);
264 return BlockType::CONSTANT;
267 // Build up a histogram of bit sizes.
268 unsigned histogram[sizeof(Docid) * CHAR_BIT + 1] = { 0 };
269 unsigned max_bits = 0;
270 for (unsigned i = 0; i < num; ++i) {
271 unsigned bits = num_bits(in[i]);
273 max_bits = std::max(max_bits, bits);
277 unsigned best_cost = bytes_for_packed_bits(num, max_bits);
278 unsigned best_bit_width = max_bits;
280 // Try PFOR with bitmap exceptions.
281 const unsigned bitmap_cost = bytes_for_packed_bits(num, 1);
282 unsigned num_exceptions = 0;
283 for (unsigned exception_bit_width = 1; exception_bit_width <= max_bits; ++exception_bit_width) {
284 unsigned test_bit_width = max_bits - exception_bit_width;
285 num_exceptions += histogram[test_bit_width + 1];
287 // 1 byte for signaling exception bit width, then the bitmap,
288 // then the base values, then the exceptions.
289 unsigned cost = 1 + bitmap_cost + bytes_for_packed_bits(num, test_bit_width) +
290 bytes_for_packed_bits(num_exceptions, exception_bit_width);
291 if (cost < best_cost) {
293 best_bit_width = test_bit_width;
297 // Try PFOR with varbyte exceptions.
298 bool best_is_varbyte = false;
299 for (unsigned test_bit_width = 0; test_bit_width < max_bits; ++test_bit_width) {
300 // 1 byte for signaling number of exceptions, plus the base values,
301 // and then we count up the varbytes and indexes. (This is precise
303 unsigned cost = 1 + bytes_for_packed_bits(num, test_bit_width);
304 for (unsigned i = 0; i < num && cost < best_cost; ++i) {
305 unsigned val = in[i] >> test_bit_width;
307 // Not stored, and then also no index.
308 } else if (val <= 176) {
310 } else if (val <= 16560) {
312 } else if (val <= 540848) {
314 } else if (val <= 16777215) {
320 if (cost < best_cost) {
322 best_bit_width = test_bit_width;
323 best_is_varbyte = true;
327 // TODO: Consider the last-resort option of just raw storage (255).
329 if (best_is_varbyte) {
330 *bit_width = best_bit_width;
331 return BlockType::PFOR_VB;
332 } else if (best_bit_width == max_bits) {
333 *bit_width = max_bits;
334 return BlockType::FOR;
336 *bit_width = best_bit_width;
337 *exception_bit_width = max_bits - best_bit_width;
338 return BlockType::PFOR_BITMAP;
342 // The basic entry point. Takes one block of integers (which already must
343 // be delta-minus-1-encoded) and packs it into TurboPFor format.
344 // interleaved corresponds to the interleaved parameter in decode_pfor_delta1()
345 // or the ā128vā infix in the reference code's function names; such formats
346 // are much faster to decode, so for full blocks, you probably want it.
347 // The interleaved flag isn't stored anywhere; it's implicit whether you
348 // want to use it for full blocks or not.
350 // The first value must already be written using write_baseval() (so the delta
351 // coding starts from the second value). Returns the end of the string.
352 // May write 4 bytes past the end.
353 template<unsigned BlockSize, class Docid>
354 unsigned char *encode_pfor_single_block(const Docid *in, unsigned num, bool interleaved, unsigned char *out)
358 assert(num == BlockSize);
361 unsigned bit_width, exception_bit_width;
362 BlockType block_type = decide_block_type(in, num, &bit_width, &exception_bit_width);
363 *out++ = (block_type << 6) | bit_width;
365 switch (block_type) {
366 case BlockType::CONSTANT: {
367 unsigned bit_width = num_bits(in[0]);
368 write_le<Docid>(in[0], out);
369 return out + div_round_up(bit_width, 8);
372 return encode_for(in, num, bit_width, interleaved, out);
373 case BlockType::PFOR_BITMAP:
374 return encode_pfor_bitmap(in, num, bit_width, exception_bit_width, interleaved, out);
375 case BlockType::PFOR_VB:
376 return encode_pfor_vb(in, num, bit_width, interleaved, out);
382 #endif // !defined(_TURBOPFOR_ENCODE_H)