Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-0.6] fix(quickfix): avoid O(N^2) when filling from string typval #16663

Merged
merged 1 commit into from
Dec 15, 2021

Conversation

github-actions[bot]
Copy link
Contributor

Description

Backport of #16654 to release-0.6.

When filling a quickfix/loclist from a string-typed VimL variable, the
complexity is O(N^2) in the number of lines in the variable.

The problem is caused by using `xstrlcpy(3)` to copy the characters from
the current position up to the next newline into the quickfix/loclist
buffer in a loop.

strlcpy(3) returns the length of `src`, so by necessity it has to
compute `strlen(src)`. This means scanning the full rest of the typval
on every iteration while only copying a small fraction (up to the next
'\n').

This is not a problem whenever the srclen-to-copylen ratio is close to
1, which it usually is. But not in this case. Since we already
calculated exactly how many bytes we want to copy, we should be using
memcpy(3).

This problem is not present in Vim, as it uses `vim_strncpy`, a
`strncpy(3)`-alike, which stops at either `\0` or `n`, whichever comes
first.

The quickfix/loclist window can be filled using a:

  1. File (used by commands like :grep/:make/... to source directly
     from their errorfile)
  2. Buffer (used by :cbuffer and its variants)
  3. Typval
   a. String (used by :cexpr and its variants)
   b. List of strings (used by setqflist(), setloclist(), :cepxr and its
   variants)

This commit optimizes case (3a), especially when the typval is a long
string.

The pathological path is triggered by (e.g.) :grep enhancements as found
in https://gist.github.com/romainl/56f0c28ef953ffc157f36cc495947ab3:

    function! Grep(...)
        return system(join([&grepprg] + a:000), ' '))
    endfunction
    :cgetexpr Grep('foo')

It would've been better for Neovim to use `systemlist` here, before this
commit.

(cherry picked from commit ac9ba74)
@gpanders gpanders merged commit aa0ddc6 into release-0.6 Dec 15, 2021
@gpanders gpanders deleted the backport-16654-to-release-0.6 branch December 15, 2021 15:08
bfredl added a commit that referenced this pull request Dec 31, 2021
Bug Fixes

  * api: allow nvim_buf_set_extmark to accept end_row key #16686 1b54344
  * diagnostic: assert that diagnostics have line number and column #16687 9dae939
  * diagnostic: clamp diagnostics on negative line numbers #16497 096f841
  * diagnostic: escape special chars in file names #16588 beac24d
  * diagnostic: respect "if_many" source option for virtual text #16697 060eeaa
  * diagnostic: set effective buffer number for DiagnosticChanged autocmd #16485 84784a8
  * diagnostic: set effective buffer number in autocmd (again) #16590 08ddfa9, closes #16474
  * lua: do not cast offset to char_u 93f1ec0
  * lsp: avoid attaching to unloaded buffers #16726 0088994
  * lsp: call config on_exit handler before context is cleared #16781 571609f
  * lsp: fix `nil`-index behavior for UTF-8 in `_str_*index_enc` methods #16785 03bd914
  * lsp: handle offset encoding #16783 7b60ec7
  * lsp: progress handlers should return vim.NIL on error #16476 fb11ef0
  * options: disallow empty 'fdc' and 'scl' #16776 37a00be
  * quickfix: avoid O(N^2) when filling from string typval #16663 aa0ddc6
  * screenpos, float: add top and left border adjustment 8f68548
  * terminal: fix resize crash with pending scrollback #16665 ae249d8
  * ui: close floating window on BufLeave event #16664 785bace
  * uri: change scheme pattern to not include the comma character #16798 0e96f7d

Features

  * lsp,diagnostic: open folds in jump-related functions #16784 ee9e342
  * lsp: add buf_detach_client #16741 ec101b9
  * lsp: use `vim.ui.select` for selecting lsp client #16782 14357c8
  * runtime: new checkhealth filetype #16708 09306f0
@dundargoc dundargoc mentioned this pull request Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants