]> git.sesse.net Git - plocate/blob - bench.cpp
clang-format again.
[plocate] / bench.cpp
1 #include <chrono>
2 #include <fcntl.h>
3 #include <memory>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #define dprintf(...)
8 //#define dprintf(...) fprintf(stderr, __VA_ARGS__);
9
10 #include "db.h"
11 #include "io_uring_engine.h"
12 #include "turbopfor-encode.h"
13 #include "turbopfor.h"
14 #include "vp4.h"
15
16 using namespace std;
17 using namespace std::chrono;
18
19 int main(void)
20 {
21         int fd = open("plocate.db", O_RDONLY);
22         if (fd == -1) {
23                 perror("plocate.db");
24                 exit(1);
25         }
26
27         Header hdr;
28         complete_pread(fd, &hdr, sizeof(hdr), /*offset=*/0);
29
30         unique_ptr<Trigram[]> ht(new Trigram[hdr.hashtable_size + hdr.extra_ht_slots + 1]);
31         complete_pread(fd, ht.get(), (hdr.hashtable_size + hdr.extra_ht_slots + 1) * sizeof(Trigram), hdr.hash_table_offset_bytes);
32
33         size_t posting_list_bytes = 0, own_posting_list_bytes = 0, total_elements = 0, most_bytes_pl = 0;
34         uint32_t longest_pl = 0;
35         vector<pair<string, unsigned>> posting_lists;
36         for (unsigned i = 0; i < hdr.hashtable_size + hdr.extra_ht_slots; ++i) {
37                 if (ht[i].num_docids == 0) {
38                         continue;
39                 }
40                 size_t len = ht[i + 1].offset - ht[i].offset;
41                 string str;
42                 str.resize(len);
43                 complete_pread(fd, &str[0], len, ht[i].offset);
44                 posting_lists.emplace_back(move(str), ht[i].num_docids);
45                 longest_pl = std::max(ht[i].num_docids, longest_pl);
46                 most_bytes_pl = std::max(len, most_bytes_pl);
47                 posting_list_bytes += len;
48                 total_elements += ht[i].num_docids;
49         }
50         ht.reset();
51         fprintf(stderr, "Read %zu posting lists.\n", posting_lists.size());
52
53         string encoded_pl;
54         encoded_pl.resize(longest_pl * 2 + 16384);  // Lots of margin.
55
56         size_t num_decode_errors = 0, num_encode_errors = 0;
57         for (auto &[pl, num_docids] : posting_lists) {
58                 //fprintf(stderr, "%zu bytes, %u docids\n", pl.size(), num_docids);
59                 vector<uint32_t> out1, out2;
60                 out1.resize(num_docids + 128);
61                 out2.resize(num_docids + 128);
62                 unsigned char *pldata = reinterpret_cast<unsigned char *>(&pl[0]);
63                 p4nd1dec128v32(pldata, num_docids, &out1[0]);
64                 decode_pfor_delta1<128>(pldata, num_docids, /*interleaved=*/true, &out2[0]);
65                 for (unsigned i = 0; i < num_docids; ++i) {
66                         if (out1[i] != out2[i]) {
67                                 if (++num_decode_errors < 10) {
68                                         fprintf(stderr, "Decode error:\n");
69                                         for (unsigned j = 0; j < num_docids; ++j) {
70                                                 fprintf(stderr, "%3u: reference=%u ours=%u  (diff=%d)\n", j, out1[j], out2[j], out1[j] - out2[j]);
71                                         }
72                                 }
73                                 break;
74                         }
75                 }
76
77                 // Test encoding, by encoding with out own implementation
78                 // and checking that decoding with the reference gives
79                 // the same result. We do not measure performance (we're slow).
80                 uint32_t deltas[128];
81                 unsigned char *ptr = reinterpret_cast<unsigned char *>(&encoded_pl[0]);
82                 ptr = write_baseval(out1[0], ptr);
83                 for (unsigned i = 1; i < num_docids; i += 128) {
84                         unsigned num_docids_this_block = std::min(num_docids - i, 128u);
85                         for (unsigned j = 0; j < num_docids_this_block; ++j) {
86                                 deltas[j] = out1[i + j] - out1[i + j - 1] - 1;
87                         }
88                         bool interleaved = (num_docids_this_block == 128);
89                         ptr = encode_pfor_single_block<128>(deltas, num_docids_this_block, interleaved, ptr);
90                 }
91                 own_posting_list_bytes += ptr - reinterpret_cast<unsigned char *>(&encoded_pl[0]);
92
93                 pldata = reinterpret_cast<unsigned char *>(&encoded_pl[0]);
94                 p4nd1dec128v32(pldata, num_docids, &out2[0]);
95                 for (unsigned i = 0; i < num_docids; ++i) {
96                         if (out1[i] != out2[i]) {
97                                 if (++num_encode_errors < 10) {
98                                         fprintf(stderr, "Encode error:\n");
99                                         for (unsigned j = 0; j < num_docids; ++j) {
100                                                 fprintf(stderr, "%3u: reference=%u ours=%u  (diff=%d)\n", j, out1[j], out2[j], out1[j] - out2[j]);
101                                         }
102                                 }
103                                 break;
104                         }
105                 }
106         }
107         fprintf(stderr, "%zu/%zu posting lists had errors in decoding.\n", num_decode_errors, posting_lists.size());
108         fprintf(stderr, "%zu/%zu posting lists had errors in encoding.\n", num_encode_errors, posting_lists.size());
109
110         // Benchmark.
111         vector<uint32_t> dummy;
112         dummy.resize(longest_pl + 128);
113         steady_clock::time_point start = steady_clock::now();
114         for (auto &[pl, num_docids] : posting_lists) {
115                 unsigned char *pldata = reinterpret_cast<unsigned char *>(&pl[0]);
116                 p4nd1dec128v32(pldata, num_docids, &dummy[0]);
117         }
118         steady_clock::time_point end = steady_clock::now();
119         double reference_sec = duration<double>(end - start).count();
120         fprintf(stderr, "Decoding with reference implementation: %.1f ms\n", 1e3 * reference_sec);
121
122         start = steady_clock::now();
123         for (auto &[pl, num_docids] : posting_lists) {
124                 unsigned char *pldata = reinterpret_cast<unsigned char *>(&pl[0]);
125                 decode_pfor_delta1<128>(pldata, num_docids, /*interleaved=*/true, &dummy[0]);
126         }
127         end = steady_clock::now();
128         double own_sec = duration<double>(end - start).count();
129         fprintf(stderr, "Decoding with own implementation: %.3f ms (%.2f%% speed)\n", 1e3 * own_sec, 100.0 * reference_sec / own_sec);
130         fprintf(stderr, "Size with own implementation: %.1f MB (%.2f%% of reference, %+d bytes)\n", own_posting_list_bytes / 1048576.0, 100.0 * own_posting_list_bytes / posting_list_bytes, int(own_posting_list_bytes) - int(posting_list_bytes));
131
132         // Three numbers giving rules of thumb for judging our own implementation:
133         //
134         // - Compressed speed is easy to compare to disk I/O, to see the relative importance
135         // - Uncompressed speed is easy to compare to intersection speeds and memory bandwidth
136         //   (also very roughly comparable to the benchmark numbers in the TurboPFor README)
137         // - ns/element gives an absolute measure for plocate (e.g. if we can decompress at
138         //   1 ns/element, a 10k-element posting list goes by in 0.01 ms, which is way beyond
139         //   instantaneous in practice).
140         fprintf(stderr, "%.1f MB/sec (compressed), %.1f MB/sec (uncompressed), %.1f ns/element\n", posting_list_bytes / own_sec / 1048576.0,
141                 (total_elements * sizeof(uint32_t)) / own_sec / 1048576.0, 1e9 * own_sec / total_elements);
142 }