robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.9 KiB177 linesraw
1local n = require('test.functional.testnvim')()
2local t = require('test.testutil')
3local Screen = require('test.functional.ui.screen')
4local describe, it, before_each = t.describe, t.it, t.before_each
5local exec_lua = n.exec_lua
6
7describe('decor perf', function()
8  before_each(n.clear)
9
10  it('can handle long lines', function()
11    Screen.new(100, 101)
12
13    local result = exec_lua(function()
14      local ephemeral_pattern = {
15        { 0, 4, 'Comment', 11 },
16        { 0, 3, 'Keyword', 12 },
17        { 1, 2, 'Label', 12 },
18        { 0, 1, 'String', 21 },
19        { 1, 3, 'Function', 21 },
20        { 2, 10, 'Label', 8 },
21      }
22
23      local regular_pattern = {
24        { 4, 5, 'String', 12 },
25        { 1, 4, 'Function', 2 },
26      }
27
28      for _, list in ipairs({ ephemeral_pattern, regular_pattern }) do
29        for _, p in ipairs(list) do
30          p[3] = vim.api.nvim_get_hl_id_by_name(p[3])
31        end
32      end
33
34      local text = ('abcdefghijklmnopqrstuvwxyz0123'):rep(333)
35      local line_len = #text
36      vim.api.nvim_buf_set_lines(0, 0, 0, false, { text })
37
38      local ns = vim.api.nvim_create_namespace('decor_spec.lua')
39      vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
40      vim.api.nvim_win_set_cursor(0, { 1, 0 })
41
42      local ps, pe
43      local function add_pattern(pattern, ephemeral)
44        ps = vim.uv.hrtime()
45        local i = 0
46        while i < line_len - 10 do
47          for _, p in ipairs(pattern) do
48            vim.api.nvim_buf_set_extmark(0, ns, 0, i + p[1], {
49              end_row = 0,
50              end_col = i + p[2],
51              hl_group = p[3],
52              priority = p[4],
53              ephemeral = ephemeral,
54            })
55          end
56          i = i + 5
57        end
58        pe = vim.uv.hrtime()
59      end
60
61      vim.api.nvim_set_decoration_provider(ns, {
62        on_win = function()
63          return true
64        end,
65        on_line = function()
66          add_pattern(ephemeral_pattern, true)
67        end,
68      })
69
70      add_pattern(regular_pattern, false)
71
72      local total = {}
73      local provider = {}
74      for _ = 1, 100 do
75        local tic = vim.uv.hrtime()
76        vim.cmd 'redraw!'
77        local toc = vim.uv.hrtime()
78        table.insert(total, toc - tic)
79        table.insert(provider, pe - ps)
80      end
81
82      return { total, provider }
83    end)
84
85    local total, provider = unpack(result)
86    table.sort(total)
87    table.sort(provider)
88
89    local ms = 1 / 1000000
90    local function fmt(stats)
91      return string.format(
92        'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms',
93        stats[1] * ms,
94        stats[1 + math.floor(#stats * 0.25)] * ms,
95        stats[1 + math.floor(#stats * 0.5)] * ms,
96        stats[1 + math.floor(#stats * 0.75)] * ms,
97        stats[#stats] * ms
98      )
99    end
100
101    print('\nTotal ' .. fmt(total) .. '\nDecoration provider: ' .. fmt(provider))
102  end)
103
104  it('can handle full screen of highlighting', function()
105    Screen.new(100, 51)
106
107    local result = exec_lua(function()
108      local long_line = 'local a={' .. ('a=5,'):rep(22) .. '}'
109      local lines = {}
110      for _ = 1, 50 do
111        table.insert(lines, long_line)
112      end
113      vim.api.nvim_buf_set_lines(0, 0, 0, false, lines)
114      vim.api.nvim_win_set_cursor(0, { 1, 0 })
115      vim.treesitter.start(0, 'lua')
116
117      local total = {}
118      for _ = 1, 100 do
119        local tic = vim.uv.hrtime()
120        vim.cmd 'redraw!'
121        local toc = vim.uv.hrtime()
122        table.insert(total, toc - tic)
123      end
124
125      return { total }
126    end)
127
128    local total = unpack(result)
129    table.sort(total)
130
131    local ms = 1 / 1000000
132    local res = string.format(
133      'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms',
134      total[1] * ms,
135      total[1 + math.floor(#total * 0.25)] * ms,
136      total[1 + math.floor(#total * 0.5)] * ms,
137      total[1 + math.floor(#total * 0.75)] * ms,
138      total[#total] * ms
139    )
140    print('\nTotal ' .. res)
141  end)
142
143  it('can handle long lines with treesitter highlighting', function()
144    Screen.new(100, 51)
145
146    local result = exec_lua(function()
147      local long_line = 'local a = { ' .. ('a = 5, '):rep(2000) .. '}'
148      vim.api.nvim_buf_set_lines(0, 0, 0, false, { long_line })
149      vim.api.nvim_win_set_cursor(0, { 1, 0 })
150      vim.treesitter.start(0, 'lua')
151
152      local total = {}
153      for _ = 1, 50 do
154        local tic = vim.uv.hrtime()
155        vim.cmd 'redraw!'
156        local toc = vim.uv.hrtime()
157        table.insert(total, toc - tic)
158      end
159
160      return { total }
161    end)
162
163    local total = unpack(result)
164    table.sort(total)
165
166    local ms = 1 / 1000000
167    local res = string.format(
168      'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms',
169      total[1] * ms,
170      total[1 + math.floor(#total * 0.25)] * ms,
171      total[1 + math.floor(#total * 0.5)] * ms,
172      total[1 + math.floor(#total * 0.75)] * ms,
173      total[#total] * ms
174    )
175    print('\nTotal ' .. res)
176  end)
177end)