Skip to content

Commit

Permalink
refactor: startup optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Robitx committed Sep 19, 2024
1 parent bcc93df commit 9de1920
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
27 changes: 23 additions & 4 deletions lua/gp/dispatcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
-- Dispatcher handles the communication between the plugin and LLM providers.
--------------------------------------------------------------------------------

local uv = vim.uv or vim.loop

local logger = require("gp.logger")
local tasker = require("gp.tasker")
local vault = require("gp.vault")
Expand All @@ -18,7 +20,7 @@ local D = {

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

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

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

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

local files = vim.fn.glob(D.query_dir .. "/*.json", false, true)
local files = {}
local handle = uv.fs_scandir(D.query_dir)
if handle then
local name, type
while true do
name, type = uv.fs_scandir_next(handle)
if not name then
break
end
local path = D.query_dir .. "/" .. name
type = type or uv.fs_stat(path).type
if type == "file" and name:match("%.json$") then
table.insert(files, path)
end
end
end

logger.debug("dispatcher: query files: " .. #files)
if #files > 200 then
logger.debug("too many query files, truncating cache")
logger.debug("dispatcher: too many query files, truncating cache")
table.sort(files, function(a, b)
return a > b
end)
Expand All @@ -63,7 +82,7 @@ D.setup = function(opts)
end
end

logger.debug("dispatcher setup finished\n" .. vim.inspect(D))
logger.debug("dispatcher: setup finished\n" .. vim.inspect(D))
end

---@param messages table
Expand Down
7 changes: 5 additions & 2 deletions lua/gp/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ M.setup = function(path, sensitive)
if vim.fn.isdirectory(dir) == 0 then
vim.fn.mkdir(dir, "p")
end

local file_stats = uv.fs_stat(path)
M.debug("Log file " .. file .. " has " .. (file_stats and file_stats.size or 0) .. " bytes")

file = path

-- truncate log file if it's too big
if uv.fs_stat(file) then
if file_stats and file_stats.size > 5 * 1024 * 1024 then
local content = {}
for line in io.lines(file) do
table.insert(content, line)
Expand Down
5 changes: 4 additions & 1 deletion lua/gp/macros/target_filetype.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local macro = require("gp.macro")

local values = vim.fn.getcompletion("", "filetype")
local values = nil

local M = {}

Expand All @@ -16,6 +16,9 @@ M = {
end,

completion = function(params)
if not values then
values = vim.fn.getcompletion("", "filetype")
end
return values
end,

Expand Down

0 comments on commit 9de1920

Please sign in to comment.