diff options
author | bfredl <bjorn.linse@gmail.com> | 2024-02-19 12:00:26 +0100 |
---|---|---|
committer | bfredl <bjorn.linse@gmail.com> | 2024-02-20 11:24:15 +0100 |
commit | f2c73e9ee2bd094f21f55dc97c5ad8d2f3a51621 (patch) | |
tree | 23e28db7aa89921f2fc17d315926d6e473f099e0 /src/nvim/fold.c | |
parent | 8952a89db588db10a9dba16356f9bbd35ca5fabb (diff) | |
download | rneovim-f2c73e9ee2bd094f21f55dc97c5ad8d2f3a51621.tar.gz rneovim-f2c73e9ee2bd094f21f55dc97c5ad8d2f3a51621.tar.bz2 rneovim-f2c73e9ee2bd094f21f55dc97c5ad8d2f3a51621.zip |
refactor(api): reduce temporary allocations when replacing lines
The way ml_replace_buf is implemented makes it unfriendly for
being used in a loop: every call allocates a scratch buffer for putting
the line into the "dirty" state. This then immediately needs to be freed
as the next ml_replace_buf and/or ml_append_buf call will flush that buffer.
It's better to later pay the price of allocating the scratch buffer only if
the line is being immediately edited (likely when using the API to only
change one line) with an extra memcpy, than allocating that buffer
multiple times every time the API is called.
Of course, a separate xmalloc/xfree cycle for each time the dirty line
changes is unwanted to begin with. But fixing that is a later refactor.
Diffstat (limited to 'src/nvim/fold.c')
-rw-r--r-- | src/nvim/fold.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/nvim/fold.c b/src/nvim/fold.c index 0ef9e48e3e..c571aaf0a4 100644 --- a/src/nvim/fold.c +++ b/src/nvim/fold.c @@ -1626,7 +1626,7 @@ static void foldAddMarker(buf_T *buf, pos_T pos, const char *marker, size_t mark STRCPY(newline + line_len + (p - cms) + markerlen, p + 2); added = markerlen + strlen(cms) - 2; } - ml_replace_buf(buf, lnum, newline, false); + ml_replace_buf(buf, lnum, newline, false, false); if (added) { extmark_splice_cols(buf, (int)lnum - 1, (int)line_len, 0, (int)added, kExtmarkUndo); @@ -1690,7 +1690,7 @@ static void foldDelMarker(buf_T *buf, linenr_T lnum, char *marker, size_t marker assert(p >= line); memcpy(newline, line, (size_t)(p - line)); STRCPY(newline + (p - line), p + len); - ml_replace_buf(buf, lnum, newline, false); + ml_replace_buf(buf, lnum, newline, false, false); extmark_splice_cols(buf, (int)lnum - 1, (int)(p - line), (int)len, 0, kExtmarkUndo); } |