2 * Copyright © 2011 Andrea Canciani
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of
9 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Red Hat, Inc. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: Andrea Canciani <ranma42@gmail.com>
26 #include "cairo-perf.h"
30 #define LIVE_ENTRIES 257
31 #define ACTIVE_FONTS (LIVE_ENTRIES - HOLDOVERS - 1)
34 * The original implementation of hash tables was very inefficient, as
35 * pointed out in https://bugs.freedesktop.org/show_bug.cgi?id=17399
37 * This benchmark tries to fill up the scaled_font_map hash table to
38 * show the O(n) behavior.
42 do_hash_table (cairo_t *cr, int width, int height, int loops)
45 * Microsoft C Compiler complains that:
46 * error C2466: cannot allocate an array of constant size 0
47 * so we add an unused element to make it happy
49 cairo_scaled_font_t *active_fonts[ACTIVE_FONTS + 1];
53 cairo_matrix_init_identity (&m);
55 /* Touch HOLDOVERS scaled fonts to fill up the holdover list. */
56 for (i = 0; i < HOLDOVERS; i++) {
57 m.yy = m.xx * (i + 1);
58 cairo_set_font_matrix (cr, &m);
59 cairo_get_scaled_font (cr);
63 * Reference some scaled fonts so that they will be kept in the
64 * scaled fonts map. We want LIVE_ENTRIES elements in the font
65 * map, but cairo keeps HOLDOVERS recently used fonts in it and we
66 * will be activating a new font in the cr context, so we just
67 * keep references to ACTIVE_FONTS fonts.
69 * Note: setting LIVE_ENTRIES == HOLDOVERS+1 means that we keep no
70 * font in active_fonts and the slowness is caused by the holdover
73 for (i = 0; i < ACTIVE_FONTS; i++) {
74 cairo_scaled_font_t *scaled_font;
76 m.yy = m.xx * (i + 1);
77 cairo_set_font_matrix (cr, &m);
79 scaled_font = cairo_get_scaled_font (cr);
80 active_fonts[i] = cairo_scaled_font_reference (scaled_font);
83 cairo_perf_timer_start ();
84 cairo_perf_set_thread_aware (cr, FALSE);
88 cairo_perf_set_thread_aware (cr, TRUE);
91 /* Generate ITER new scaled fonts per loop */
92 for (i = 0; i < ITER; i++) {
93 m.yy = m.xx * (i + 1);
94 cairo_set_font_matrix (cr, &m);
95 cairo_get_scaled_font (cr);
99 cairo_perf_timer_stop ();
101 for (i = 0; i < ACTIVE_FONTS; i++)
102 cairo_scaled_font_destroy (active_fonts[i]);
104 return cairo_perf_timer_elapsed ();
108 hash_table_enabled (cairo_perf_t *perf)
110 return cairo_perf_can_run (perf, "hash-table", NULL);
114 hash_table (cairo_perf_t *perf, cairo_t *cr, int width, int height)
116 cairo_perf_cover_sources_and_operators (perf, "hash-table",
117 do_hash_table, NULL);