From: Daniel Carl Date: Tue, 26 Feb 2013 22:04:06 +0000 (+0100) Subject: Better history lookup performance. X-Git-Url: https://git.owens.tech/projects.html/projects.html/git?a=commitdiff_plain;h=1cd10e6bbe0c1ca5f3b1329bf7897335769103d4;p=vimb.git Better history lookup performance. Don't generate the list of matching item if we step through the items. Generate the active list only one time and use it to step through the matching history items. --- diff --git a/src/history.c b/src/history.c index 5bb53d5..c419c47 100644 --- a/src/history.c +++ b/src/history.c @@ -39,37 +39,39 @@ void history_append(const char* line) const char* history_get(const int step) { const char* command; - GList* history = NULL; /* get the search prefix only on start of history search */ if (!vp.state.history_prefix) { OVERWRITE_STRING(vp.state.history_prefix, GET_TEXT()); - } - for (GList* l = core.behave.history; l; l = l->next) { - char* entry = (char*)l->data; - if (g_str_has_prefix(entry, vp.state.history_prefix)) { - history = g_list_prepend(history, entry); + /* generate new history list with the matching items */ + for (GList* l = core.behave.history; l; l = l->next) { + char* entry = (char*)l->data; + if (g_str_has_prefix(entry, vp.state.history_prefix)) { + vp.state.history_active = g_list_prepend(vp.state.history_active, entry); + } } + + vp.state.history_active = g_list_reverse(vp.state.history_active); } - const int len = g_list_length(history); + const int len = g_list_length(vp.state.history_active); if (!len) { return NULL; } - history = g_list_reverse(history); - /* if reached end/beginnen start at the opposit site of list again */ vp.state.history_pointer = (len + vp.state.history_pointer + step) % len; - command = (char*)g_list_nth_data(history, vp.state.history_pointer); + command = (char*)g_list_nth_data(vp.state.history_active, vp.state.history_pointer); return command; } void history_rewind(void) { - vp.state.history_pointer = 0; OVERWRITE_STRING(vp.state.history_prefix, NULL); + vp.state.history_pointer = 0; + /* free temporary used history list */ + g_list_free_full(vp.state.history_active, (GDestroyNotify)g_free); } diff --git a/src/main.h b/src/main.h index a794b12..0384050 100644 --- a/src/main.h +++ b/src/main.h @@ -228,8 +228,12 @@ typedef struct { SearchDirection search_dir; char* search_query; GList* downloads; + /* points to the actual shown history entry */ int history_pointer; + /* search string for that is searched in history */ char* history_prefix; + /* list holding matching items according to history_prefix */ + GList* history_active; } State; /* behaviour */