Skip to content

Commit 5513e28

Browse files
perf: store callouts and checkboxes in table keyed on raw value
## Details Currently resolving the callout or checkbox for a particular shortcut_link requires iterating through the configured values one at a time. This is slower than it needs to be since we can pre-compute all the normalized raw values once, store that as the key to the table, then do a single lookup into the table when needed, skipping the for loop. To do this add an additional buffer config type that gets returned from state. This new config will store the normalized component mappings along with all of the other values already present. This will only run once per buffer.
1 parent a5e2d0b commit 5513e28

File tree

13 files changed

+54
-46
lines changed

13 files changed

+54
-46
lines changed

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 September 09
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 11
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/components.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---@class render.md.component.Config
2+
---@field callout table<string, render.md.CustomComponent>
3+
---@field checkbox table<string, render.md.CustomComponent>
4+
5+
---@class render.md.buffer.Config: render.md.BufferConfig
6+
---@field component render.md.component.Config
7+
8+
---@class render.md.component.Resolver
9+
local M = {}
10+
11+
---@param config render.md.BufferConfig
12+
---@return render.md.buffer.Config
13+
function M.resolve(config)
14+
---@type render.md.component.Config
15+
local component = {
16+
callout = M.normalize(config.callout),
17+
checkbox = M.normalize(config.checkbox.custom),
18+
}
19+
return vim.tbl_deep_extend('force', { component = component }, config)
20+
end
21+
22+
---@private
23+
---@param components table<string, render.md.CustomComponent>
24+
function M.normalize(components)
25+
local result = {}
26+
for _, component in pairs(components) do
27+
result[component.raw:lower()] = component
28+
end
29+
return result
30+
end
31+
32+
return M

lua/render-markdown/core/extmark.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Extmark.new(namespace, buf, mark)
1919
return self
2020
end
2121

22-
---@param config render.md.BufferConfig
22+
---@param config render.md.buffer.Config
2323
---@param row? integer
2424
function Extmark:render(config, row)
2525
if self:should_show(config.anti_conceal, row) then

lua/render-markdown/core/ui.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function M.update(buf, win, parse)
9696
end
9797

9898
---@private
99-
---@param config render.md.BufferConfig
99+
---@param config render.md.buffer.Config
100100
---@param win integer
101101
---@return 'default'|'rendered'
102102
function M.next_state(config, win)

lua/render-markdown/handler/markdown.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local str = require('render-markdown.core.str')
66

77
---@class render.md.handler.buf.Markdown
88
---@field private marks render.md.Marks
9-
---@field private config render.md.BufferConfig
9+
---@field private config render.md.buffer.Config
1010
---@field private context render.md.Context
1111
---@field private renderers table<string, render.md.Renderer>
1212
local Handler = {}

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local str = require('render-markdown.core.str')
66

77
---@class render.md.handler.buf.MarkdownInline
88
---@field private marks render.md.Marks
9-
---@field private config render.md.BufferConfig
9+
---@field private config render.md.buffer.Config
1010
---@field private context render.md.Context
1111
local Handler = {}
1212
Handler.__index = Handler
@@ -55,13 +55,13 @@ end
5555
---@private
5656
---@param info render.md.NodeInfo
5757
function Handler:shortcut(info)
58-
local callout = self:get_callout(info)
58+
local callout = self.config.component.callout[info.text:lower()]
5959
if callout ~= nil then
6060
self:callout(info, callout)
6161
return
6262
end
6363

64-
local checkbox = self:get_checkbox(info)
64+
local checkbox = self.config.component.checkbox[info.text:lower()]
6565
if checkbox ~= nil then
6666
self:checkbox(info, checkbox)
6767
return
@@ -74,32 +74,6 @@ function Handler:shortcut(info)
7474
end
7575
end
7676

77-
---@private
78-
---@param info render.md.NodeInfo
79-
---@return render.md.CustomComponent?
80-
function Handler:get_callout(info)
81-
local text = info.text:lower()
82-
for _, callout in pairs(self.config.callout) do
83-
if text == callout.raw:lower() then
84-
return callout
85-
end
86-
end
87-
return nil
88-
end
89-
90-
---@private
91-
---@param info render.md.NodeInfo
92-
---@return render.md.CustomComponent?
93-
function Handler:get_checkbox(info)
94-
local text = info.text
95-
for _, checkbox in pairs(self.config.checkbox.custom) do
96-
if text == checkbox.raw then
97-
return checkbox
98-
end
99-
end
100-
return nil
101-
end
102-
10377
---@private
10478
---@param info render.md.NodeInfo
10579
---@param callout render.md.CustomComponent

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.3.6'
8+
M.version = '6.3.7'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

lua/render-markdown/render/base.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local str = require('render-markdown.core.str')
33

44
---@class render.md.Renderer
55
---@field protected marks render.md.Marks
6-
---@field protected config render.md.BufferConfig
6+
---@field protected config render.md.buffer.Config
77
---@field protected context render.md.Context
88
---@field protected info render.md.NodeInfo
99
---@field setup fun(self: render.md.Renderer): boolean
@@ -12,7 +12,7 @@ local Base = {}
1212
Base.__index = Base
1313

1414
---@param marks render.md.Marks
15-
---@param config render.md.BufferConfig
15+
---@param config render.md.buffer.Config
1616
---@param context render.md.Context
1717
---@param info render.md.NodeInfo
1818
---@return render.md.Renderer

lua/render-markdown/render/code.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local Render = setmetatable({}, Base)
2020
Render.__index = Render
2121

2222
---@param marks render.md.Marks
23-
---@param config render.md.BufferConfig
23+
---@param config render.md.buffer.Config
2424
---@param context render.md.Context
2525
---@param info render.md.NodeInfo
2626
---@return render.md.Renderer

lua/render-markdown/render/heading.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ local Render = setmetatable({}, Base)
2020
Render.__index = Render
2121

2222
---@param marks render.md.Marks
23-
---@param config render.md.BufferConfig
23+
---@param config render.md.buffer.Config
2424
---@param context render.md.Context
2525
---@param info render.md.NodeInfo
2626
---@return render.md.Renderer

0 commit comments

Comments
 (0)