From 521d571992e580f9b87449c5f19bc72818cbb8e1 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Fri, 17 Jul 2020 06:30:23 -0400 Subject: vim-patch:8.0.1774: reading very long lines can be slow 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. https://github.com/vim/vim/commit/13d3b05ed2cf9a54b18b4e8236f0af2c5386200c --- src/nvim/fileio.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 20f0cdccc3..f922591d0b 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -300,6 +300,7 @@ readfile( int skip_read = false; context_sha256_T sha_ctx; int read_undo_file = false; + int split = 0; // number of split lines linenr_T linecnt; int error = FALSE; /* errors encountered */ int ff_error = EOL_UNKNOWN; /* file format with errors */ @@ -1013,8 +1014,21 @@ retry: */ { if (!skip_read) { - 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; + } + } + // Protect against the argument of lalloc() going negative. + if (size < 0 || size + linerest + 1 < 0 || linerest >= MAXCOL) { + split++; + *ptr = NL; // split line by inserting a NL + size = 1; + } else if (!skip_read) { for (; size >= 10; size /= 2) { new_buffer = verbose_try_malloc((size_t)size + (size_t)linerest + 1); if (new_buffer) { @@ -1862,6 +1876,10 @@ failed: STRCAT(IObuff, _("[CR missing]")); c = TRUE; } + if (split) { + STRCAT(IObuff, _("[long lines split]")); + c = true; + } if (notconverted) { STRCAT(IObuff, _("[NOT converted]")); c = TRUE; -- cgit