summaryrefslogtreecommitdiff
path: root/src/benchmarks/eina/eina_bench_hash.c
blob: 0429097c70013340d00158d7276b508d138a7551 (plain) (blame)
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
/* EINA - EFL data type library
 * Copyright (C) 2008 Cedric Bail
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#ifdef EINA_BENCH_HAVE_GLIB
# include <glib.h>
#endif

#include "Evas_Data.h"
#include "Ecore_Data.h"

#include "eina_hash.h"
#include "eina_array.h"
#include "eina_bench.h"
#include "eina_rbtree.h"
#include "eina_convert.h"

#ifdef CITYHASH_BENCH
// Hash function for a byte array.
uint64_t CityHash64(const char *buf, size_t len);

static unsigned int
_eina_string_key_length(const char *key)
{
   if (!key)
      return 0;

   return (int)strlen(key) + 1;
}

static int
_eina_string_key_cmp(const char *key1, __UNUSED__ int key1_length,
                     const char *key2, __UNUSED__ int key2_length)
{
   return strcmp(key1, key2);
}
#endif


typedef struct _Eina_Bench_Rbtree Eina_Bench_Rbtree;
struct _Eina_Bench_Rbtree
{
   Eina_Rbtree node;
   char key[10];
   int value;
};

static Eina_Rbtree_Direction
_eina_bench_rbtree_cmp(const Eina_Bench_Rbtree *left,
                       const Eina_Bench_Rbtree *right,
                       __UNUSED__ void *data)
{
   if (!left)
      return EINA_RBTREE_RIGHT;

   if (!right)
      return EINA_RBTREE_LEFT;

   return strcmp(left->key,
                 right->key) < 0 ? EINA_RBTREE_LEFT : EINA_RBTREE_RIGHT;
}

static inline int
_eina_bench_rbtree_key(const Eina_Bench_Rbtree *node,
                       const char *key,
                       int length,
                       __UNUSED__ void *data)
{
   return strncmp(node->key, key, length);
}

static void
_eina_bench_rbtree_free(Eina_Rbtree *node, __UNUSED__ void *data)
{
   free(node);
}

static void
eina_bench_lookup_rbtree(int request)
{
   Eina_Rbtree *root = NULL;
   int i;
   int j;

   for (i = 0; i < request; ++i)
     {
        Eina_Bench_Rbtree *tmp;

        tmp = malloc(sizeof (Eina_Bench_Rbtree));
        if (!tmp)
           continue;

        tmp->value = i;
        eina_convert_itoa(i, tmp->key);

        root = eina_rbtree_inline_insert(root,
                                         &tmp->node,
                                         EINA_RBTREE_CMP_NODE_CB(
                                            _eina_bench_rbtree_cmp),
                                         NULL);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < request; ++i)
        {
           Eina_Rbtree *tmp;
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);

           tmp = eina_rbtree_inline_lookup(root,
                                           tmp_key,
                                           10,
                                           EINA_RBTREE_CMP_KEY_CB(
                                              _eina_bench_rbtree_key),
                                           NULL);
           /* Suppress warnings as we really don't want to do anything. */
           (void) tmp;
        }

   eina_rbtree_delete(root, EINA_RBTREE_FREE_CB(_eina_bench_rbtree_free), NULL);
}

static void
eina_bench_lookup_murmur(int request)
{
   Eina_Hash *hash = NULL;
   int *tmp_val;
   unsigned int i;
   unsigned int j;

   hash = eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
                        EINA_KEY_CMP(_eina_string_key_cmp),
                        EINA_KEY_HASH(eina_hash_murmur3),
                        free,
                        8);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        char tmp_key[10];

        tmp_val = malloc(sizeof (int));

        if (!tmp_val)
           continue;

        eina_convert_itoa(i, tmp_key);
        *tmp_val = i;

        eina_hash_add(hash, tmp_key, tmp_val);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);
           tmp_val = eina_hash_find(hash, tmp_key);
        }

   eina_hash_free(hash);
}

#ifdef CITYHASH_BENCH
static void
eina_bench_lookup_cityhash(int request)
{
   Eina_Hash *hash = NULL;
   int *tmp_val;
   unsigned int i;
   unsigned int j;

   hash = eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
                        EINA_KEY_CMP(_eina_string_key_cmp),
                        EINA_KEY_HASH(CityHash64),
                        free,
                        8);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        char tmp_key[10];

        tmp_val = malloc(sizeof (int));

        if (!tmp_val)
           continue;

        eina_convert_itoa(i, tmp_key);
        *tmp_val = i;

        eina_hash_add(hash, tmp_key, tmp_val);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);
           tmp_val = eina_hash_find(hash, tmp_key);
        }

   eina_hash_free(hash);
}
#endif

static void
eina_bench_lookup_superfast(int request)
{
   Eina_Hash *hash = NULL;
   int *tmp_val;
   unsigned int i;
   unsigned int j;

   hash = eina_hash_string_superfast_new(free);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        char tmp_key[10];

        tmp_val = malloc(sizeof (int));

        if (!tmp_val)
           continue;

        eina_convert_itoa(i, tmp_key);
        *tmp_val = i;

        eina_hash_add(hash, tmp_key, tmp_val);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);
           tmp_val = eina_hash_find(hash, tmp_key);
        }

   eina_hash_free(hash);
}

static void
eina_bench_lookup_djb2(int request)
{
   Eina_Hash *hash = NULL;
   int *tmp_val;
   unsigned int i;
   unsigned int j;

   hash = eina_hash_string_djb2_new(free);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        char tmp_key[10];

        tmp_val = malloc(sizeof (int));

        if (!tmp_val)
           continue;

        eina_convert_itoa(i, tmp_key);
        *tmp_val = i;

        eina_hash_add(hash, tmp_key, tmp_val);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);

           tmp_val = eina_hash_find(hash, tmp_key);
        }

   eina_hash_free(hash);
}

