Better history lookup performance.
authorDaniel Carl <danielcarl@gmx.de>
Tue, 26 Feb 2013 22:04:06 +0000 (23:04 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Tue, 26 Feb 2013 22:04:06 +0000 (23:04 +0100)
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.

src/history.c
src/main.h

index 5bb53d5..c419c47 100644 (file)
@@ -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);
 }
index a794b12..0384050 100644 (file)
@@ -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 */