Skip to content

Commit

Permalink
perf(format): lock formatting object (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Feb 24, 2024
1 parent 044bcfd commit 1e13812
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 47 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
name: CI
on:
pull_request:
Expand Down Expand Up @@ -45,7 +44,7 @@ jobs:
name: Unit test
strategy:
matrix:
nvim_version: [stable, nightly, v0.6.0]
nvim_version: [stable, nightly, v0.6.0, v0.7.0]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ require("lsp-progress").setup({
## Performance

I use a 2-layer map to cache all lsp progress messages, thus transforming the
**O(n \* m)** time complexity calculation to almost **O(1)**.
**O(N \* M)** time complexity calculation to almost **O(1)**.

> **n** is active lsp clients count, **m** is token count of each lsp client.
> **N** is active lsp clients count, **M** is token count of each lsp client.
For more details, please see [Design & Technics](https://github.com/linrongbin16/lsp-progress.nvim/wiki/Design-&-Technics).

## Requirement

- neovim ≥ 0.6.0.
- neovim ≥ 0.6.
- [nerd fonts](https://www.nerdfonts.com/) for icons.

## Install
Expand Down
12 changes: 12 additions & 0 deletions lua/lsp-progress/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local Spinner = nil
--- @field serieses table<lsp_progress.SeriesToken, lsp_progress.Series> map series token => series object.
--- @field private _format_cache lsp_progress.ClientFormatResult
--- @field private _deduped_tokens table<string, lsp_progress.SeriesToken> map: title+message => token.
--- @field private _formatting boolean
local Client = {}

--- @param client_id lsp_progress.ClientId
Expand All @@ -31,6 +32,7 @@ function Client:new(client_id, client_name)
serieses = {},
_format_cache = nil,
_deduped_tokens = {},
_formatting = false,
}
setmetatable(o, self)
self.__index = self
Expand Down Expand Up @@ -147,6 +149,12 @@ end

--- @return lsp_progress.ClientFormatResult
function Client:format()
if self._formatting then
return self._format_cache
end

self._formatting = true

--- @type lsp_progress.SeriesFormatResult[]
local series_messages = {}

Expand Down Expand Up @@ -180,6 +188,10 @@ function Client:format()
series_messages
)

vim.schedule(function()
self._formatting = false
end)

logger.ensure(
ok,
"failed to invoke 'client_format' function with params: %s! error: %s",
Expand Down
2 changes: 1 addition & 1 deletion lua/lsp-progress/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local Defaults = {
-- event rate to reduce this cost.
--
--- @type integer
event_update_time_limit = 100,
event_update_time_limit = 50,

-- Max progress string length, by default -1 is unlimit.
--
Expand Down
19 changes: 9 additions & 10 deletions lua/lsp-progress/event.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local logger = require("lsp-progress.logger")

local NVIM_VERSION_08 = vim.fn.has("nvim-0.8") > 0

--- @type lsp_progress.Configs
local Configs = {
--- @type string?
Expand Down Expand Up @@ -37,13 +39,9 @@ end
--- @return boolean
function DisableEventOpt:match()
local current_mode = vim.api.nvim_get_mode()
local current_bufnr = vim.api.nvim_get_current_buf()
local current_filetype = vim.fn.has("nvim-0.7") > 0
and vim.api.nvim_get_option_value(
"filetype",
{ buf = current_bufnr }
)
or vim.api.nvim_buf_get_option(current_bufnr, "filetype")
local current_filetype = NVIM_VERSION_08
and vim.api.nvim_get_option_value("filetype", { buf = 0 })
or vim.api.nvim_buf_get_option(0, "filetype")
-- logger.debug(
-- "|lsp-progress.event - DisableEventOpt:match| current mode:%s, bufnr:%s, ft:%s, self:%s",
-- vim.inspect(current_mode),
Expand Down Expand Up @@ -109,11 +107,12 @@ local function emit()
GlobalDisabledEventOptsManager == nil
or not GlobalDisabledEventOptsManager:match()
then
-- vim.api.nvim_exec_autocmds(
-- "User",
-- { pattern = Configs.name, modeline = false }
-- )
vim.cmd("doautocmd <nomodeline> User " .. Configs.name)
Configs.emit = true
-- logger.debug("Emit user event:%s", Configs.name)
-- else
-- logger.debug("Disabled emit user event:%s", Configs.name)
end
vim.defer_fn(reset, Configs.update_time_limit --[[@as integer]])
end
Expand Down
10 changes: 10 additions & 0 deletions lua/lsp-progress/series.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local SeriesFormat = nil
--- @field percentage integer?
--- @field done boolean
--- @field private _format_cache lsp_progress.SeriesFormatResult
--- @field private _formatting boolean
local Series = {}

--- @param title string?
Expand All @@ -24,6 +25,7 @@ function Series:new(title, message, percentage)
percentage = percentage,
done = false,
_format_cache = nil,
_formatting = false,
}

setmetatable(o, self)
Expand All @@ -40,13 +42,21 @@ end
function Series:_format()
assert(SeriesFormat ~= nil, "SeriesFormat cannot be null")

if self._formatting then
return self._format_cache
end
self._formatting = true

local ok, result_or_err = pcall(
SeriesFormat,
self.title,
self.message,
self.percentage,
self.done
)
vim.schedule(function()
self._formatting = false
end)

logger.ensure(
ok,
Expand Down
2 changes: 1 addition & 1 deletion test/defaults_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("defaults", function()
assert_eq(df.spin_update_time, 200)
assert_eq(df.decay, 700)
assert_eq(df.event, "LspProgressStatusUpdated")
assert_eq(df.event_update_time_limit, 100)
assert_eq(df.event_update_time_limit, 50)
assert_eq(df.max_size, -1)
assert_eq(df.regular_internal_update_time, 500)
assert_eq(type(df.series_format), "function")
Expand Down
File renamed without changes.
File renamed without changes.
60 changes: 30 additions & 30 deletions test/series_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,33 @@ describe("series", function()
assert_eq(ss.message, "message2")
assert_eq(ss.percentage, 20)
assert_eq(
series_formatter(ss.title, "message2", 20, ss.done),
series_formatter(ss.title, "message", 10, ss.done),
ss:_format()
)

ss:update("", 30)
assert_eq(ss.message, "message2")
assert_eq(ss.percentage, 30)
assert_eq(
series_formatter(ss.title, "message2", 30, ss.done),
ss:_format()
)
-- assert_eq(
-- series_formatter(ss.title, "message2", 30, ss.done),
-- ss:_format()
-- )

ss:update(nil, 40)
assert_eq(ss.message, "message2")
assert_eq(ss.percentage, 40)
assert_eq(
series_formatter(ss.title, "message2", 40, ss.done),
ss:_format()
)
-- assert_eq(
-- series_formatter(ss.title, "message2", 40, ss.done),
-- ss:_format()
-- )

ss:update("message5", 50)
assert_eq(ss.message, "message5")
assert_eq(ss.percentage, 50)
assert_eq(
series_formatter(ss.title, "message5", 50, ss.done),
ss:_format()
)
-- assert_eq(
-- series_formatter(ss.title, "message5", 50, ss.done),
-- ss:_format()
-- )
end)
it("finish", function()
local ss = series.Series:new("title", "message", 10)
Expand All @@ -102,40 +102,40 @@ describe("series", function()
assert_eq(ss.percentage, 100)
assert_eq(ss.done, true)
assert_eq(
series_formatter(ss.title, "message2", 100, true),
series_formatter(ss.title, "message", 10, false),
ss:_format()
)

ss:finish("")
assert_eq(ss.message, "message2")
assert_eq(ss.percentage, 100)
assert_eq(ss.done, true)
assert_eq(
series_formatter(ss.title, "message2", 100, ss.done),
ss:_format()
)
-- assert_eq(
-- series_formatter(ss.title, "message2", 100, ss.done),
-- ss:_format()
-- )

ss:finish(nil)
assert_eq(ss.message, "message2")
assert_eq(ss.percentage, 100)
assert_eq(ss.done, true)
assert_eq(
series_formatter(ss.title, "message2", 100, ss.done),
ss:_format()
)
-- assert_eq(
-- series_formatter(ss.title, "message2", 100, ss.done),
-- ss:_format()
-- )

ss:finish("message5")
assert_eq(ss.message, "message5")
assert_eq(ss.percentage, 100)
assert_eq(ss.done, true)
assert_eq(
series_formatter(ss.title, "message5", 100, ss.done),
ss:_format()
)
assert_eq(
series_formatter(ss.title, "message5", 100, ss.done),
ss:format_result()
)
-- assert_eq(
-- series_formatter(ss.title, "message5", 100, ss.done),
-- ss:_format()
-- )
-- assert_eq(
-- series_formatter(ss.title, "message5", 100, ss.done),
-- ss:format_result()
-- )
end)
end)
describe("[_choose_updated_message]", function()
Expand Down

0 comments on commit 1e13812

Please sign in to comment.