Skip to content

Commit 05419f9

Browse files
committed
modules/performance: add performance.byteCompileLua option
* add `performance.byteCompileLua.enable` toggle to enable or disable all byte compiling features * add `performance.byteCompileLua.initLua` toggle to enable or disable byte compiling of init.lua * add `writeByteCompiledLua` helper for saving byte compiled lua source code to the nix store * `nixvim-print-init` utility is always pointed to uncompiled init.lua * add tests
1 parent 47b6c48 commit 05419f9

File tree

4 files changed

+183
-2
lines changed

4 files changed

+183
-2
lines changed

lib/builders.nix

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,26 @@
2626
--indent-width 4 \
2727
"$out"
2828
'';
29+
30+
/*
31+
Write a byte compiled lua file to the nix store.
32+
33+
# Type
34+
35+
```
36+
writeByteCompiledLua :: String -> String -> Derivation
37+
```
38+
39+
# Arguments
40+
41+
- [name] The name of the derivation
42+
- [text] The content of the lua file
43+
*/
44+
writeByteCompiledLua =
45+
name: text:
46+
pkgs.runCommandLocal name { inherit text; } ''
47+
echo -n "$text" > "$out"
48+
49+
${lib.getExe' pkgs.luajit "luajit"} -bd -- "$out" "$out"
50+
'';
2951
}

