From f9468fa987413756acd3beec8828b15c059b8954 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 14 Oct 2021 09:47:07 +0200 Subject: [PATCH] history: process file faster by not filtering out hypothetical duplicates When the history file has been created by nano, it will not contain any duplicate search or replace strings, nor duplicate commands, so checking for such a duplicate for each read item was a waste of time. And if the user edited the history file and created duplicates, who are we to filter them out? They will not cause the history mechanism to malfunction; they just take a little extra memory. --- src/browser.c | 2 +- src/definitions.h | 3 +++ src/files.c | 2 +- src/history.c | 13 +++++++------ src/prototypes.h | 2 +- src/search.c | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/browser.c b/src/browser.c index e07d5bf2..44846a4c 100644 --- a/src/browser.c +++ b/src/browser.c @@ -355,7 +355,7 @@ void search_filename(bool forwards) if (*answer != '\0') { last_search = mallocstrcpy(last_search, answer); #ifdef ENABLE_HISTORIES - update_history(&search_history, answer); + update_history(&search_history, answer, PRUNE_DUPLICATE); #endif } diff --git a/src/definitions.h b/src/definitions.h index 52b8c1e3..916585e4 100644 --- a/src/definitions.h +++ b/src/definitions.h @@ -122,6 +122,9 @@ #define ANNOTATE TRUE #define NONOTES FALSE +#define PRUNE_DUPLICATE TRUE +#define IGNORE_DUPLICATES FALSE + #ifdef ENABLE_UTF8 /* In UTF-8 a valid character is at most four bytes long. */ #define MAXCHARLEN 4 diff --git a/src/files.c b/src/files.c index fea621f5..007bd168 100644 --- a/src/files.c +++ b/src/files.c @@ -1242,7 +1242,7 @@ void do_insertfile(bool execute) if (*answer != '\0') { execute_command(answer); #ifdef ENABLE_HISTORIES - update_history(&execute_history, answer); + update_history(&execute_history, answer, PRUNE_DUPLICATE); #endif } diff --git a/src/history.c b/src/history.c index 54181b36..a4278e4f 100644 --- a/src/history.c +++ b/src/history.c @@ -92,10 +92,10 @@ linestruct *find_in_history(const linestruct *start, const linestruct *end, /* Update a history list (the one in which item is the current position) * with a fresh string text. That is: add text, or move it to the end. */ -void update_history(linestruct **item, const char *text) +void update_history(linestruct **item, const char *text, bool avoid_duplicates) { linestruct **htop = NULL, **hbot = NULL; - linestruct *thesame; + linestruct *thesame = NULL; if (*item == search_history) { htop = &searchtop; @@ -108,11 +108,12 @@ void update_history(linestruct **item, const char *text) hbot = &executebot; } - /* See if the string is already in the history. */ - thesame = find_in_history(*hbot, *htop, text, HIGHEST_POSITIVE); + /* When requested, check if the string is already in the history. */ + if (avoid_duplicates) + thesame = find_in_history(*hbot, *htop, text, HIGHEST_POSITIVE); /* If an identical string was found, delete that item. */ - if (thesame != NULL) { + if (thesame) { linestruct *after = thesame->next; /* If the string is at the head of the list, move the head. */ @@ -279,7 +280,7 @@ void load_history(void) stanza[--read] = '\0'; if (read > 0) { recode_NUL_to_LF(stanza, read); - update_history(history, stanza); + update_history(history, stanza, IGNORE_DUPLICATES); } else if (history == &search_history) history = &replace_history; else diff --git a/src/prototypes.h b/src/prototypes.h index a6bd6400..72db13ac 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -335,7 +335,7 @@ void do_help(void); #ifdef ENABLE_HISTORIES void history_init(void); void reset_history_pointer_for(const linestruct *list); -void update_history(linestruct **item, const char *text); +void update_history(linestruct **item, const char *text, bool avoid_duplicates); #ifdef ENABLE_TABCOMP char *get_history_completion(linestruct **h, char *s, size_t len); #endif diff --git a/src/search.c b/src/search.c index 1c550b3a..94f96ac3 100644 --- a/src/search.c +++ b/src/search.c @@ -118,7 +118,7 @@ void search_init(bool replacing, bool retain_answer) if (*answer != '\0') { last_search = mallocstrcpy(last_search, answer); #ifdef ENABLE_HISTORIES - update_history(&search_history, answer); + update_history(&search_history, answer, PRUNE_DUPLICATE); #endif } @@ -706,7 +706,7 @@ void ask_for_and_do_replacements(void) #ifdef ENABLE_HISTORIES /* When not "", add the replace string to the replace history list. */ if (response == 0) - update_history(&replace_history, answer); + update_history(&replace_history, answer, PRUNE_DUPLICATE); #endif /* When cancelled, or when a function was run, get out. */