diff options
author | Riley Bruins <ribru17@hotmail.com> | 2025-01-11 15:44:07 -0800 |
---|---|---|
committer | Christian Clason <ch.clason+github@icloud.com> | 2025-01-12 16:44:24 +0100 |
commit | 3fdc4302415972eb5d98ba832372236be3d22572 (patch) | |
tree | 9993ceaf881d0c6fb77f7a39c1cca3e6fc6a3826 /test | |
parent | 40bf23adaf98dc357a59f9524a16e06f990faeaa (diff) | |
download | rneovim-3fdc4302415972eb5d98ba832372236be3d22572.tar.gz rneovim-3fdc4302415972eb5d98ba832372236be3d22572.tar.bz2 rneovim-3fdc4302415972eb5d98ba832372236be3d22572.zip |
perf(treesitter): cache queries strongly
**Problem:** Query parsing uses a weak cache which is invalidated
frequently
**Solution:** Make the cache strong, and invalidate it manually when
necessary (that is, when `rtp` is changed or `query.set()` is called)
Co-authored-by: Christian Clason <c.clason@uni-graz.at>
Diffstat (limited to 'test')
-rw-r--r-- | test/functional/treesitter/query_spec.lua | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/test/functional/treesitter/query_spec.lua b/test/functional/treesitter/query_spec.lua index 6e21ed1d99..6bab171ee8 100644 --- a/test/functional/treesitter/query_spec.lua +++ b/test/functional/treesitter/query_spec.lua @@ -86,7 +86,7 @@ void ui_refresh(void) local before = vim.api.nvim__stats().ts_query_parse_count collectgarbage('stop') for _ = 1, _n, 1 do - vim.treesitter.query.parse('c', long_query, _n) + vim.treesitter.query.parse('c', long_query) end collectgarbage('restart') collectgarbage('collect') @@ -96,8 +96,39 @@ void ui_refresh(void) end eq(1, q(1)) - -- cache is cleared by garbage collection even if valid "cquery" reference is kept around - eq(1, q(100)) + -- cache is retained even after garbage collection + eq(0, q(100)) + end) + + it('cache is cleared upon runtimepath changes, or setting query manually', function() + ---@return number + exec_lua(function() + _G.query_parse_count = _G.query_parse_count or 0 + local parse = vim.treesitter.query.parse + vim.treesitter.query.parse = function(...) + _G.query_parse_count = _G.query_parse_count + 1 + return parse(...) + end + end) + + local function q(_n) + return exec_lua(function() + for _ = 1, _n, 1 do + vim.treesitter.query.get('c', 'highlights') + end + return _G.query_parse_count + end) + end + + eq(1, q(10)) + exec_lua(function() + vim.opt.rtp:prepend('/another/dir') + end) + eq(2, q(100)) + exec_lua(function() + vim.treesitter.query.set('c', 'highlights', [[; test]]) + end) + eq(3, q(100)) end) it('supports query and iter by capture (iter_captures)', function() |