-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbenchmark.cc
2848 lines (2478 loc) · 63.4 KB
/
benchmark.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010, Andrea Mazzoleni. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Tommy benchmark program.
*
* Simply run it without any options. It will generate a set of *.lst files
* suitable for producing graphs with gnuplot or another program.
*
* This program is supposed to be also compiled with a C++ compiler,
* to be able to use the Google dense_hash_map templates.
* For this reason it contains a lot of unnecessary casting for C of void* pointers.
*/
#define __STDC_LIMIT_MACROS 1 /* needed by ck */
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#if defined(__linux)
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#endif
#if defined(__MACH__)
#include <mach/mach_time.h>
#endif
/* Judy available on in x86 */
#if defined(_WIN32)
#include <windows.h>
#if defined(_M_IX86) || defined(__i386__)
#define USE_JUDY
#endif
#else
#if defined(__i386__)
#define USE_JUDY
#endif
#endif
/* Available only in C++ */
#ifdef __cplusplus
#define USE_GOOGLEDENSEHASH
#define USE_GOOGLEBTREE
#define USE_STXBTREE
#define USE_CPPMAP
#define USE_CPPUNORDEREDMAP
#endif
/******************************************************************************/
/* data structure */
/* Tommy data structures */
/* We directly include the C file to have functions automatically */
/* expanded inline by the compiler like other implementations */
#include "tommyds/tommy.h"
#include "tommyds/tommy.c"
/* C++ Btree */
#ifdef USE_CPPMAP
#include <map>
#endif
/* C++ Hashtable */
#ifdef USE_CPPUNORDEREDMAP
#include <unordered_map>
#endif
/* Google C dense hash table */
/* http://code.google.com/p/google-sparsehash/ in the experimental/ directory */
/* Disabled by default because it's superseeded by the C++ version. */
/* Note that it has a VERY BAD performance on the "Change" test, */
/* so we disable it in the general graphs */
/* #define USE_GOOGLELIBCHASH */
#ifdef USE_GOOGLELIBCHASH
#define htonl(x) 0 /* used for serialization, not needed here */
#define ntohl(x) 0 /* used for serialization, not needed here */
#define Table(x) Dense##x /* Use google dense tables */
#include "benchmark/lib/google/libchash.c"
#endif
/* Google C++ dense hash table. */
/* http://code.google.com/p/google-sparsehash/ */
/* Note that after erasing we always call resize(0) to possibly trigger a table resize to free some space */
/* Otherwise, it would be an unfair advantage never shrinking on deletion. */
/* The shrink is triggered only sometimes, so the performance doesn't suffer to much */
#ifdef USE_GOOGLEDENSEHASH
#include <google/dense_hash_map>
#endif
/* Google BTree */
/* https://code.google.com/p/cpp-btree/ */
#ifdef USE_GOOGLEBTREE
#if defined(_MSC_VER) /* Allow compilation in MSVC 2010 */
#include <stdint.h>
typedef size_t ssize_t;
#endif
#undef min
#undef max
#include "benchmark/lib/cpp-btree/btree_map.h"
#endif
/* STX BTree */
/* http://panthema.net/2007/stx-btree/ */
#ifdef USE_STXBTREE
#include "benchmark/lib/stx/btree_map.h"
#endif
/* UTHASH */
/* http://uthash.sourceforge.net/ */
#include "benchmark/lib/uthash/uthash.h"
/* Nedtries */
/* http://www.nedprod.com/programs/portable/nedtries/ */
/* Note that I fixed a crash bug when inserting objects with 0 key */
/* This fix is now in the nedtries github */
/* https://github.com/ned14/nedtries/commit/21039696f27db4ffac70a82f89dc5d00ae74b332 */
/* We do not use the C++ implementation as it doesn't compile with gcc 4.4.3 */
#include "benchmark/lib/nedtries/nedtrie.h"
/* KHash */
/* http://attractivechaos.awardspace.com/ */
/* It has the unfair advantage on remove by never shrinking when deleting objects. */
#define inline __inline
#include "benchmark/lib/khash/khash.h"
/* RBtree */
/* http://www.canonware.com/rb/ */
/* We are using the most recent implementation rb_never/rb.h. Not rb_old/ or rb_new/ */
#define RB_COMPACT
#define ssize_t size_t
#define bool int
#define true 1
#define false 0
#include "benchmark/lib/rb/rb_newer.h"
/* Judy */
/* http://judy.sourceforge.net/ */
/* Disabled on 64 bits platform in Windows as it's not supported */
#ifdef USE_JUDY
#include "benchmark/lib/judy/Judy.h"
#endif
/* JudyArray */
/* http://code.google.com/p/judyarray/ */
/* Ensure to use the version "judy64na.c". previous ones are not not working with integers key. */
#include "benchmark/lib/judyarray/judy64na.c"
#define USE_JUDYARRAY
/* Binary Search Cube */
/* https://sites.google.com/site/binarysearchcube/ */
#include "benchmark/lib/cube/binary-search-tesseract-1.0.c"
#define USE_CUBE
/* libdynamic */
/* https://github.com/fredrikwidlund/libdynamic */
#define USE_LIBDYNAMIC
#ifdef USE_LIBDYNAMIC
#define new c_new
#include "benchmark/lib/libdynamic/map.c"
#undef new
#endif
/* Concurrency Kit Hash Set */
/* http://concurrencykit.org/ */
/* Note that it has a VERY BAD performance on the "Change" test, */
/* so we disable it in the graphs becasue makes it unreadable */
/*
$ ./tommybench -n 63095 -d ck
Tommy benchmark program.
63095 ck forward
forward, insert, ck, 193 [ns]
forward, change, ck, 3102 [ns] <<<<<
forward, hit, ck, 344 [ns]
forward, miss, ck, 3330 [ns] <<<<<
forward, remove, ck, 327 [ns]
63095 ck random
random, insert, ck, 193 [ns]
random, change, ck, 2984 [ns] <<<<<
random, hit, ck, 340 [ns]
random, miss, ck, 3261 [ns] <<<<<
random, remove, ck, 341 [ns]
OK
*/
/* #define USE_CK */
#if defined(USE_CK) && defined(__linux)
/* if you enable it, ensure to link also with the -lck option */
extern "C" {
#include <ck_hs.h>
}
#endif
/* default hash function used */
#define hash(v) tommy_inthash_u32(v)
/******************************************************************************/
/* objects */
#define PAYLOAD 16 /**< Size of payload data for objects */
struct rbt_object {
rb_node(struct rbt_object) link;
unsigned value;
char payload[PAYLOAD];
};
struct hashtable_object {
tommy_node node;
unsigned value;
char payload[PAYLOAD];
};
struct uthash_object {
UT_hash_handle hh;
unsigned value;
char payload[PAYLOAD];
};
struct khash_object {
unsigned value;
char payload[PAYLOAD];
};
struct google_object {
unsigned value;
char payload[PAYLOAD];
};
struct stx_object {
unsigned value;
char payload[PAYLOAD];
};
struct trie_object {
tommy_trie_node node;
unsigned value;
char payload[PAYLOAD];
};
struct trie_inplace_object {
tommy_trie_inplace_node node;
unsigned value;
char payload[PAYLOAD];
};
struct judy_object {
unsigned value;
char payload[PAYLOAD];
};
struct judyarray_object {
unsigned value;
char payload[PAYLOAD];
};
struct nedtrie_object {
NEDTRIE_ENTRY(nedtrie_object) link;
unsigned value;
char payload[PAYLOAD];
};
struct cpp_object {
unsigned value;
char payload[PAYLOAD];
};
struct cube_object {
unsigned value;
char payload[PAYLOAD];
};
struct libdynamic_object {
unsigned value;
char payload[PAYLOAD];
};
struct ck_object {
unsigned value;
char payload[PAYLOAD];
};
struct rbt_object* RBTREE;
struct hashtable_object* HASHTABLE;
struct hashtable_object* HASHDYN;
struct hashtable_object* HASHLIN;
struct trie_object* TRIE;
struct trie_inplace_object* TRIE_INPLACE;
struct khash_object* KHASH;
struct google_object* GOOGLE;
struct stx_object* STX;
struct uthash_object* UTHASH;
struct nedtrie_object* NEDTRIE;
struct cpp_object* CPP;
#ifdef USE_JUDY
struct judy_object* JUDY;
#endif
#ifdef USE_JUDYARRAY
struct judyarray_object* JUDYARRAY;
#endif
#ifdef USE_CUBE
struct cube_object* CUBE;
#endif
#ifdef USE_LIBDYNAMIC
/* We insert in the hashtable pointer to objects, because our objects are big */
size_t libdynamic_hash(map*, void* void_obj)
{
libdynamic_object** obj = (libdynamic_object**)void_obj;
return hash((*obj)->value);
}
int libdynamic_equal(map*, void* void_obj1, void* void_obj2)
{
libdynamic_object** obj1 = (libdynamic_object**)void_obj1;
libdynamic_object** obj2 = (libdynamic_object**)void_obj2;
return (*obj1)->value == (*obj2)->value;
}
void libdynamic_set(map*, void* void_obj1, void* void_obj2)
{
libdynamic_object** obj1 = (libdynamic_object**)void_obj1;
libdynamic_object** obj2 = (libdynamic_object**)void_obj2;
*obj1 = *obj2;
}
struct libdynamic_object* LIBDYNAMIC_RELEASE;
void libdynamic_release(map*, void* void_obj)
{
libdynamic_object** obj = (libdynamic_object**)void_obj;
LIBDYNAMIC_RELEASE = *obj;
}
struct libdynamic_object* LIBDYNAMIC;
#endif
#ifdef USE_CK
struct ck_object* CK;
static void* hs_malloc(size_t r)
{
return malloc(r);
}
static void hs_free(void* p, size_t b, bool r)
{
(void)b;
(void)r;
free(p);
}
static struct ck_malloc my_allocator = {
hs_malloc,
0,
hs_free
};
static unsigned long hs_hash(const void* void_object, unsigned long seed)
{
const struct ck_object* object = void_object;
(void)seed;
return hash(object->value);
}
static bool hs_compare(const void *previous, const void *compare)
{
const struct ck_object* object_1 = previous;
const struct ck_object* object_2 = compare;
return object_1->value == object_2->value;
}
#endif
/******************************************************************************/
/* containers */
int rbt_compare(const void* void_a, const void* void_b)
{
const struct rbt_object* a = (const struct rbt_object*)void_a;
const struct rbt_object* b = (const struct rbt_object*)void_b;
int va = a->value;
int vb = b->value;
if (va < vb)
return -1;
if (va > vb)
return 1;
return 0;
}
int tommy_hashtable_compare(const void* void_arg, const void* void_obj)
{
const unsigned* arg = (const unsigned*)void_arg;
const struct hashtable_object* obj = (const struct hashtable_object*)void_obj;
if (*arg == obj->value)
return 0;
return 1;
}
typedef rbt(struct rbt_object) rbtree_t;
rb_gen(static, rbt_, rbtree_t, struct rbt_object, link, rbt_compare)
NEDTRIE_HEAD(nedtrie_t, nedtrie_object);
static size_t nedtrie_func(const struct nedtrie_object* r)
{
return r->value;
}
NEDTRIE_GENERATE(static, nedtrie_t, nedtrie_object, link, nedtrie_func, NEDTRIE_NOBBLEZEROS(nedtrie_t));
KHASH_MAP_INIT_INT(word, struct khash_object*)
rbtree_t tree;
tommy_hashtable hashtable;
tommy_hashdyn hashdyn;
tommy_hashlin hashlin;
tommy_allocator trie_allocator;
tommy_trie trie;
tommy_trie_inplace trie_inplace;
struct uthash_object* uthash = 0;
struct nedtrie_t nedtrie;
khash_t(word)* khash;
#ifdef __cplusplus
/* use a specialized hash, otherwise the performance depends on the STL implementation used. */
class cpp_hash {
public:
unsigned operator()(unsigned key) const { return hash(key); }
};
#endif
#ifdef USE_CPPMAP
typedef std::map<unsigned, struct cpp_object*> cppmap_t;
cppmap_t* cppmap;
#endif
#ifdef USE_CPPUNORDEREDMAP
typedef std::unordered_map<unsigned, struct cpp_object*, cpp_hash> cppunorderedmap_t;
cppunorderedmap_t* cppunorderedmap;
#endif
#ifdef USE_GOOGLELIBCHASH
struct HashTable* googlelibhash;
#endif
#ifdef USE_GOOGLEDENSEHASH
typedef google::dense_hash_map<unsigned, struct google_object*, cpp_hash> googledensehash_t;
googledensehash_t* googledensehash;
#endif
#ifdef USE_GOOGLEBTREE
typedef btree::btree_map<unsigned, struct google_object*> googlebtree_t;
googlebtree_t* googlebtree;
#endif
#ifdef USE_STXBTREE
typedef stx::btree_map<unsigned, struct stx_object*> stxbtree_t;
stxbtree_t* stxbtree;
#endif
#ifdef USE_JUDY
Pvoid_t judy = 0;
#endif
#ifdef USE_JUDYARRAY
Judy* judyarray = 0;
#endif
#ifdef USE_CUBE
struct cube* cube = 0;
#endif
#ifdef USE_LIBDYNAMIC
struct map libdynamic;
struct libdynamic_object libdynamic_empty = { (unsigned)-1 };
struct libdynamic_object* libdynamic_empty_ptr = &libdynamic_empty;
#endif
#ifdef USE_CK
ck_hs_t ck;
#endif
/******************************************************************************/
/* time */
#if defined(_WIN32)
static LARGE_INTEGER win_frequency;
#endif
static void nano_init(void)
{
#if defined(_WIN32)
if (!QueryPerformanceFrequency(&win_frequency)) {
win_frequency.QuadPart = 0;
}
#endif
}
static tommy_uint64_t nano(void)
{
tommy_uint64_t ret;
#if defined(_WIN32)
LARGE_INTEGER t;
if (!QueryPerformanceCounter(&t))
return 0;
ret = (t.QuadPart / win_frequency.QuadPart) * 1000000000;
ret += (t.QuadPart % win_frequency.QuadPart) * 1000000000 / win_frequency.QuadPart;
#elif defined(__MACH__)
mach_timebase_info_data_t info;
kern_return_t r;
tommy_uint64_t t;
t = mach_absolute_time();
r = mach_timebase_info(&info);
if (r != 0) {
abort();
}
ret = (t / info.denom) * info.numer;
ret += (t % info.denom) * info.numer / info.denom;
#elif defined(__linux)
struct timespec ts;
int r;
r = clock_gettime(CLOCK_MONOTONIC, &ts);
if (r != 0) {
abort();
}
ret = ts.tv_sec * (tommy_uint64_t)1000000000 + ts.tv_nsec;
#else
struct timeval tv;
int r;
r = gettimeofday(&tv, 0);
if (r != 0) {
abort();
}
ret = tv.tv_sec * (tommy_uint64_t)1000000000 + tv.tv_usec * 1000;
#endif
return ret;
}
/******************************************************************************/
/* random */
/**
* Pseudo random number generator.
* Note that using (rand() % max) in Visual C results in totally bogus values,
* with *strong* cache effects when accessing elements in a not really random order.
* This happen because Visual C uses a simple linear congruential generator with only 32 bits.
*/
tommy_uint64_t SEED = 0;
unsigned rnd(unsigned max)
{
unsigned r;
tommy_uint64_t divider;
loop:
/* linear congruential generator from MMIX by Donald Knuth, http://en.wikipedia.org/wiki/Linear_congruential_generator */
#ifdef _MSC_VER
divider = 0xFFFFFFFFFFFFFFFF / max;
SEED = SEED * 6364136223846793005 + 1442695040888963407;
#else
divider = 0xFFFFFFFFFFFFFFFFULL / max;
SEED = SEED * 6364136223846793005LL + 1442695040888963407LL;
#endif
r = (unsigned)(SEED / divider);
/* it may happen as the divider is approximated down */
if (r >= max)
goto loop;
return r;
}
/******************************************************************************/
/* test */
#ifdef _DEBUG
#define MAX 100000
#else
#if defined(__x86_64__) || defined(_M_X64)
#define MAX 100000000
#else
#define MAX 10000000
#endif
#endif
/**
* Max number of retries.
*/
#define RETRY_MAX 7
/**
* Max total number of test for retries.
*/
#define MIN_TRY 2000000
/**
* Operations.
*/
#define OPERATION_INSERT 0
#define OPERATION_HIT 1
#define OPERATION_MISS 2
#define OPERATION_SIZE 3
#define OPERATION_CHANGE 4
#define OPERATION_REMOVE 5
#define OPERATION_MAX 6
const char* OPERATION_NAME[OPERATION_MAX] = {
"insert",
"hit",
"miss",
"size",
"change",
"remove",
};
/**
* Orders.
*/
#define ORDER_FORWARD 0
#define ORDER_RANDOM 1
#define ORDER_MAX 2
const char* ORDER_NAME[ORDER_MAX] = {
"forward",
"random",
};
/**
* Data structures.
*/
#define DATA_HASHTABLE 0
#define DATA_HASHDYN 1
#define DATA_HASHLIN 2
#define DATA_TRIE 3
#define DATA_TRIE_INPLACE 4
#define DATA_TREE 5
#define DATA_NEDTRIE 6
#define DATA_KHASH 7
#define DATA_UTHASH 8
#define DATA_JUDY 9
#define DATA_JUDYARRAY 10
#define DATA_GOOGLEDENSEHASH 11
#define DATA_GOOGLEBTREE 12
#define DATA_STXBTREE 13
#define DATA_CPPUNORDEREDMAP 14
#define DATA_CPPMAP 15
#define DATA_CUBE 16
#define DATA_LIBDYNAMIC 17
#ifdef USE_GOOGLELIBCHASH
#define DATA_GOOGLELIBCHASH 18
#endif
#ifdef USE_CK
#define DATA_CK 19
#endif
#define DATA_MAX 20
const char* DATA_NAME[DATA_MAX] = {
"tommy-hashtable",
"tommy-hashdyn",
"tommy-hashlin",
"tommy-trie",
"tommy-trie-inplace",
"rbtree",
"nedtrie",
"khash",
"uthash",
"judy",
"judyarray",
"googledensehash",
"googlebtree",
"stxbtree",
"c++unorderedmap",
"c++map",
"tesseract",
"libdynamic",
"googlelibchash",
"concurrencykit",
};
/**
* Logged data.
*/
unsigned LOG[RETRY_MAX][DATA_MAX][ORDER_MAX][OPERATION_MAX];
/**
* Time limit in nanosecond.
* We stop measuring degenerated cases after this limit.
*/
#define TIME_MAX_NS 1500
/**
* Last measure.
* Used to stop degenerated cases.
*/
unsigned LAST[DATA_MAX][ORDER_MAX];
/**
* Test state.
*/
unsigned the_retry; /**< Number of retry. */
unsigned the_data; /**< Data struture in test. */
unsigned the_operation; /**< Operation in test. */
unsigned the_order; /**< Order in test. */
unsigned the_max; /**< Number of elements in test. */
tommy_uint64_t the_time; /**< Start time of the test. */
unsigned the_start_data; /**< Saved data structure in the last START command, simply to avoid to repeat it for STOP. */
/**
* Control flow state.
*/
tommy_bool_t the_log;
/**
* If the data structure should be in the graph, even if with no data.
* It allows to have equal graphs also if in some platforms a data structure is not available.
*/
tommy_bool_t is_listed(unsigned data)
{
(void)data;
/* always have all the columns, we exclude them in the graphs */
return 1;
}
/**
* If the data structure should be measured.
*/
tommy_bool_t is_select(unsigned data)
{
return the_data == data;
}
tommy_bool_t start(unsigned data)
{
the_start_data = data;
if (!is_select(the_start_data))
return 0;
if (!the_log)
printf("%10s, %10s, %12s, ", ORDER_NAME[the_order], OPERATION_NAME[the_operation], DATA_NAME[data]);
the_time = nano();
return 1;
}
void stop(void)
{
tommy_uint64_t elapsed = nano() - the_time;
if (!is_select(the_start_data))
return;
if (!the_log) {
printf("%4u [ns]\n", (unsigned)(elapsed / the_max));
}
LOG[the_retry][the_data][the_order][the_operation] = (unsigned)(elapsed / the_max);
}
void mem(unsigned data, tommy_size_t v)
{
if (!the_log) {
printf("%10s, %10s, %12s, ", ORDER_NAME[the_order], OPERATION_NAME[the_operation], DATA_NAME[data]);
printf("%4u [byte]\n", (unsigned)(v / the_max));
}
LOG[the_retry][the_data][the_order][the_operation] = (unsigned)(v / the_max);
}
#define COND(s) if (is_select(s))
#define START(s) if (start(s)) for(i=0;i<the_max;++i)
#define STOP() stop()
#define MEM(s, v) if (is_select(s)) mem(s, v)
#define OPERATION(operation) the_operation = operation
#define ORDER(order) the_order = order
#define DATA(data) the_data = data
FILE* open(const char* mode)
{
char buf[128];
sprintf(buf, "dat_%s_%s.lst", ORDER_NAME[the_order], OPERATION_NAME[the_operation]);
return fopen(buf, mode);
}
/******************************************************************************/
/* test */
/**
* Cache clearing buffer.
*/
unsigned char CACHE[8*1024*1024];
void cache_clear(void)
{
unsigned i;
/* read & write */
for(i=0;i<sizeof(CACHE);i += 32)
CACHE[i] += 1;
#ifdef WIN32
Sleep(0);
#endif
}
/**
* Different orders used.
*/
unsigned* FORWARD;
unsigned* RAND0;
unsigned* RAND1;
void order_init(int max, int sparse)
{
int i;
FORWARD = (unsigned*)malloc(max * sizeof(unsigned));
RAND0 = (unsigned*)malloc(max * sizeof(unsigned));
RAND1 = (unsigned*)malloc(max * sizeof(unsigned));
/* generated forward orders */
if (sparse) {
for(i=0;i<max;++i) {
FORWARD[i] = 0xffffffff / max * i;
RAND0[i] = FORWARD[i];
RAND1[i] = FORWARD[i];
}
} else {
for(i=0;i<max;++i) {
FORWARD[i] = 0x80000000 + i * 2;
RAND0[i] = FORWARD[i];
RAND1[i] = FORWARD[i];
}
}
/* randomize using the Fisher-Yates shuffle, http://en.wikipedia.org/wiki/Fisher-Yates_shuffle */
for(i=max-1;i>=0;--i) {
unsigned j, t;
j = rnd(i+1);
t = RAND0[i];
RAND0[i] = RAND0[j];
RAND0[j] = t;
j = rnd(i+1);
t = RAND1[i];
RAND1[i] = RAND1[j];
RAND1[j] = t;
}
}
void order_done(void)
{
free(FORWARD);
free(RAND0);
free(RAND1);
}
void test_alloc(void)
{
COND(DATA_TREE) {
rbt_new(&tree);
RBTREE = (struct rbt_object*)malloc(sizeof(struct rbt_object) * the_max);
}
COND(DATA_HASHTABLE) {
tommy_hashtable_init(&hashtable, 2 * the_max);
HASHTABLE = (struct hashtable_object*)malloc(sizeof(struct hashtable_object) * the_max);
}
COND(DATA_HASHDYN) {
tommy_hashdyn_init(&hashdyn);
HASHDYN = (struct hashtable_object*)malloc(sizeof(struct hashtable_object) * the_max);
}
COND(DATA_HASHLIN) {
tommy_hashlin_init(&hashlin);
HASHLIN = (struct hashtable_object*)malloc(sizeof(struct hashtable_object) * the_max);
}
COND(DATA_TRIE) {
tommy_allocator_init(&trie_allocator, TOMMY_TRIE_BLOCK_SIZE, TOMMY_TRIE_BLOCK_SIZE);
tommy_trie_init(&trie, &trie_allocator);
TRIE = (struct trie_object*)malloc(sizeof(struct trie_object) * the_max);
}
COND(DATA_TRIE_INPLACE) {
tommy_trie_inplace_init(&trie_inplace);
TRIE_INPLACE = (struct trie_inplace_object*)malloc(sizeof(struct trie_inplace_object) * the_max);
}
COND(DATA_KHASH) {
KHASH = (struct khash_object*)malloc(sizeof(struct khash_object) * the_max);
khash = kh_init(word);
}
#ifdef USE_GOOGLELIBCHASH
COND(DATA_GOOGLELIBCHASH) {
GOOGLE = (struct google_object*)malloc(sizeof(struct google_object) * the_max);
googlelibhash = AllocateHashTable(sizeof(void*), 0);
}
#endif
#ifdef USE_GOOGLEDENSEHASH
COND(DATA_GOOGLEDENSEHASH) {
GOOGLE = (struct google_object*)malloc(sizeof(struct google_object) * the_max);
googledensehash = new googledensehash_t;
googledensehash->set_empty_key(-1);
googledensehash->set_deleted_key(-2);
}
#endif
#ifdef USE_GOOGLEBTREE
COND(DATA_GOOGLEBTREE) {
GOOGLE = (struct google_object*)malloc(sizeof(struct google_object) * the_max);
googlebtree = new googlebtree_t;
}
#endif
#ifdef USE_STXBTREE
COND(DATA_STXBTREE) {
STX = (struct stx_object*)malloc(sizeof(struct stx_object) * the_max);
stxbtree = new stxbtree_t;
}
#endif
#ifdef USE_CPPMAP
COND(DATA_CPPMAP) {
CPP = (struct cpp_object*)malloc(sizeof(struct cpp_object) * the_max);
cppmap = new cppmap_t;
}
#endif
#ifdef USE_CPPUNORDEREDMAP
COND(DATA_CPPUNORDEREDMAP) {
CPP = (struct cpp_object*)malloc(sizeof(struct cpp_object) * the_max);
cppunorderedmap = new cppunorderedmap_t;
}
#endif
COND(DATA_UTHASH) {
UTHASH = (struct uthash_object*)malloc(sizeof(struct uthash_object) * the_max);
}
COND(DATA_NEDTRIE) {
NEDTRIE_INIT(&nedtrie);
NEDTRIE = (struct nedtrie_object*)malloc(sizeof(struct nedtrie_object) * the_max);
}
#ifdef USE_JUDY
COND(DATA_JUDY) {
JUDY = (struct judy_object*)malloc(sizeof(struct judy_object) * the_max);
}
#endif
#ifdef USE_JUDYARRAY
COND(DATA_JUDYARRAY) {
JUDYARRAY = (struct judyarray_object*)malloc(sizeof(struct judyarray_object) * the_max);
judyarray = (Judy*)judy_open(1024, 1);
}
#endif