typedef struct _Eina_Bench_DJB2 Eina_Bench_DJB2;
struct _Eina_Bench_DJB2
{
   char *key;
   int value;
};

static void
eina_bench_lookup_djb2_inline(int request)
{
   Eina_Hash *hash = NULL;
   Eina_Bench_DJB2 *elm;
   unsigned int i;
   unsigned int j;

   hash = eina_hash_string_djb2_new(free);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        int length;

        elm = malloc(sizeof (Eina_Bench_DJB2) + 10);
        if (!elm)
           continue;

        elm->key = (char *)(elm + 1);

        length = eina_convert_itoa(i, elm->key) + 1;
        elm->value = i;

        eina_hash_direct_add_by_hash(hash, elm->key, length,
                                     eina_hash_djb2(elm->key, length), elm);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];
           int length = 6;

           length = eina_convert_itoa(rand() % request, tmp_key) + 1;

           elm =
              eina_hash_find_by_hash(hash, tmp_key, length,
                                     eina_hash_djb2(tmp_key, length));
        }

   eina_hash_free(hash);
}

#ifdef EINA_BENCH_HAVE_GLIB
typedef struct _Eina_Bench_Glib Eina_Bench_Glib;
struct _Eina_Bench_Glib
{
   char *key;
   int value;
};

static void
eina_bench_lookup_ghash(int request)
{
   Eina_Bench_Glib *elm;
   GHashTable *hash;
   unsigned int i;
   unsigned int j;

   hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        elm = malloc(sizeof (Eina_Bench_Glib) + 10);
        if (!elm)
           continue;

        elm->key = (char *)(elm + 1);

        eina_convert_itoa(i, elm->key);
        elm->value = i;

        g_hash_table_insert(hash, elm->key, elm);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);

           elm = g_hash_table_lookup(hash, tmp_key);
        }

   g_hash_table_destroy(hash);
}
#endif

static void
eina_bench_lookup_evas(int request)
{
   Evas_Hash *hash = NULL;
   Eina_Array *array = NULL;
   int *tmp_val;
   Eina_Array_Iterator it;
   unsigned int i;
   unsigned int j;

   array = eina_array_new(10000);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        char tmp_key[10];

        tmp_val = malloc(sizeof (int));

        if (!tmp_val)
           continue;

        eina_convert_itoa(i, tmp_key);
        *tmp_val = i;

        hash = evas_hash_add(hash, tmp_key, tmp_val);

        eina_array_push(array, tmp_val);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);

           tmp_val = evas_hash_find(hash, tmp_key);
        }

   evas_hash_free(hash);

   EINA_ARRAY_ITER_NEXT(array, i, tmp_val, it)
     free(tmp_val);

   eina_array_free(array);
}

typedef struct _Eina_Bench_Ecore Eina_Bench_Ecore;
struct _Eina_Bench_Ecore
{
   char *key;
   int value;
};

static void
eina_bench_lookup_ecore(int request)
{
   Ecore_Hash *hash = NULL;
   Eina_Bench_Ecore *elm;
   unsigned int i;
   unsigned int j;

   hash = ecore_hash_new(ecore_str_hash, ecore_str_compare);

   ecore_hash_free_key_cb_set(hash, NULL);
   ecore_hash_free_value_cb_set(hash, free);

   for (i = 0; i < (unsigned int)request; ++i)
     {
        elm = malloc(sizeof (Eina_Bench_Ecore) + 10);
        if (!elm)
           continue;

        elm->key = (char *)(elm + 1);
        eina_convert_itoa(i, elm->key);
        elm->value = i;

        ecore_hash_set(hash, elm->key, elm);
     }

   srand(time(NULL));

   for (j = 0; j < 200; ++j)
      for (i = 0; i < (unsigned int)request; ++i)
        {
           char tmp_key[10];

           eina_convert_itoa(rand() % request, tmp_key);

           elm = ecore_hash_get(hash, tmp_key);
        }

   ecore_hash_destroy(hash);
}

void eina_bench_hash(Eina_Benchmark *bench)
{
   eina_benchmark_register(bench, "superfast-lookup",
                           EINA_BENCHMARK(
                              eina_bench_lookup_superfast),   10, 10000, 10);
   eina_benchmark_register(bench, "djb2-lookup",
                           EINA_BENCHMARK(
                              eina_bench_lookup_djb2),        10, 10000, 10);
   eina_benchmark_register(bench, "djb2-lookup-inline",
                           EINA_BENCHMARK(
                              eina_bench_lookup_djb2_inline), 10, 10000, 10);
   eina_benchmark_register(bench, "murmur",
                           EINA_BENCHMARK(
                              eina_bench_lookup_murmur),      10, 10000, 10);
#ifdef CITYHASH_BENCH
   eina_benchmark_register(bench, "cityhash",
                           EINA_BENCHMARK(
                              eina_bench_lookup_cityhash),    10, 10000, 10);
#endif
   eina_benchmark_register(bench, "rbtree",
                           EINA_BENCHMARK(
                              eina_bench_lookup_rbtree),      10, 10000, 10);
#ifdef EINA_BENCH_HAVE_GLIB
   eina_benchmark_register(bench, "ghash-lookup",
                           EINA_BENCHMARK(
                              eina_bench_lookup_ghash),       10, 10000, 10);
#endif
   eina_benchmark_register(bench, "evas-lookup",
                           EINA_BENCHMARK(
                              eina_bench_lookup_evas),        10, 10000, 10);
   eina_benchmark_register(bench, "ecore-lookup",
                           EINA_BENCHMARK(
                              eina_bench_lookup_ecore),       10, 10000, 10);

}