From b1fa08da897a8f98609098a8612354a11d07b099 Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Mon, 29 Jul 2013 15:34:44 +0200 Subject: [PATCH] Another history processing improvement. Don't translate file lines into history or bookmarks structs, if we have enough unique items in the processed list (history max items). So it's not necessary to convert the lines and to remove them in a second step from the list. --- src/bookmark.c | 2 +- src/history.c | 17 ++--------------- src/util.c | 21 +++++++++++++++++++-- src/util.h | 2 +- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/bookmark.c b/src/bookmark.c index dad6679..e3aa925 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -163,7 +163,7 @@ static GList *load(const char *file) { return util_file_to_unique_list( file, (Util_Content_Func)line_to_bookmark, (GCompareFunc)bookmark_comp, - (GDestroyNotify)free_bookmark + (GDestroyNotify)free_bookmark, vb.config.history_max ); } diff --git a/src/history.c b/src/history.c index 9c9e761..688f9e3 100644 --- a/src/history.c +++ b/src/history.c @@ -264,23 +264,10 @@ static const char *get_file_by_type(HistoryType type) static GList *load(const char *file) { /* read the history items from file */ - GList *list = NULL; - - list = util_file_to_unique_list( + return util_file_to_unique_list( file, (Util_Content_Func)line_to_history, (GCompareFunc)history_comp, - (GDestroyNotify)free_history + (GDestroyNotify)free_history, vb.config.history_max ); - - /* if list is too long - remove items from end (oldest entries) */ - if (vb.config.history_max < g_list_length(list)) { - while (vb.config.history_max < g_list_length(list)) { - GList *last = g_list_first(list); - g_free(last->data); - list = g_list_delete_link(list, last); - } - } - - return list; } /** diff --git a/src/util.c b/src/util.c index bed23d4..daca1dd 100644 --- a/src/util.c +++ b/src/util.c @@ -98,13 +98,26 @@ char **util_get_lines(const char *filename) return lines; } +/** + * Retrieves a list with unique items from file. + * + * @filename: file to read items from + * @func: function to parse a single line to item + * @unique_func: function to decide if two items are equal + * @free_func: function to free already converted item if this isn't unque + * @max_items: maximum number of items that are returned, use 0 for + * unlimited items + */ GList *util_file_to_unique_list(const char *filename, Util_Content_Func func, - GCompareFunc unique_func, GDestroyNotify free_func) + GCompareFunc unique_func, GDestroyNotify free_func, unsigned int max_items) { GList *gl = NULL; + /* yes, the whole file is read and wen possible don not need all the + * lines, but this is easier to implement compared to reading the file + * line wise from ent to begining */ char *line, **lines = util_get_lines(filename); void *value; - int len; + int len, num_items = 0; len = g_strv_length(lines); if (!len) { @@ -128,6 +141,10 @@ GList *util_file_to_unique_list(const char *filename, Util_Content_Func func, free_func(value); } else { gl = g_list_prepend(gl, value); + /* skip the loop if we precessed max_items unique items */ + if (++num_items >= max_items) { + break; + } } } } diff --git a/src/util.h b/src/util.h index 6b51e13..388c064 100644 --- a/src/util.h +++ b/src/util.h @@ -33,7 +33,7 @@ void util_create_file_if_not_exists(const char* filename); char* util_get_file_contents(const char* filename, gsize* length); char** util_get_lines(const char* filename); GList *util_file_to_unique_list(const char *filename, Util_Content_Func func, - GCompareFunc unique_func, GDestroyNotify free_func); + GCompareFunc unique_func, GDestroyNotify free_func, unsigned int max_items); char* util_strcasestr(const char* haystack, const char* needle); char *util_str_replace(const char* search, const char* replace, const char* string); gboolean util_create_tmp_file(const char *content, char **file); -- 2.20.1