1 /* Copyright (C) 2007 Josh MacDonald */
19 uint32_t good_32bit_values[] = {
21 741103597U, 887987685U,
25 uint64_t good_64bit_values[] = {
26 1181783497276652981ULL, 4292484099903637661ULL,
27 7664345821815920749ULL, // ...
31 struct false_type { };
33 template <typename Word>
37 int bitsof<uint32_t>() {
42 int bitsof<uint64_t>() {
47 int operator()(const uint8_t &c) {
52 template <typename Word>
53 struct hhash { // take "h" of the high-bits as a hash value for this
54 // checksum, which are the most "distant" in terms of the
55 // spectral test for the rabin_karp MLCG. For short windows,
56 // the high bits aren't enough, XOR "mask" worth of these in.
57 Word operator()(const Word& t, const int &h, const int &mask) {
58 return (t >> h) ^ (t & mask);
62 template <typename Word>
66 uint32_t good_word<uint32_t>() {
67 return good_32bit_values[0];
71 uint64_t good_word<uint64_t>() {
72 return good_64bit_values[0];
77 #define SELF Word, CksumSize, CksumSkip, Permute, Hash, Compaction
78 #define MEMBER template <typename Word, \
87 typedef Word word_type;
88 typedef Permute permute_type;
89 typedef Hash hash_type;
91 enum { cksum_size = CksumSize,
92 cksum_skip = CksumSkip,
93 compaction = Compaction,
100 typedef Word word_type;
101 typedef Permute permute_type;
102 typedef Hash hash_type;
104 enum { cksum_size = CksumSize,
105 cksum_skip = CksumSkip,
106 compaction = Compaction,
109 // (a^cksum_size-1 c_0) + (a^cksum_size-2 c_1) ...
111 multiplier = good_word<Word>();
112 powers = new Word[cksum_size];
113 powers[cksum_size - 1] = 1;
114 for (int i = cksum_size - 2; i >= 0; i--) {
115 powers[i] = powers[i + 1] * multiplier;
117 product = powers[0] * multiplier;
124 Word step(const uint8_t *ptr) {
126 for (int i = 0; i < cksum_size; i++) {
127 h += permute_type()(ptr[i]) * powers[i];
132 Word state0(const uint8_t *ptr) {
133 incr_state = step(ptr);
137 Word incr(const uint8_t *ptr) {
138 incr_state = multiplier * incr_state -
139 product * permute_type()(ptr[-1]) +
140 permute_type()(ptr[cksum_size - 1]);
151 struct adler32_cksum {
152 typedef Word word_type;
153 typedef Permute permute_type;
154 typedef Hash hash_type;
156 enum { cksum_size = CksumSize,
157 cksum_skip = CksumSkip,
158 compaction = Compaction,
161 Word step(const uint8_t *ptr) {
162 return xd3_lcksum (ptr, cksum_size);
165 Word state0(const uint8_t *ptr) {
166 incr_state = step(ptr);
170 Word incr(const uint8_t *ptr) {
171 incr_state = xd3_large_cksum_update (incr_state, ptr - 1, cksum_size);
180 template <typename Word>
182 typedef list<const uint8_t*> ptr_list;
183 typedef Word word_type;
184 typedef map<word_type, ptr_list> table_type;
185 typedef typename table_type::iterator table_iterator;
186 typedef typename ptr_list::iterator ptr_iterator;
195 file_stats(int size, int skip)
210 void update(const word_type &word, const uint8_t *ptr) {
211 table_iterator t_i = table.find(word);
215 if (t_i == table.end()) {
216 table.insert(make_pair(word, ptr_list()));
219 ptr_list &pl = table[word];
221 for (ptr_iterator p_i = pl.begin();
224 if (memcmp(*p_i, ptr, cksum_size) == 0) {
234 unique_values = table.size();
239 struct test_result_base;
241 static vector<test_result_base*> all_tests;
243 struct test_result_base {
244 virtual ~test_result_base() {
246 virtual void reset() = 0;
247 virtual void print() = 0;
248 virtual void get(const uint8_t* buf, const int buf_size, int iters) = 0;
249 virtual void stat() = 0;
250 virtual int count() = 0;
251 virtual int dups() = 0;
252 virtual double uniqueness() = 0;
253 virtual double fullness() = 0;
254 virtual double collisions() = 0;
255 virtual double coverage() = 0;
256 virtual double compression() = 0;
257 virtual double time() = 0;
258 virtual double score() = 0;
259 virtual void set_score(double min_dups_frac, double min_time) = 0;
260 virtual double total_time() = 0;
261 virtual int total_count() = 0;
262 virtual int total_dups() = 0;
266 bool operator()(test_result_base *a,
267 test_result_base *b) {
268 return a->score() < b->score();
273 struct test_result : public test_result_base {
274 typedef Word word_type;
275 typedef Permute permute_type;
276 typedef Hash hash_type;
278 enum { cksum_size = CksumSize,
279 cksum_skip = CksumSkip,
280 compaction = Compaction,
283 const char *test_name;
284 file_stats<Word> fstats;
298 // These are not reset
305 test_result(const char *name)
307 fstats(cksum_size, cksum_skip),
316 all_tests.push_back(this);
331 // four values used by new_table()/summarize_table()
351 if (cksum_skip == 1) {
359 return fstats.count - fstats.unique;
363 return fstats.unique - fstats.unique_values;
366 double uniqueness() {
367 return 1.0 - (double) dups() / count();
371 return (double) h_buckets_full / (1 << h_bits);
374 double collisions() {
375 return (double) colls() / fstats.unique;
379 return (double) h_buckets_full / uniqueness() / count();
382 double compression() {
383 return 1.0 - coverage();
387 return (double) accum_millis / accum_iters;
394 void set_score(double min_compression, double min_time) {
395 h_score = (compression() - 0.99 * min_compression)
396 * (time() - 0.99 * min_time);
399 double total_time() {
416 accum_time += time();
417 accum_count += count();
418 accum_dups += dups();
419 accum_colls += colls();
420 accum_size += test_size;
424 if (fstats.count != count()) {
425 fprintf(stderr, "internal error: %d != %d\n", fstats.count, count());
428 printf("%s: (%u#%u) count %u uniq %0.2f%% full %u (%0.4f%% coll %0.4f%%) covers %0.2f%% w/ 2^%d @ %.4f MB/s %u iters\n",
433 100.0 * uniqueness(),
436 100.0 * collisions(),
439 0.001 * accum_iters * test_size / accum_millis,
443 int size_log2 (int slots)
445 int bits = bitsof<word_type>() - 1;
448 for (i = 3; i <= bits; i += 1) {
449 if (slots <= (1 << i)) {
450 return i - compaction;
457 void new_table(int entries) {
459 h_bits = size_log2(entries);
463 s_bits = bitsof<word_type>() - h_bits;
466 hash_table = new char[n / 8];
467 memset(hash_table, 0, n / 8);
470 int get_table_bit(int i) {
471 return hash_table[i/8] & (1 << i%8);
474 int set_table_bit(int i) {
475 return hash_table[i/8] |= (1 << i%8);
478 void summarize_table() {
481 for (int i = 0; i < n; i++) {
482 if (get_table_bit(i)) {
489 void get(const uint8_t* buf, const int buf_size, int test_iters) {
490 rabin_karp<SELF> test;
491 //adler32_cksum<SELF> test;
499 test_size = buf_size;
500 last_offset = buf_size - cksum_size;
502 if (last_offset < 0) {
508 periods = last_offset / cksum_skip;
509 n_steps = periods + 1;
510 n_incrs = last_offset + 1;
511 stop = last_offset - (periods + 1) * cksum_skip;
514 // Compute file stats once.
515 if (fstats.unique_values == 0) {
516 if (cksum_skip == 1) {
517 for (int i = 0; i <= buf_size - cksum_size; i++) {
518 fstats.update(hash(test.step(buf + i), s_bits, s_mask), buf + i);
521 ptr = buf + last_offset;
524 for (; ptr != end; ptr -= cksum_skip) {
525 fstats.update(hash(test.step(ptr), s_bits, s_mask), ptr);
531 long start_test = get_millisecs_now();
533 if (cksum_skip != 1) {
536 for (int i = 0; i < test_iters; i++) {
537 ptr = buf + last_offset;
540 for (; ptr != end; ptr -= cksum_skip) {
541 set_table_bit(hash(test.step(ptr), s_bits, s_mask));
548 stop = buf_size - cksum_size + 1;
553 if (cksum_skip == 1) {
557 for (int i = 0; i < test_iters; i++) {
562 set_table_bit(hash(test.state0(ptr++), s_bits, s_mask));
565 for (; ptr != end; ptr++) {
566 Word w = test.incr(ptr);
567 assert(w == test.step(ptr));
568 set_table_bit(hash(w, s_bits, s_mask));
575 accum_iters += test_iters;
576 accum_millis += get_millisecs_now() - start_test;
580 template <typename Word>
581 void print_array(const char *tname) {
582 printf("static const %s hash_multiplier[64] = {\n", tname);
584 for (int i = 0; i < 64; i++) {
585 printf(" %uU,\n", p);
586 p *= good_word<Word>();
588 printf("};\n", tname);
591 int main(int argc, char** argv) {
598 fprintf(stderr, "usage: %s file ...\n", argv[0]);
602 //print_array<uint32_t>("uint32_t");
604 #define TEST(T,Z,S,P,H,C) test_result<T,Z,S,P,H<T>,C> \
605 _ ## T ## _ ## Z ## _ ## S ## _ ## P ## _ ## H ## _ ## C \
606 (#T "_" #Z "_" #S "_" #P "_" #H "_" #C)
610 TEST(uint32_t, 4, SKIP, plain, hhash, 0); /* x */ \
611 TEST(uint32_t, 4, SKIP, plain, hhash, 1); /* x */ \
612 TEST(uint32_t, 4, SKIP, plain, hhash, 2); /* x */ \
613 TEST(uint32_t, 4, SKIP, plain, hhash, 3); /* x */ \
617 #define TESTS(SKIP) \
618 TEST(uint32_t, 9, SKIP, plain, hhash, 0); /* x */ \
619 TEST(uint32_t, 9, SKIP, plain, hhash, 1); /* x */ \
620 TEST(uint32_t, 9, SKIP, plain, hhash, 2); /* x */ \
621 TEST(uint32_t, 9, SKIP, plain, hhash, 3)
623 #define TESTS_ALL(SKIP) \
624 TEST(uint32_t, 3, SKIP, plain, hhash, 0); \
625 TEST(uint32_t, 3, SKIP, plain, hhash, 1); \
626 TEST(uint32_t, 4, SKIP, plain, hhash, 0); /* x */ \
627 TEST(uint32_t, 4, SKIP, plain, hhash, 1); /* x */ \
628 TEST(uint32_t, 4, SKIP, plain, hhash, 2); /* x */ \
629 TEST(uint32_t, 4, SKIP, plain, hhash, 3); /* x */ \
630 TEST(uint32_t, 5, SKIP, plain, hhash, 0); \
631 TEST(uint32_t, 5, SKIP, plain, hhash, 1); \
632 TEST(uint32_t, 8, SKIP, plain, hhash, 0); \
633 TEST(uint32_t, 8, SKIP, plain, hhash, 1); \
634 TEST(uint32_t, 9, SKIP, plain, hhash, 0); /* x */ \
635 TEST(uint32_t, 9, SKIP, plain, hhash, 1); /* x */ \
636 TEST(uint32_t, 9, SKIP, plain, hhash, 2); /* x */ \
637 TEST(uint32_t, 9, SKIP, plain, hhash, 3); /* x */ \
638 TEST(uint32_t, 11, SKIP, plain, hhash, 0); /* x */ \
639 TEST(uint32_t, 11, SKIP, plain, hhash, 1); /* x */ \
640 TEST(uint32_t, 13, SKIP, plain, hhash, 0); \
641 TEST(uint32_t, 13, SKIP, plain, hhash, 1); \
642 TEST(uint32_t, 15, SKIP, plain, hhash, 0); /* x */ \
643 TEST(uint32_t, 15, SKIP, plain, hhash, 1); /* x */ \
644 TEST(uint32_t, 16, SKIP, plain, hhash, 0); /* x */ \
645 TEST(uint32_t, 16, SKIP, plain, hhash, 1); /* x */ \
646 TEST(uint32_t, 21, SKIP, plain, hhash, 0); \
647 TEST(uint32_t, 21, SKIP, plain, hhash, 1); \
648 TEST(uint32_t, 34, SKIP, plain, hhash, 0); \
649 TEST(uint32_t, 34, SKIP, plain, hhash, 1); \
650 TEST(uint32_t, 55, SKIP, plain, hhash, 0); \
651 TEST(uint32_t, 55, SKIP, plain, hhash, 1)
668 for (i = 1; i < argc; i++) {
669 if ((ret = read_whole_file(argv[i],
675 fprintf(stderr, "file %s is %zu bytes\n",
678 double min_time = -1.0;
679 double min_compression = 0.0;
681 for (vector<test_result_base*>::iterator i = all_tests.begin();
682 i != all_tests.end(); ++i) {
683 test_result_base *test = *i;
687 long start_test = get_millisecs_now();
690 test->get(buf, buf_len, iters);
693 } while (get_millisecs_now() - start_test < 2000);
697 if (min_time < 0.0) {
698 min_compression = test->compression();
699 min_time = test->time();
702 if (min_time > test->time()) {
703 min_time = test->time();
706 if (min_compression > test->compression()) {
707 min_compression = test->compression();
713 // for (vector<test_result_base*>::iterator i = all_tests.begin();
714 // i != all_tests.end(); ++i) {
715 // test_result_base *test = *i;
716 // test->set_score(min_compression, min_time);
719 // sort(all_tests.begin(), all_tests.end(), compare_h());
721 // for (vector<test_result_base*>::iterator i = all_tests.begin();
722 // i != all_tests.end(); ++i) {
723 // test_result_base *test = *i;