From e83e113eae9e7cb3e09719bfc0ad68450faf3ecd Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 25 May 2009 23:25:38 +0100 Subject: [PATCH] [surface] Speed up cairo_surface_get_mime_data(). The number of mime-types attached to a surface is usually small, typically zero. Therefore it is quicker to do a strcmp() against each key in the private mime-data array than it is to intern the string (i.e. compute a hash, search the hash table, and do a final strcmp). --- src/cairo-array.c | 8 -------- src/cairo-surface.c | 33 ++++++++++++++++++--------------- src/cairoint.h | 6 ++++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/cairo-array.c b/src/cairo-array.c index 77e575ff2..fcd1246dc 100644 --- a/src/cairo-array.c +++ b/src/cairo-array.c @@ -352,14 +352,6 @@ _cairo_array_size (cairo_array_t *array) return array->size; } -/* #cairo_user_data_array_t */ - -typedef struct { - const cairo_user_data_key_t *key; - void *user_data; - cairo_destroy_func_t destroy; -} cairo_user_data_slot_t; - /** * _cairo_user_data_array_init: * @array: a #cairo_user_data_array_t diff --git a/src/cairo-surface.c b/src/cairo-surface.c index fc994b0cf..d42e1ef96 100644 --- a/src/cairo-surface.c +++ b/src/cairo-surface.c @@ -611,27 +611,30 @@ cairo_surface_get_mime_data (cairo_surface_t *surface, const unsigned char **data, unsigned int *length) { - cairo_status_t status; - cairo_mime_data_t *mime_data; + cairo_user_data_slot_t *slots; + int i, num_slots; *data = NULL; *length = 0; - if (surface->status) + if (unlikely (surface->status)) return; - status = _cairo_intern_string (&mime_type, -1); - if (unlikely (status)) { - status = _cairo_surface_set_error (surface, status); - return; + /* The number of mime-types attached to a surface is usually small, + * typically zero. Therefore it is quicker to do a strcmp() against + * each key than it is to intern the string (i.e. compute a hash, + * search the hash table, and do a final strcmp). + */ + num_slots = surface->mime_data.num_elements; + slots = _cairo_array_index (&surface->mime_data, 0); + for (i = 0; i < num_slots; i++) { + if (strcmp ((char *) slots[i].key, mime_type) == 0) { + cairo_mime_data_t *mime_data = slots[i].user_data; + + *data = mime_data->data; + *length = mime_data->length; + return; + } } - - mime_data = _cairo_user_data_array_get_data (&surface->mime_data, - (cairo_user_data_key_t *) mime_type); - if (mime_data == NULL) - return; - - *data = mime_data->data; - *length = mime_data->length; } slim_hidden_def (cairo_surface_get_mime_data); diff --git a/src/cairoint.h b/src/cairoint.h index d694ae41b..adc9f9212 100644 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -312,6 +312,12 @@ _cairo_array_num_elements (cairo_array_t *array); cairo_private int _cairo_array_size (cairo_array_t *array); +typedef struct { + const cairo_user_data_key_t *key; + void *user_data; + cairo_destroy_func_t destroy; +} cairo_user_data_slot_t; + cairo_private void _cairo_user_data_array_init (cairo_user_data_array_t *array);