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.
This commit is contained in:
Benno Schulenberg 2021-10-14 09:47:07 +02:00
parent fb7c12f644
commit f9468fa987
6 changed files with 15 additions and 11 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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. */
/* 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

View File

@ -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

View File

@ -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. */