Skip to content

Commit

Permalink
perf(hash): optimize hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenEast committed Feb 15, 2023
1 parent 14489df commit 734df88
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
5 changes: 0 additions & 5 deletions lua/nightfox/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,4 @@ function M.get_compiled_info(opts)
return output_path, util.join_paths(output_path, style .. file_suffix)
end

function M.hash()
local hash = require("nightfox.lib.hash")(M.options)
return hash and hash or 0
end

return M
8 changes: 6 additions & 2 deletions lua/nightfox/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ function M.setup(opts)

local git_path = util.join_paths(debug.getinfo(1).source:sub(2, -23), ".git", "ORIG_HEAD")
local git = vim.fn.getftime(git_path)
local hash = require("nightfox.lib.hash")(opts) .. (git == -1 and git_path or git)
local hash = require("nightfox.lib.hash")({
opts = opts,
git = git,
path = git_path,
})

if cached ~= hash then
if cached ~= tostring(hash) then
M.compile()
write_file(cached_path, hash)
end
Expand Down
38 changes: 20 additions & 18 deletions lua/nightfox/lib/hash.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
local bitop = bit or bit32 or require("nightfox.lib.native_bit")

-- Reference for dj2b algorithem
-- https://theartincode.stanis.me/008-djb2/
local function djb2(s)
local h = 5381
local t = { string.byte(s, 1, #s) }
for i = 1, #t do
h = bitop.lshift(h, 5) + t[i] -- h * 33 + c
end
return h
end

-- Reference: https://github.com/catppuccin/nvim/blob/151b5f6aa74f08a707a7862519bdc38bb2b9f505/lua/catppuccin/lib/hashing.lua
local function hash(x)
local t = type(x)
local function serialize(val)
local t = type(val)
if t == "table" then
local h = 0
for k, v in pairs(x) do
h = bitop.bxor(h, djb2(k .. hash(v)))
local result = ""
for k, v in pairs(val) do
result = result .. tostring(k) .. serialize(v)
end
return h
return result
elseif t == "function" then
return djb2(string.dump(x))
return string.dump(val)
else
return tostring(val)
end
end

local function checksum(val)
local str = serialize(val)
local hash = 5381
for i = 1, #str do
hash = bit.lshift(hash, 5) + str:byte(i)
end
return tostring(x)
return hash
end

return hash
return checksum
7 changes: 1 addition & 6 deletions lua/nightfox/override.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ local function reset()
store.has_override = false
end

local function hash()
local hash = require("nightfox.lib.hash")(store)
return hash and hash or 0
end

local function check_link(tbl)
for _, style in pairs(tbl) do
for _, opts in pairs(style) do
Expand All @@ -27,7 +22,7 @@ local function check_link(tbl)
end
end

return setmetatable({ reset = reset, hash = hash }, {
return setmetatable({ reset = reset }, {
__index = function(_, value)
if store[value] then
return store[value]
Expand Down

0 comments on commit 734df88

Please sign in to comment.