Skip to content

Commit

Permalink
perf: lazy load yazi.nvim modules by default (#253)
Browse files Browse the repository at this point in the history
Previously yazi.nvim would load most of its modules up front when
required. This is not ideal as typically this delays Neovim's startup
time. Although the delay is measured in milliseconds, it might add up if
tens of other plugins need to be loaded too.

This commit changes the behavior so that yazi.nvim only loads the
modules when they are actually needed. The number of loaded modules has
changed from 17 to 7, but I did not measure the startup time difference.
  • Loading branch information
mikavilpas authored Jul 24, 2024
1 parent 1b44d62 commit f832c3c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
Empty file removed .gitmodules
Empty file.
1 change: 1 addition & 0 deletions integration-tests/client/testEnvironmentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type StartupScriptModification =
| "modify_yazi_config_to_use_ya_as_event_reader.lua"
| "modify_yazi_config_and_add_hovered_buffer_background.lua"
| "use_light_neovim_colorscheme.lua"
| "report_loaded_yazi_modules.lua"

declare global {
interface Window {
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/cypress/e2e/lazy-loading.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { startNeovimWithYa } from "./using-ya-to-read-events/startNeovimWithYa"

describe("lazy loading yazi.nvim", () => {
// The idea is that yazi.nvim exposes its public functions when you call
// `require("yazi")`. It should load as little code up front as possible,
// because some users don't use a package manager like lazy.nvim that
// supports lazy loading modules by default.

it("delays loading of many source code files", () => {
// Not sure what would be a good way to test lazy loading. For now, just
// load the plugin and see how many modules have been loaded.
//
// Maybe in the future, we can have a better way to do this.
cy.visit("http://localhost:5173")
startNeovimWithYa({
startupScriptModifications: ["report_loaded_yazi_modules.lua"],
})

cy.typeIntoTerminal(":=CountYaziModules(){enter}")

// NOTE: if this number changes in the future, it's ok. This test is just
// to make sure that we don't accidentally load all modules up front due to
// an unrelated change.
cy.contains("Loaded 7 modules")
})
})
9 changes: 9 additions & 0 deletions integration-tests/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ io.on("connection", function connection(socket) {
args.push("-c", `lua dofile('${file}')`)
break
}
case "report_loaded_yazi_modules.lua": {
const file = path.join(
testDirectory,
"config-modifications",
"report_loaded_yazi_modules.lua",
)
args.push("-c", `lua dofile('${file}')`)
break
}
default:
modification satisfies never
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require('yazi')

-- selene: allow(unused_variable)
function CountYaziModules()
---@type string[]
local yazi_modules = {}

for k, _ in pairs(package.loaded) do
if k:sub(1, 4) == 'yazi' then
yazi_modules[#yazi_modules + 1] = k
end
end

return vim.inspect({
string.format('Loaded %s modules', #yazi_modules),
table.concat(yazi_modules, ','),
})
end
16 changes: 10 additions & 6 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
---@module "plenary"

local window = require('yazi.window')
local utils = require('yazi.utils')
local configModule = require('yazi.config')
local event_handling = require('yazi.event_handling')
local Log = require('yazi.log')
local YaziProcess = require('yazi.process.yazi_process')

local M = {}

Expand All @@ -18,13 +13,19 @@ M.previous_state = {}
---@param config? YaziConfig?
---@param input_path? string
function M.yazi(config, input_path)
local utils = require('yazi.utils')
local YaziProcess = require('yazi.process.yazi_process')
local event_handling = require('yazi.event_handling')

if utils.is_yazi_available() ~= true then
print('Please install yazi. Check the documentation for more information')
return
end

config =
vim.tbl_deep_extend('force', configModule.default(), M.config, config or {})

local Log = require('yazi.log')
Log.level = config.log_level

if
Expand All @@ -45,7 +46,7 @@ function M.yazi(config, input_path)
config.chosen_file_path = config.chosen_file_path or vim.fn.tempname()
config.events_file_path = config.events_file_path or vim.fn.tempname()

local win = window.YaziFloatingWindow.new(config)
local win = require('yazi.window').YaziFloatingWindow.new(config)
win:open_and_display()

local yazi_process = YaziProcess:start(
Expand Down Expand Up @@ -140,6 +141,8 @@ function M.toggle(config)
)

local path = M.previous_state and M.previous_state.last_hovered or nil

local Log = require('yazi.log')
if path == nil then
Log:debug('No previous file hovered, opening yazi with default path')
else
Expand All @@ -157,6 +160,7 @@ function M.setup(opts)
M.config =
vim.tbl_deep_extend('force', configModule.default(), M.config, opts or {})

local Log = require('yazi.log')
Log.level = M.config.log_level

local yazi_augroup = vim.api.nvim_create_augroup('yazi', { clear = true })
Expand Down

0 comments on commit f832c3c

Please sign in to comment.