patch 9.1.0798: too many strlen() calls in cmdhist.c

Problem:  too many strlen() calls in cmdhist.c
Solution: refactor code and remove strlen() calls
          (John Marriott)

closes: #15888

Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
John Marriott
2024-10-21 22:37:07 +02:00
committed by Christian Brabandt
parent 6eca04e9f1
commit 8df07d0ca3
6 changed files with 112 additions and 54 deletions

View File

@ -125,8 +125,6 @@ init_history(void)
{
int newlen; // new length of history table
histentry_T *temp;
int i;
int j;
int type;
// If size of history table changed, reallocate it
@ -159,11 +157,16 @@ init_history(void)
if (hisidx[type] < 0) // there are no entries yet
{
int i;
for (i = 0; i < newlen; ++i)
clear_hist_entry(&temp[i]);
}
else if (newlen > hislen) // array becomes bigger
{
int i;
int j;
for (i = 0; i <= hisidx[type]; ++i)
temp[i] = history[type][i];
j = i;
@ -174,13 +177,19 @@ init_history(void)
}
else // array becomes smaller or 0
{
int i;
int j;
j = hisidx[type];
for (i = newlen - 1; ; --i)
{
if (i >= 0) // copy newest entries
temp[i] = history[type][j];
else // remove older entries
{
vim_free(history[type][j].hisstr);
history[type][j].hisstrlen = 0;
}
if (--j < 0)
j = hislen - 1;
if (j == hisidx[type])
@ -200,6 +209,7 @@ clear_hist_entry(histentry_T *hisptr)
hisptr->hisnum = 0;
hisptr->viminfo = FALSE;
hisptr->hisstr = NULL;
hisptr->hisstrlen = 0;
hisptr->time_set = 0;
}
@ -218,6 +228,7 @@ in_history(
int i;
int last_i = -1;
char_u *p;
size_t len;
if (hisidx[type] < 0)
return FALSE;
@ -232,7 +243,7 @@ in_history(
p = history[type][i].hisstr;
if (STRCMP(str, p) == 0
&& !(writing && history[type][i].viminfo)
&& (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
&& (type != HIST_SEARCH || sep == p[history[type][i].hisstrlen + 1]))
{
if (!move_to_front)
return TRUE;
@ -247,6 +258,7 @@ in_history(
return FALSE;
str = history[type][i].hisstr;
len = history[type][i].hisstrlen;
while (i != hisidx[type])
{
if (++i >= hislen)
@ -257,6 +269,7 @@ in_history(
history[type][i].hisnum = ++hisnum[type];
history[type][i].viminfo = FALSE;
history[type][i].hisstr = str;
history[type][i].hisstrlen = len;
history[type][i].time_set = vim_time();
return TRUE;
}
@ -337,8 +350,13 @@ add_to_history(
// Store the separator after the NUL of the string.
hisptr->hisstr = vim_strnsave(new_entry, new_entrylen + 2);
if (hisptr->hisstr != NULL)
if (hisptr->hisstr == NULL)
hisptr->hisstrlen = 0;
else
{
hisptr->hisstr[new_entrylen + 1] = sep;
hisptr->hisstrlen = new_entrylen;
}
hisptr->hisnum = ++hisnum[histype];
hisptr->viminfo = FALSE;
@ -405,20 +423,6 @@ calc_hist_idx(int histype, int num)
return -1;
}
/*
* Get a history entry by its index.
* "histype" may be one of the HIST_ values.
*/
static char_u *
get_history_entry(int histype, int idx)
{
idx = calc_hist_idx(histype, idx);
if (idx >= 0)
return history[histype][idx].hisstr;
else
return (char_u *)"";
}
/*
* Clear all entries of a history.
* "histype" may be one of the HIST_ values.
@ -517,6 +521,7 @@ del_history_idx(int histype, int idx)
return FALSE;
idx = hisidx[histype];
vim_free(history[histype][i].hisstr);
history[histype][i].hisstrlen = 0;
// When deleting the last added search string in a mapping, reset
// last_maptick, so that the last added search string isn't deleted again.
@ -576,7 +581,6 @@ f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
{
int n;
char_u buf[NUMBUFLEN];
char_u *str;
if (in_vim9script()
@ -595,9 +599,14 @@ f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
n = del_history_idx(get_histtype(str),
(int)tv_get_number(&argvars[1]));
else
{
char_u buf[NUMBUFLEN];
// string given: remove all matching entries
n = del_history_entry(get_histtype(str),
tv_get_string_buf(&argvars[1], buf));
}
rettv->vval.v_number = n;
}
@ -607,9 +616,7 @@ f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
void
f_histget(typval_T *argvars UNUSED, typval_T *rettv)
{
int type;
int idx;
char_u *str;
char_u *str;
if (in_vim9script()
&& (check_for_string_arg(argvars, 0) == FAIL
@ -621,13 +628,21 @@ f_histget(typval_T *argvars UNUSED, typval_T *rettv)
rettv->vval.v_string = NULL;
else
{
int type;
int idx;
type = get_histtype(str);
if (argvars[1].v_type == VAR_UNKNOWN)
idx = get_history_idx(type);
else
idx = (int)tv_get_number_chk(&argvars[1], NULL);
// -1 on type error
rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
idx = calc_hist_idx(type, idx);
if (idx < 0)
rettv->vval.v_string = vim_strnsave((char_u *)"", 0);
else
rettv->vval.v_string = vim_strnsave(history[type][idx].hisstr, history[type][idx].hisstrlen);
}
rettv->v_type = VAR_STRING;
}
@ -647,10 +662,9 @@ f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
histname = tv_get_string_chk(&argvars[0]);
i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname);
if (i >= HIST_CMD && i < HIST_COUNT)
i = get_history_idx(i);
rettv->vval.v_number = get_history_idx(i);
else
i = -1;
rettv->vval.v_number = i;
rettv->vval.v_number = -1;
}
#endif // FEAT_EVAL
@ -662,17 +676,21 @@ f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
void
remove_key_from_history(void)
{
char_u *p_start;
char_u *p_end;
char_u *p;
int i;
i = hisidx[HIST_CMD];
if (i < 0)
return;
p = history[HIST_CMD][i].hisstr;
if (p == NULL)
p_start = history[HIST_CMD][i].hisstr;
if (p_start == NULL)
return;
for ( ; *p; ++p)
p_end = p_start + history[HIST_CMD][i].hisstrlen;
for (p = p_start; *p; ++p)
{
if (STRNCMP(p, "key", 3) == 0 && !SAFE_isalpha(p[3]))
{
p = vim_strchr(p + 3, '=');
@ -682,9 +700,14 @@ remove_key_from_history(void)
for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
if (p[i] == '\\' && p[i + 1])
++i;
STRMOVE(p, p + i);
mch_memmove(p, p + i, (p_end - (p + i)) + 1); // +1 for the NUL
p_end -= i; // adjust p_end for shortened string
--p;
}
}
history[HIST_CMD][i].hisstrlen = (size_t)(p_end - p_start);
}
#endif
@ -750,17 +773,16 @@ ex_history(exarg_T *eap)
for (; !got_int && histype1 <= histype2; ++histype1)
{
STRCPY(IObuff, "\n # ");
STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
vim_snprintf((char *)IObuff, IOSIZE, "\n # %s history", history_names[histype1]);
msg_puts_title((char *)IObuff);
idx = hisidx[histype1];
hist = history[histype1];
j = hisidx1;
k = hisidx2;
if (j < 0)
j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
j = (-j > hislen) ? 0 : hist[(hislen + j + idx + 1) % hislen].hisnum;
if (k < 0)
k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
k = (-k > hislen) ? 0 : hist[(hislen + k + idx + 1) % hislen].hisnum;
if (idx >= 0 && j <= k)
for (i = idx + 1; !got_int; ++i)
{
@ -770,14 +792,16 @@ ex_history(exarg_T *eap)
&& hist[i].hisnum >= j && hist[i].hisnum <= k
&& !message_filtered(hist[i].hisstr))
{
int len;
msg_putchar('\n');
sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
hist[i].hisnum);
len = vim_snprintf((char *)IObuff, IOSIZE,
"%c%6d ", i == idx ? '>' : ' ', hist[i].hisnum);
if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
(int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
trunc_string(hist[i].hisstr, IObuff + len,
(int)Columns - 10, IOSIZE - (int)len);
else
STRCAT(IObuff, hist[i].hisstr);
STRCPY(IObuff + len, hist[i].hisstr);
msg_outtrans(IObuff);
out_flush();
}