Skip to content

Commit

Permalink
patch 8.0.1774: reading very long lines can be slow
Browse files Browse the repository at this point in the history
Problem:    Reading very long lines can be slow.
Solution:   Read up to 1 Mbyte at a time to avoid a lot of copying.  Add a
            check for going over the column limit.
  • Loading branch information
brammool committed Apr 29, 2018
1 parent c36651b commit 13d3b05
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/fileio.c
Expand Up @@ -1209,28 +1209,42 @@ readfile(
* The amount is limited by the fact that read() only can read
* upto max_unsigned characters (and other things).
*/
#if VIM_SIZEOF_INT <= 2
if (linerest >= 0x7ff0)
if (!skip_read)
{
++split;
*ptr = NL; /* split line by inserting a NL */
size = 1;
}
else
#endif
{
if (!skip_read)
{
#if VIM_SIZEOF_INT > 2
# if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
size = SSIZE_MAX; /* use max I/O size, 52K */
# else
size = 0x10000L; /* use buffer >= 64K */
/* Use buffer >= 64K. Add linerest to double the size if the
* line gets very long, to avoid a lot of copying. But don't
* read more than 1 Mbyte at a time, so we can be interrupted.
*/
size = 0x10000L + linerest;
if (size > 0x100000L)
size = 0x100000L;
# endif
#else
size = 0x7ff0L - linerest; /* limit buffer to 32K */
#endif
}

/* Protect against the argument of lalloc() going negative. */
if (
#if VIM_SIZEOF_INT <= 2
linerest >= 0x7ff0
#else
size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL
#endif
)
{
++split;
*ptr = NL; /* split line by inserting a NL */
size = 1;
}
else
{
if (!skip_read)
{
for ( ; size >= 10; size = (long)((long_u)size >> 1))
{
if ((new_buffer = lalloc((long_u)(size + linerest + 1),
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Expand Up @@ -761,6 +761,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1774,
/**/
1773,
/**/
Expand Down

0 comments on commit 13d3b05

Please sign in to comment.