Skip to content

Commit d34f9b1

Browse files
committed
patch 8.0.0551: the typeahead buffer is reallocated too often
Problem: The typeahead buffer is reallocated too often. Solution: Re-use the existing buffer if possible.
1 parent 9585a16 commit d34f9b1

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/getchar.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ init_typebuf(void)
920920
typebuf.tb_noremap = noremapbuf_init;
921921
typebuf.tb_buflen = TYPELEN_INIT;
922922
typebuf.tb_len = 0;
923-
typebuf.tb_off = 0;
923+
typebuf.tb_off = MAXMAPLEN + 4;
924924
typebuf.tb_change_cnt = 1;
925925
}
926926
}
@@ -974,11 +974,21 @@ ins_typebuf(
974974
typebuf.tb_off -= addlen;
975975
mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
976976
}
977+
else if (typebuf.tb_len == 0 && typebuf.tb_buflen
978+
>= addlen + 3 * (MAXMAPLEN + 4))
979+
{
980+
/*
981+
* Buffer is empty and string fits in the existing buffer.
982+
* Leave some space before and after, if possible.
983+
*/
984+
typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
985+
mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
986+
}
977987
else
978988
{
979989
/*
980990
* Need to allocate a new buffer.
981-
* In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4
991+
* In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
982992
* characters. We add some extra room to avoid having to allocate too
983993
* often.
984994
*/
@@ -1291,7 +1301,7 @@ alloc_typebuf(void)
12911301
return FAIL;
12921302
}
12931303
typebuf.tb_buflen = TYPELEN_INIT;
1294-
typebuf.tb_off = 0;
1304+
typebuf.tb_off = MAXMAPLEN + 4; /* can insert without realloc */
12951305
typebuf.tb_len = 0;
12961306
typebuf.tb_maplen = 0;
12971307
typebuf.tb_silent = 0;

src/version.c

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

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
551,
767769
/**/
768770
550,
769771
/**/

0 commit comments

Comments
 (0)