Skip to content

Commit 9de1920

Browse files
committed
refactor: startup optimization
1 parent bcc93df commit 9de1920

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

lua/gp/dispatcher.lua

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
-- Dispatcher handles the communication between the plugin and LLM providers.
33
--------------------------------------------------------------------------------
44

5+
local uv = vim.uv or vim.loop
6+
57
local logger = require("gp.logger")
68
local tasker = require("gp.tasker")
79
local vault = require("gp.vault")
@@ -18,7 +20,7 @@ local D = {
1820

1921
---@param opts table # user config
2022
D.setup = function(opts)
21-
logger.debug("dispatcher setup started\n" .. vim.inspect(opts))
23+
logger.debug("dispatcher: setup started\n" .. vim.inspect(opts))
2224

2325
D.config.curl_params = opts.curl_params or default_config.curl_params
2426

@@ -52,9 +54,26 @@ D.setup = function(opts)
5254

5355
D.query_dir = helpers.prepare_dir(D.query_dir, "query store")
5456

55-
local files = vim.fn.glob(D.query_dir .. "/*.json", false, true)
57+
local files = {}
58+
local handle = uv.fs_scandir(D.query_dir)
59+
if handle then
60+
local name, type
61+
while true do
62+
name, type = uv.fs_scandir_next(handle)
63+
if not name then
64+
break
65+
end
66+
local path = D.query_dir .. "/" .. name
67+
type = type or uv.fs_stat(path).type
68+
if type == "file" and name:match("%.json$") then
69+
table.insert(files, path)
70+
end
71+
end
72+
end
73+
74+
logger.debug("dispatcher: query files: " .. #files)
5675
if #files > 200 then
57-
logger.debug("too many query files, truncating cache")
76+
logger.debug("dispatcher: too many query files, truncating cache")
5877
table.sort(files, function(a, b)
5978
return a > b
6079
end)
@@ -63,7 +82,7 @@ D.setup = function(opts)
6382
end
6483
end
6584

66-
logger.debug("dispatcher setup finished\n" .. vim.inspect(D))
85+
logger.debug("dispatcher: setup finished\n" .. vim.inspect(D))
6786
end
6887

6988
---@param messages table

lua/gp/logger.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ M.setup = function(path, sensitive)
3333
if vim.fn.isdirectory(dir) == 0 then
3434
vim.fn.mkdir(dir, "p")
3535
end
36+
37+
local file_stats = uv.fs_stat(path)
38+
M.debug("Log file " .. file .. " has " .. (file_stats and file_stats.size or 0) .. " bytes")
39+
3640
file = path
3741

38-
-- truncate log file if it's too big
39-
if uv.fs_stat(file) then
42+
if file_stats and file_stats.size > 5 * 1024 * 1024 then
4043
local content = {}
4144
for line in io.lines(file) do
4245
table.insert(content, line)

lua/gp/macros/target_filetype.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local macro = require("gp.macro")
22

3-
local values = vim.fn.getcompletion("", "filetype")
3+
local values = nil
44

55
local M = {}
66

@@ -16,6 +16,9 @@ M = {
1616
end,
1717

1818
completion = function(params)
19+
if not values then
20+
values = vim.fn.getcompletion("", "filetype")
21+
end
1922
return values
2023
end,
2124

0 commit comments

Comments
 (0)