Skip to content

Commit

Permalink
perf: store callouts and checkboxes in table keyed on raw value
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
MeanderingProgrammer committed Sep 11, 2024
1 parent a5e2d0b commit 5513e28
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion doc/render-markdown.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*render-markdown.txt* For 0.10.0 Last change: 2024 September 09
*render-markdown.txt* For 0.10.0 Last change: 2024 September 11

==============================================================================
Table of Contents *render-markdown-table-of-contents*
Expand Down
32 changes: 32 additions & 0 deletions lua/render-markdown/components.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---@class render.md.component.Config
---@field callout table<string, render.md.CustomComponent>
---@field checkbox table<string, render.md.CustomComponent>

---@class render.md.buffer.Config: render.md.BufferConfig
---@field component render.md.component.Config

---@class render.md.component.Resolver
local M = {}

---@param config render.md.BufferConfig
---@return render.md.buffer.Config
function M.resolve(config)
---@type render.md.component.Config
local component = {
callout = M.normalize(config.callout),
checkbox = M.normalize(config.checkbox.custom),
}
return vim.tbl_deep_extend('force', { component = component }, config)
end

---@private
---@param components table<string, render.md.CustomComponent>
function M.normalize(components)
local result = {}
for _, component in pairs(components) do
result[component.raw:lower()] = component
end
return result
end

return M
2 changes: 1 addition & 1 deletion lua/render-markdown/core/extmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Extmark.new(namespace, buf, mark)
return self
end

---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param row? integer
function Extmark:render(config, row)
if self:should_show(config.anti_conceal, row) then
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/core/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function M.update(buf, win, parse)
end

---@private
---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param win integer
---@return 'default'|'rendered'
function M.next_state(config, win)
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local str = require('render-markdown.core.str')

---@class render.md.handler.buf.Markdown
---@field private marks render.md.Marks
---@field private config render.md.BufferConfig
---@field private config render.md.buffer.Config
---@field private context render.md.Context
---@field private renderers table<string, render.md.Renderer>
local Handler = {}
Expand Down
32 changes: 3 additions & 29 deletions lua/render-markdown/handler/markdown_inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local str = require('render-markdown.core.str')

---@class render.md.handler.buf.MarkdownInline
---@field private marks render.md.Marks
---@field private config render.md.BufferConfig
---@field private config render.md.buffer.Config
---@field private context render.md.Context
local Handler = {}
Handler.__index = Handler
Expand Down Expand Up @@ -55,13 +55,13 @@ end
---@private
---@param info render.md.NodeInfo
function Handler:shortcut(info)
local callout = self:get_callout(info)
local callout = self.config.component.callout[info.text:lower()]
if callout ~= nil then
self:callout(info, callout)
return
end

local checkbox = self:get_checkbox(info)
local checkbox = self.config.component.checkbox[info.text:lower()]
if checkbox ~= nil then
self:checkbox(info, checkbox)
return
Expand All @@ -74,32 +74,6 @@ function Handler:shortcut(info)
end
end

---@private
---@param info render.md.NodeInfo
---@return render.md.CustomComponent?
function Handler:get_callout(info)
local text = info.text:lower()
for _, callout in pairs(self.config.callout) do
if text == callout.raw:lower() then
return callout
end
end
return nil
end

---@private
---@param info render.md.NodeInfo
---@return render.md.CustomComponent?
function Handler:get_checkbox(info)
local text = info.text
for _, checkbox in pairs(self.config.checkbox.custom) do
if text == checkbox.raw then
return checkbox
end
end
return nil
end

---@private
---@param info render.md.NodeInfo
---@param callout render.md.CustomComponent
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local M = {}

---@private
---@type string
M.version = '6.3.6'
M.version = '6.3.7'

function M.check()
vim.health.start('render-markdown.nvim [version]')
Expand Down
4 changes: 2 additions & 2 deletions lua/render-markdown/render/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local str = require('render-markdown.core.str')

---@class render.md.Renderer
---@field protected marks render.md.Marks
---@field protected config render.md.BufferConfig
---@field protected config render.md.buffer.Config
---@field protected context render.md.Context
---@field protected info render.md.NodeInfo
---@field setup fun(self: render.md.Renderer): boolean
Expand All @@ -12,7 +12,7 @@ local Base = {}
Base.__index = Base

---@param marks render.md.Marks
---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/render/code.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local Render = setmetatable({}, Base)
Render.__index = Render

---@param marks render.md.Marks
---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/render/heading.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local Render = setmetatable({}, Base)
Render.__index = Render

---@param marks render.md.Marks
---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/render/quote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local Render = setmetatable({}, Base)
Render.__index = Render

---@param marks render.md.Marks
---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/render/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local Render = setmetatable({}, Base)
Render.__index = Render

---@param marks render.md.Marks
---@param config render.md.BufferConfig
---@param config render.md.buffer.Config
---@param context render.md.Context
---@param info render.md.NodeInfo
---@return render.md.Renderer
Expand Down
14 changes: 8 additions & 6 deletions lua/render-markdown/state.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
local components = require('render-markdown.components')
local presets = require('render-markdown.presets')
local treesitter = require('render-markdown.core.treesitter')
local util = require('render-markdown.core.util')

---@type table<integer, render.md.BufferConfig>
---@type table<integer, render.md.buffer.Config>
local configs = {}

---@class render.md.State
Expand Down Expand Up @@ -66,17 +67,18 @@ function M.modify_anti_conceal(amount)
end

---@param buf integer
---@return render.md.BufferConfig
---@return render.md.buffer.Config
function M.get_config(buf)
local config = configs[buf]
if config == nil then
config = M.default_buffer_config()
local buf_config = M.default_buffer_config()
for _, name in ipairs({ 'buftype', 'filetype' }) do
local override_config = M.config.overrides[name][util.get_buf(buf, name)]
if override_config ~= nil then
config = vim.tbl_deep_extend('force', config, override_config)
local override = M.config.overrides[name][util.get_buf(buf, name)]
if override ~= nil then
buf_config = vim.tbl_deep_extend('force', buf_config, override)
end
end
config = components.resolve(buf_config)
configs[buf] = config
end
return config
Expand Down

0 comments on commit 5513e28

Please sign in to comment.