Skip to content

Commit f4b3641

Browse files
John Marriottchrisbra
authored andcommitted
patch 9.1.1137: ins_str() is inefficient by calling STRLEN()
Problem: ins_str() is inefficient by calling STRLLEN() Solution: refactor ins_str() to take a length argument and let all callers provide the correct length when calling ins_str() (John Marriott) closes: #16711 Signed-off-by: John Marriott <basilisk@internode.on.net> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 066a534 commit f4b3641

9 files changed

Lines changed: 46 additions & 39 deletions

File tree

src/change.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,10 +1178,9 @@ ins_char_bytes(char_u *buf, int charlen)
11781178
* Caller must have prepared for undo.
11791179
*/
11801180
void
1181-
ins_str(char_u *s)
1181+
ins_str(char_u *s, size_t slen)
11821182
{
11831183
char_u *oldp, *newp;
1184-
int newlen = (int)STRLEN(s);
11851184
int oldlen;
11861185
colnr_T col;
11871186
linenr_T lnum = curwin->w_cursor.lnum;
@@ -1193,16 +1192,16 @@ ins_str(char_u *s)
11931192
oldp = ml_get(lnum);
11941193
oldlen = (int)ml_get_len(lnum);
11951194

1196-
newp = alloc(oldlen + newlen + 1);
1195+
newp = alloc(oldlen + slen + 1);
11971196
if (newp == NULL)
11981197
return;
11991198
if (col > 0)
12001199
mch_memmove(newp, oldp, (size_t)col);
1201-
mch_memmove(newp + col, s, (size_t)newlen);
1202-
mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
1200+
mch_memmove(newp + col, s, slen);
1201+
mch_memmove(newp + col + slen, oldp + col, (size_t)(oldlen - col + 1));
12031202
ml_replace(lnum, newp, FALSE);
1204-
inserted_bytes(lnum, col, newlen);
1205-
curwin->w_cursor.col += newlen;
1203+
inserted_bytes(lnum, col, slen);
1204+
curwin->w_cursor.col += slen;
12061205
}
12071206

