From 910f142b0201ac95fa7c62bf6d1a658e16093f16 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sat, 15 Dec 2007 03:27:01 +0000 Subject: [PATCH] 2007-12-14 Jim Evins * libglabels/db.c: Optimize template search by name by using a GHashTable. * src/mini-preview-pixbuf-cache.c: * src/mini-preview-pixbuf-cache.h: Store one pixbuf per template, but use a key for each alias. * src/wdgt-media-select.c: Optimize loading of tree data, thus optimizing the "new label" dialog. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@707 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- glabels2/ChangeLog | 10 ++++ glabels2/libglabels/db.c | 73 ++++++++++++++++++++---- glabels2/src/mini-preview-pixbuf-cache.c | 38 +++++++++++- glabels2/src/mini-preview-pixbuf-cache.h | 8 ++- glabels2/src/wdgt-media-select.c | 12 +--- 5 files changed, 115 insertions(+), 26 deletions(-) diff --git a/glabels2/ChangeLog b/glabels2/ChangeLog index 82f86634..a486c9af 100644 --- a/glabels2/ChangeLog +++ b/glabels2/ChangeLog @@ -1,3 +1,13 @@ +2007-12-14 Jim Evins + + * libglabels/db.c: + Optimize template search by name by using a GHashTable. + * src/mini-preview-pixbuf-cache.c: + * src/mini-preview-pixbuf-cache.h: + Store one pixbuf per template, but use a key for each alias. + * src/wdgt-media-select.c: + Optimize loading of tree data, thus optimizing the "new label" dialog. + 2007-12-12 Jim Evins * data/templates/avery-us-templates.xml: diff --git a/glabels2/libglabels/db.c b/glabels2/libglabels/db.c index b9cb9ad6..67eb27bd 100644 --- a/glabels2/libglabels/db.c +++ b/glabels2/libglabels/db.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -56,10 +57,15 @@ static GList *papers = NULL; static GList *categories = NULL; static GList *templates = NULL; +static GHashTable *template_cache = NULL; + /*===========================================*/ /* Local function prototypes */ /*===========================================*/ +static void init_template_cache (void); +static void add_to_template_cache (lglTemplate *template); + static GList *read_papers (void); static GList *read_paper_files_from_dir (GList *papers, const gchar *dirname); @@ -72,7 +78,7 @@ static GList *read_templates (void); static GList *read_template_files_from_dir (GList *templates, const gchar *dirname); -static lglTemplate *template_full_page (const gchar *page_size); +static lglTemplate *template_full_page (const gchar *page_size); @@ -146,6 +152,7 @@ lgl_db_init (void) } lgl_db_free_paper_id_list (page_sizes); + init_template_cache (); } } @@ -1083,6 +1090,7 @@ lgl_db_register_template (const lglTemplate *template) template_copy = lgl_template_dup (template); lgl_template_add_category (template_copy, "user-defined"); templates = g_list_append (templates, template_copy); + add_to_template_cache (template_copy); return LGL_DB_REG_OK; } else @@ -1334,7 +1342,7 @@ lgl_db_free_template_name_list (GList *names) lglTemplate * lgl_db_lookup_template_from_name (const gchar *name) { - GList *p_tmplt, *p_alias; + GList *p_alias; lglTemplate *template; lglTemplateAlias *alias; gchar *candidate_name; @@ -1351,30 +1359,75 @@ lgl_db_lookup_template_from_name (const gchar *name) return lgl_template_dup ((lglTemplate *) templates->data); } - for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next) + template = g_hash_table_lookup (template_cache, name); + + if (template) { - template = (lglTemplate *) p_tmplt->data; - for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next) + for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next) { alias = (lglTemplateAlias *)p_alias->data; candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part); - if ( UTF8_EQUAL (candidate_name, name) ) { + if ( UTF8_EQUAL (candidate_name, name) ) + { g_free (candidate_name); - new_template = lgl_template_dup (template); + new_template = lgl_template_dup (template); new_template->brand = g_strdup (alias->brand); new_template->part = g_strdup (alias->part); return new_template; - } + } + g_free (candidate_name); - } - } + } + } /* No matching template has been found so return the first template */ return lgl_template_dup ((lglTemplate *) templates->data); } +static void +init_template_cache (void) +{ + GList *p_tmplt, *p_alias; + lglTemplate *template; + lglTemplateAlias *alias; + gchar *name; + + template_cache = g_hash_table_new (g_str_hash, g_str_equal); + + for ( p_tmplt=templates; p_tmplt != NULL; p_tmplt=p_tmplt->next ) + { + template = (lglTemplate *) p_tmplt->data; + + for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next ) + { + alias = (lglTemplateAlias *)p_alias->data; + name = g_strdup_printf ("%s %s", alias->brand, alias->part); + + g_hash_table_insert (template_cache, name, template); + } + } +} + + +static void +add_to_template_cache (lglTemplate *template) +{ + GList *p_alias; + lglTemplateAlias *alias; + gchar *name; + + for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next ) + { + alias = (lglTemplateAlias *)p_alias->data; + name = g_strdup_printf ("%s %s", alias->brand, alias->part); + + g_hash_table_insert (template_cache, name, template); + } +} + + static GList * read_templates (void) { diff --git a/glabels2/src/mini-preview-pixbuf-cache.c b/glabels2/src/mini-preview-pixbuf-cache.c index 5e5790f4..c7d3afa3 100644 --- a/glabels2/src/mini-preview-pixbuf-cache.c +++ b/glabels2/src/mini-preview-pixbuf-cache.c @@ -54,8 +54,9 @@ static GHashTable *mini_preview_pixbuf_cache = NULL; void gl_mini_preview_pixbuf_cache_init (void) { - GList *names = NULL; - GList *p; + GList *names = NULL; + GList *p; + lglTemplate *template; gl_debug (DEBUG_PIXBUF_CACHE, "START"); @@ -65,13 +66,44 @@ gl_mini_preview_pixbuf_cache_init (void) for ( p=names; p != NULL; p=p->next ) { gl_debug (DEBUG_PIXBUF_CACHE, "name = \"%s\"", p->data); - gl_mini_preview_pixbuf_cache_add_by_name ((gchar *)p->data); + + template = lgl_db_lookup_template_from_name (p->data); + gl_mini_preview_pixbuf_cache_add_by_template (template); + lgl_template_free (template); } lgl_db_free_template_name_list (names); gl_debug (DEBUG_PIXBUF_CACHE, "END pixbuf_cache=%p", mini_preview_pixbuf_cache); } +/*****************************************************************************/ +/* Add pixbuf to cache by template. */ +/*****************************************************************************/ +void +gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template) +{ + GdkPixbuf *pixbuf; + GList *p; + lglTemplateAlias *alias; + gchar *name; + + gl_debug (DEBUG_PIXBUF_CACHE, "START"); + + pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72); + + for ( p=template->aliases; p != NULL; p=p->next ) + { + alias = (lglTemplateAlias *)p->data; + + name = g_strdup_printf ("%s %s", alias->brand, alias->part); + g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf)); + } + + g_object_unref (pixbuf); + + gl_debug (DEBUG_PIXBUF_CACHE, "END"); +} + /*****************************************************************************/ /* Add pixbuf to cache by name. */ /*****************************************************************************/ diff --git a/glabels2/src/mini-preview-pixbuf-cache.h b/glabels2/src/mini-preview-pixbuf-cache.h index 1322a68a..092d5692 100644 --- a/glabels2/src/mini-preview-pixbuf-cache.h +++ b/glabels2/src/mini-preview-pixbuf-cache.h @@ -24,15 +24,17 @@ #ifndef __MINI_PREVIEW_PIXBUF_CACHE_H__ #define __MINI_PREVIEW_PIXBUF_CACHE_H__ +#include #include G_BEGIN_DECLS -void gl_mini_preview_pixbuf_cache_init (void); +void gl_mini_preview_pixbuf_cache_init (void); -void gl_mini_preview_pixbuf_cache_add_by_name (gchar *name); +void gl_mini_preview_pixbuf_cache_add_by_name (gchar *name); +void gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template); -GdkPixbuf *gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name); +GdkPixbuf *gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name); G_END_DECLS diff --git a/glabels2/src/wdgt-media-select.c b/glabels2/src/wdgt-media-select.c index 0c4b4764..7ddffe01 100644 --- a/glabels2/src/wdgt-media-select.c +++ b/glabels2/src/wdgt-media-select.c @@ -730,7 +730,6 @@ load_recent_list (GtkListStore *store, gchar *size; gchar *layout; gchar *description; - gchar *name; gl_debug (DEBUG_MEDIA_SELECT, "START"); @@ -745,10 +744,7 @@ load_recent_list (GtkListStore *store, gl_debug (DEBUG_MEDIA_SELECT, "p->data = \"%s\"", p->data); template = lgl_db_lookup_template_from_name (p->data); - - name = lgl_template_get_name (template); - pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (name); - g_free (name); + pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (p->data); size = get_label_size_desc (template); layout = get_layout_desc (template); @@ -814,7 +810,6 @@ load_search_all_list (GtkListStore *store, gchar *size; gchar *layout; gchar *description; - gchar *name; gl_debug (DEBUG_MEDIA_SELECT, "START"); @@ -829,10 +824,7 @@ load_search_all_list (GtkListStore *store, gl_debug (DEBUG_MEDIA_SELECT, "p->data = \"%s\"", p->data); template = lgl_db_lookup_template_from_name (p->data); - - name = lgl_template_get_name (template); - pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (name); - g_free (name); + pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (p->data); size = get_label_size_desc (template); layout = get_layout_desc (template); -- 2.39.5