modules/performance.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ let
44
in
55
{
66
options.performance = {
7+
byteCompileLua = {
8+
enable = lib.mkEnableOption "byte compiling of lua files";
9+
initLua = lib.mkOption {
10+
description = "Whether to byte compile init.lua.";
11+
type = types.bool;
12+
default = true;
13+
example = false;
14+
};
15+
};
16+
717
combinePlugins = {
818
enable = lib.mkEnableOption "combinePlugins" // {
919
description = ''

modules/top-level/output.nix

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,17 @@ in
206206
config.content
207207
];
208208

209-
init = helpers.writeLua "init.lua" customRC;
209+
textInit = helpers.writeLua "init.lua" customRC;
210+
byteCompiledInit = helpers.writeByteCompiledLua "init.lua" customRC;
211+
init =
212+
if
213+
config.type == "lua"
214+
&& config.performance.byteCompileLua.enable
215+
&& config.performance.byteCompileLua.initLua
216+
then
217+
byteCompiledInit
218+
else
219+
textInit;
210220

211221
extraWrapperArgs = builtins.concatStringsSep " " (
212222
(optional (
@@ -232,7 +242,7 @@ in
232242
name = "nixvim-print-init";
233243
runtimeInputs = [ pkgs.bat ];
234244
text = ''
235-
bat --language=lua "${init}"
245+
bat --language=lua "${textInit}"
236246
'';
237247
};
238248

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{ pkgs, helpers, ... }:
2+
let
3+
isByteCompiledFun = ''
4+
local function is_byte_compiled(filename)
5+
local f = assert(io.open(filename, "rb"))
6+
local data = assert(f:read("*a"))
7+
-- Assume that file is binary if it contains null bytes
8+
for i = 1, #data do
9+
if data:byte(i) == 0 then
10+
return true
11+
end
12+
end
13+
return false
14+
end
15+
16+
local function test_rtp_file(name, is_compiled)
17+
local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime")
18+
if is_compiled then
19+
assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not")
20+
else
21+
assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is")
22+
end
23+
end
24+
'';
25+
in
26+
{
27+
default.module =
28+
{ config, ... }:
29+
{
30+
performance.byteCompileLua.enable = true;
31+
32+
extraFiles = {
33+
"plugin/file_text.lua".text = "vim.opt.tabstop = 2";
34+
"plugin/file_source.lua".source = helpers.writeLua "file_source.lua" "vim.opt.tabstop = 2";
35+
"plugin/test.vim".text = "set tabstop=2";
36+
"plugin/test.json".text = builtins.toJSON { a = 1; };
37+
};
38+
39+
files = {
40+
"plugin/file.lua" = {
41+
opts.tabstop = 2;
42+
};
43+
"plugin/file.vim" = {
44+
opts.tabstop = 2;
45+
};
46+
};
47+
48+
extraPlugins = [ pkgs.vimPlugins.nvim-lspconfig ];
49+
50+
extraConfigLua = ''
51+
-- The test will search for this string in nixvim-print-init output: VALIDATING_STRING.
52+
-- Since this is the comment, it won't appear in byte compiled file.
53+
'';
54+
55+
# Using plugin for the test code to avoid infinite recursion
56+
extraFiles."plugin/test.lua".text = ''
57+
${isByteCompiledFun}
58+
59+
-- vimrc is byte compiled
60+
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
61+
assert(is_byte_compiled(init), "MYVIMRC is expected to be byte compiled, but it's not")
62+
63+
-- nixvim-print-init prints text
64+
local init_content = vim.fn.system("${config.printInitPackage}/bin/nixvim-print-init")
65+
assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled")
66+
67+
-- extraFiles
68+
test_rtp_file("plugin/file_text.lua", false)
69+
test_rtp_file("plugin/file_source.lua", false)
70+
test_rtp_file("plugin/test.vim", false)
71+
test_rtp_file("plugin/test.json", false)
72+
73+
-- files
74+
test_rtp_file("plugin/file.lua", false)
75+
test_rtp_file("plugin/file.vim", false)
76+
77+
-- Plugins and neovim runtime aren't byte compiled by default
78+
test_rtp_file("lua/vim/lsp.lua", false)
79+
test_rtp_file("lua/lspconfig.lua", false)
80+
'';
81+
82+
};
83+
84+
disabled.module =
85+
{ config, ... }:
86+
{
87+
performance.byteCompileLua.enable = false;
88+
89+
extraFiles."plugin/test1.lua".text = "vim.opt.tabstop = 2";
90+
91+
files."plugin/test2.lua".opts.tabstop = 2;
92+
93+
extraPlugins = [ pkgs.vimPlugins.nvim-lspconfig ];
94+
95+
extraConfigLua = ''
96+
-- The test will search for this string in nixvim-print-init output: VALIDATING_STRING.
97+
-- Since this is the comment, it won't appear in byte compiled file.
98+
'';
99+
100+
# Using plugin for the test code to avoid infinite recursion
101+
extraFiles."plugin/test.lua".text = ''
102+
${isByteCompiledFun}
103+
104+
-- vimrc
105+
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
106+
assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is")
107+
108+
-- nixvim-print-init prints text
109+
local init_content = vim.fn.system("${config.printInitPackage}/bin/nixvim-print-init")
110+
assert(init_content:find("VALIDATING_STRING"), "nixvim-print-init's output is byte compiled")
111+
112+
-- Nothing is byte compiled
113+
-- extraFiles
114+
test_rtp_file("plugin/test1.lua", false)
115+
-- files
116+
test_rtp_file("plugin/test2.lua", false)
117+
-- Plugins
118+
test_rtp_file("lua/lspconfig.lua", false)
119+
-- Neovim runtime
120+
test_rtp_file("lua/vim/lsp.lua", false)
121+
'';
122+
123+
};
124+
125+
init-lua-disabled = {
126+
performance.byteCompileLua = {
127+
enable = true;
128+
initLua = false;
129+
};
130+
131+
extraConfigLuaPost = ''
132+
${isByteCompiledFun}
133+
134+
-- vimrc is not byte compiled
135+
local init = vim.env.MYVIMRC or vim.fn.getscriptinfo({name = "init.lua"})[1].name
136+
assert(not is_byte_compiled(init), "MYVIMRC is not expected to be byte compiled, but it is")
137+
'';
138+
};
139+
}

0 commit comments

Comments
 (0)