perf(keymap): reduce bind parser CPU cost (#157)

* fix(keymap): reduce Hyprland bind ID CPU cost

* perf(keymap): avoid bind hashing in all parsers
This commit is contained in:
blacku
2026-07-30 08:35:38 -04:00
committed by GitHub
parent 284c55c71a
commit f87e7c076e
9 changed files with 77 additions and 47 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ end
files[rootPath] = table.concat(rootLines, "\n")
local bindLines, bindDescriptions = {}, {}
for index = 1, 60 do
for index = 1, 120 do
if index % 15 == 1 then
bindLines[#bindLines + 1] = "-- " .. tostring(math.floor(index / 15) + 1) .. ". Group"
end
+9 -17
View File
@@ -21,21 +21,7 @@ local values, watchers = {
categories = { { name = "Previous", binds = { { id = "previous" } } } },
},
}, {}
local reads, xorCalls, loadingCategoryCount = 0, 0, -1
local originalBit32 = bit32
bit32 = {
bxor = function(left, right)
xorCalls = xorCalls + 1
local result, place = 0, 1
for _ = 1, 8 do
if left % 2 ~= right % 2 then result = result + place end
left = math.floor(left / 2)
right = math.floor(right / 2)
place = place * 2
end
return result
end,
}
local reads, loadingCategoryCount = 0, -1
noctalia = {
state = {
@@ -74,7 +60,6 @@ noctalia = {
}
assert(loadfile("service.luau"))()
bit32 = originalBit32
local snapshot = values["keymap.snapshot"]
assert(snapshot.status == "ready", "large Hyprland fixture did not parse")
@@ -82,6 +67,13 @@ assert(snapshot.total == 93, "large Hyprland fixture lost binds")
assert(#snapshot.categories == 5, "large Hyprland fixture lost category markers")
assert(reads == 1, "Hyprland root config should only be read once per refresh")
assert(loadingCategoryCount == 0, "loading snapshot should not reserialize the previous bind tree")
assert(xorCalls > 0, "Hyprland parser did not use the native-xor fingerprint path")
local seenIds = {}
for _, category in ipairs(snapshot.categories) do
for _, bind in ipairs(category.binds) do
assert(bind.id:match("^hypr:"), "invalid Hyprland bind ID")
assert(not seenIds[bind.id], "duplicate Hyprland bind ID")
seenIds[bind.id] = true
end
end
print("hypr scale tests: ok")
+19 -5
View File
@@ -1,5 +1,5 @@
local sourceLines = {}
for index = 1, 93 do
for index = 1, 120 do
if index % 20 == 1 then
sourceLines[#sourceLines + 1] = "# Group " .. tostring(math.floor(index / 20) + 1)
end
@@ -61,15 +61,29 @@ noctalia = {
end,
}
local instructionBlocks = 0
debug.sethook(function() instructionBlocks = instructionBlocks + 1 end, "", 1000)
assert(loadfile("mangowc_service.luau"))()
debug.sethook()
bit32 = originalBit32
local snapshot = values["keymap.snapshot"]
assert(snapshot.status == "ready", "large MangoWC fixture did not parse")
assert(snapshot.total == 93, "large MangoWC fixture lost binds")
assert(#snapshot.categories == 5, "large MangoWC fixture lost category markers")
assert(snapshot.total == 120, "large MangoWC fixture lost binds")
assert(#snapshot.categories == 6, "large MangoWC fixture lost category markers")
assert(reads == 1, "MangoWC root config should only be read once per refresh")
assert(loadingCategoryCount == 0, "loading snapshot should not reserialize the previous bind tree")
assert(xorCalls > 0, "MangoWC parser did not use the native-xor fingerprint path")
assert(xorCalls == 0, "MangoWC visible binds still use per-character fingerprinting")
assert(instructionBlocks < 250, "MangoWC parser exceeded its regression instruction budget")
print("MangoWC scale tests: ok")
local seenIds = {}
for _, category in ipairs(snapshot.categories) do
for _, bind in ipairs(category.binds) do
assert(bind.id:match("^mango:"), "invalid MangoWC bind ID")
assert(not seenIds[bind.id], "duplicate MangoWC bind ID")
assert(bind.fingerprint == "exact-v1", "MangoWC bind did not use exact source verification")
seenIds[bind.id] = true
end
end
print(string.format("MangoWC scale tests: ok (%d blocks)", instructionBlocks))
+6 -2
View File
@@ -80,7 +80,10 @@ noctalia = {
end,
}
local instructionBlocks = 0
debug.sethook(function() instructionBlocks = instructionBlocks + 1 end, "", 1000)
assert(loadfile("niri_service.luau"))()
debug.sethook()
bit32 = originalBit32
string.sub = originalStringSub
@@ -91,10 +94,11 @@ assert(reads["/fixture/config.kdl"] == 1, "Niri root config should only be read
assert(reads["/fixture/mine/binds.kdl"] == 1, "Niri bind include should only be read once")
assert(reads["/fixture/mine/debug.kdl"] == 1, "Niri optional include should only be read once")
assert(reads["/fixture/mine/theme.kdl"] == 1, "Niri non-bind include should only be read once")
assert(xorCalls > 0, "Niri parser did not use the native-xor fingerprint path")
assert(xorCalls == 0, "Niri visible binds still use per-character fingerprinting")
assert(instructionBlocks < 700, "Niri parser exceeded its regression instruction budget")
assert(
stringSubCalls < 45000,
"Niri parser scanned an unrelated include character by character: " .. tostring(stringSubCalls)
)
print("niri CPU-budget regression tests: ok")
print(string.format("niri CPU-budget regression tests: ok (%d blocks)", instructionBlocks))
+6 -3
View File
@@ -72,12 +72,15 @@ assert(snapshot.total == 93, "large Niri fixture lost binds")
assert(#snapshot.categories == 5, "large Niri fixture lost category markers")
assert(reads == 1, "Niri root config should only be read once per refresh")
assert(loadingCategoryCount == 0, "loading snapshot should not reserialize the previous bind tree")
assert(xorCalls > 0, "Niri parser did not use the native-xor fingerprint path")
assert(xorCalls == 0, "Niri visible binds still use per-character fingerprinting")
local seenIds = {}
for _, category in ipairs(snapshot.categories) do
for _, bind in ipairs(category.binds) do
assert(bind.id:match("^niri:[0-9a-f]+$"), "invalid bind fingerprint")
assert(bind.fingerprint:match("^[0-9a-f]+$"), "invalid source fingerprint")
assert(bind.id:match("^niri:"), "invalid Niri bind ID")
assert(not seenIds[bind.id], "duplicate Niri bind ID")
assert(bind.fingerprint == "exact-v1", "Niri bind did not use exact source verification")
seenIds[bind.id] = true
end
end