12081207
/*

src/edit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ insert_special(
20582058
if (stop_arrow() == FAIL)
20592059
return;
20602060
p[len - 1] = NUL;
2061-
ins_str(p);
2061+
ins_str(p, len - 1);
20622062
AppendToRedobuffLit(p, -1);
20632063
ctrlv = FALSE;
20642064
}
@@ -2275,7 +2275,7 @@ insertchar(
22752275
do_digraph(buf[i-1]); // may be the start of a digraph
22762276
#endif
22772277
buf[i] = NUL;
2278-
ins_str(buf);
2278+
ins_str(buf, i);
22792279
if (flags & INSCHAR_CTRLV)
22802280
{
22812281
redo_literal(*buf);
@@ -4300,7 +4300,7 @@ ins_bs(
43004300
ins_char(' ');
43014301
else
43024302
{
4303-
ins_str((char_u *)" ");
4303+
ins_str((char_u *)" ", 1);
43044304
if ((State & REPLACE_FLAG))
43054305
replace_push(NUL);
43064306
}
@@ -4976,7 +4976,7 @@ ins_tab(void)
49764976
ins_char(' ');
49774977
else
49784978
{
4979-
ins_str((char_u *)" ");
4979+
ins_str((char_u *)" ", 1);
49804980
if (State & REPLACE_FLAG) // no char replaced
49814981
replace_push(NUL);
49824982
}

src/gui.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5289,7 +5289,7 @@ gui_do_findrepl(
52895289

52905290
del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
52915291
FALSE, FALSE);
5292-
ins_str(repl_text);
5292+
ins_str(repl_text, STRLEN(repl_text));
52935293
}
52945294
}
52955295
else

src/indent.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,11 +1429,13 @@ change_indent(
14291429
ptr = alloc(i + 1);
14301430
if (ptr != NULL)
14311431
{
1432+
size_t ptrlen;
14321433
new_cursor_col += i;
14331434
ptr[i] = NUL;
1435+
ptrlen = i;
14341436
while (--i >= 0)
14351437
ptr[i] = ' ';
1436-
ins_str(ptr);
1438+
ins_str(ptr, ptrlen);
14371439
vim_free(ptr);
14381440
}
14391441
}

src/insexpand.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4402,7 +4402,7 @@ ins_compl_delete(void)
44024402
// In insert mode: Delete the typed part.
44034403
// In replace mode: Put the old characters back, if any.
44044404
int col = compl_col + (compl_status_adding() ? compl_length : 0);
4405-
char_u *remaining = NULL;
4405+
string_T remaining = {NULL, 0};
44064406
int orig_col;
44074407
int has_preinsert = ins_compl_preinsert_effect();
44084408
if (has_preinsert)
@@ -4415,18 +4415,18 @@ ins_compl_delete(void)
44154415
{
44164416
if (curwin->w_cursor.col < ml_get_curline_len())
44174417
{
4418-
char_u *line = ml_get_curline();
4419-
remaining = vim_strnsave(line + curwin->w_cursor.col,
4420-
(size_t)STRLEN(line + curwin->w_cursor.col));
4421-
if (remaining == NULL)
4418+
char_u *line = ml_get_cursor();
4419+
remaining.length = ml_get_cursor_len();
4420+
remaining.string = vim_strnsave(line, remaining.length);
4421+
if (remaining.string == NULL)
44224422
return;
44234423
}
44244424
while (curwin->w_cursor.lnum > compl_lnum)
44254425
{
44264426
if (ml_delete(curwin->w_cursor.lnum) == FAIL)
44274427
{
4428-
if (remaining)
4429-
VIM_CLEAR(remaining);
4428+
if (remaining.string)
4429+
vim_free(remaining.string);
44304430
return;
44314431
}
44324432
deleted_lines_mark(curwin->w_cursor.lnum, 1L);
@@ -4440,20 +4440,20 @@ ins_compl_delete(void)
44404440
{
44414441
if (stop_arrow() == FAIL)
44424442
{
4443-
if (remaining)
4444-
VIM_CLEAR(remaining);
4443+
if (remaining.string)
4444+
vim_free(remaining.string);
44454445
return;
44464446
}
44474447
backspace_until_column(col);
44484448
compl_ins_end_col = curwin->w_cursor.col;
44494449
}
44504450

4451-
if (remaining != NULL)
4451+
if (remaining.string != NULL)
44524452
{
44534453
orig_col = curwin->w_cursor.col;
4454-
ins_str(remaining);
4454+
ins_str(remaining.string, remaining.length);
44554455
curwin->w_cursor.col = orig_col;
4456-
vim_free(remaining);
4456+
vim_free(remaining.string);
44574457
}
44584458
// TODO: is this sufficient for redrawing? Redrawing everything causes
44594459
// flicker, thus we can't do that.

src/ops.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,8 +2796,6 @@ do_addsub(
27962796
linenr_T Prenum1)
27972797
{
27982798
int col;
2799-
char_u *buf1;
2800-
char_u buf2[NUMBUFLEN];
28012799
int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
28022800
static int hexupper = FALSE; // 0xABC
28032801
uvarnumber_T n;
@@ -3012,6 +3010,10 @@ do_addsub(
30123010
}
30133011
else
30143012
{
3013+
char_u *buf1;
3014+
int buf1len;
3015+
char_u buf2[NUMBUFLEN];
3016+
int buf2len;
30153017
pos_T save_pos;
30163018
int i;
30173019

@@ -3174,20 +3176,20 @@ do_addsub(
31743176
for (bit = bits; bit > 0; bit--)
31753177
if ((n >> (bit - 1)) & 0x1) break;
31763178

3177-
for (i = 0; bit > 0 && i < (NUMBUFLEN - 1); bit--)
3178-
buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3179+
for (buf2len = 0; bit > 0 && buf2len < (NUMBUFLEN - 1); bit--)
3180+
buf2[buf2len++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
31793181

3180-
buf2[i] = '\0';
3182+
buf2[buf2len] = NUL;
31813183
}
31823184
else if (pre == 0)
3183-
vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
3185+
buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
31843186
else if (pre == '0')
3185-
vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
3187+
buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
31863188
else if (pre && hexupper)
3187-
vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
3189+
buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
31883190
else
3189-
vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
3190-
length -= (int)STRLEN(buf2);
3191+
buf2len = vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
3192+
length -= buf2len;
31913193

31923194
/*
31933195
* Adjust number of zeros to the new number of digits, so the
@@ -3199,16 +3201,18 @@ do_addsub(
31993201
while (length-- > 0)
32003202
*ptr++ = '0';
32013203
*ptr = NUL;
3204+
buf1len = (int)(ptr - buf1);
32023205

3203-
STRCAT(buf1, buf2);
3206+
STRCPY(buf1 + buf1len, buf2);
3207+
buf1len += buf2len;
32043208

32053209
// Insert just after the first character to be removed, so that any
32063210
// text properties will be adjusted. Then delete the old number
32073211
// afterwards.
32083212
save_pos = curwin->w_cursor;
32093213
if (todel > 0)
32103214
inc_cursor();
3211-
ins_str(buf1); // insert the new number
3215+
ins_str(buf1, (size_t)buf1len); // insert the new number
32123216
vim_free(buf1);
32133217

32143218
// del_char() will also mark line needing displaying

src/proto/change.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void ins_bytes(char_u *p);
2323
void ins_bytes_len(char_u *p, int len);
2424
void ins_char(int c);
2525
void ins_char_bytes(char_u *buf, int charlen);
26-
void ins_str(char_u *s);
26+
void ins_str(char_u *s, size_t slen);
2727
int del_char(int fixpos);
2828
int del_chars(long count, int fixpos);
2929
int del_bytes(long count, int fixpos_arg, int use_delcombine);

src/textformat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ internal_format(
434434
// add the additional whitespace needed after the
435435
// comment leader for the numbered list.
436436
for (i = 0; i < padding; i++)
437-
ins_str((char_u *)" ");
437+
ins_str((char_u *)" ", 1);
438438
}
439439
else
440440
{

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1137,
707709
/**/
708710
1136,
709711
/**/

0 commit comments

Comments
 (0)