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