From 24aec99ffb9abce659146ea6ecbfb92856355855 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 8 Mar 2007 20:32:36 -0500 Subject: [PATCH] [cairo-traps] Add a cache of one trapezoid to cairo_traps_t Seems like half the time, we just need one trap. This avoids calling malloc() for those cases. --- src/cairo-traps.c | 30 +++++++++++++++++++++++------- src/cairoint.h | 2 ++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/cairo-traps.c b/src/cairo-traps.c index 2415ee6f3..04228c134 100644 --- a/src/cairo-traps.c +++ b/src/cairo-traps.c @@ -77,12 +77,12 @@ _cairo_traps_init (cairo_traps_t *traps) void _cairo_traps_fini (cairo_traps_t *traps) { - if (traps->traps_size) { + if (traps->traps != traps->traps_embedded) free (traps->traps); - traps->traps = NULL; - traps->traps_size = 0; - traps->num_traps = 0; - } + + traps->traps = NULL; + traps->traps_size = 0; + traps->num_traps = 0; } /** @@ -192,19 +192,35 @@ _cairo_traps_add_trap_from_points (cairo_traps_t *traps, cairo_fixed_t top, cair return _cairo_traps_add_trap (traps, top, bottom, &left, &right); } +/* make room for at least one more trap */ static cairo_status_t _cairo_traps_grow (cairo_traps_t *traps) { cairo_trapezoid_t *new_traps; int old_size = traps->traps_size; - int new_size = old_size ? 2 * old_size : 32; + int embedded_size = sizeof (traps->traps_embedded) / sizeof (traps->traps_embedded[0]); + int new_size = 2 * MAX (old_size, 16); + + /* we have a local buffer at traps->traps_embedded. try to fulfill the request + * from there. */ + if (old_size < embedded_size) { + traps->traps = traps->traps_embedded; + traps->traps_size = embedded_size; + return traps->status; + } assert (traps->num_traps <= traps->traps_size); if (traps->status) return traps->status; - new_traps = realloc (traps->traps, new_size * sizeof (cairo_trapezoid_t)); + if (traps->traps == traps->traps_embedded) { + new_traps = malloc (new_size * sizeof (cairo_trapezoid_t)); + if (new_traps) + memcpy (new_traps, traps->traps, old_size * sizeof (cairo_trapezoid_t)); + } else { + new_traps = realloc (traps->traps, new_size * sizeof (cairo_trapezoid_t)); + } if (new_traps == NULL) { traps->status = CAIRO_STATUS_NO_MEMORY; diff --git a/src/cairoint.h b/src/cairoint.h index c0d3a1777..38c553660 100755 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -1226,6 +1226,8 @@ typedef struct _cairo_traps { int num_traps; int traps_size; cairo_box_t extents; + + cairo_trapezoid_t traps_embedded[1]; } cairo_traps_t; #define CAIRO_FONT_SLANT_DEFAULT CAIRO_FONT_SLANT_NORMAL