Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(hash): optimize hash function #312

Merged
merged 1 commit into from Feb 17, 2023
Merged

perf(hash): optimize hash function #312

merged 1 commit into from Feb 17, 2023

Conversation

EdenEast
Copy link
Owner

@EdenEast EdenEast commented Feb 15, 2023

This pr optimizes the hash function used to compute checksums for cached values. This is achieved by serializing a table into a minimum string representation and then performing the dj2b hashing algorithm on this string.

This pr optimizes the dj2b function by removing { string.byte } because it is only faster when the string is very long, which isn't the case for configurations

Benchmarks were on both main and perf branch.

local t = require("nightfox.config").options
local hash = require("nightfox.lib.hash")

local function bench(msg, f, iter)
  iter = iter or 1000
  local sum = 0
  for _ = 1, iter do
    local start = vim.loop.hrtime()
    f()
    sum = sum + (vim.loop.hrtime() - start)
  end
  print(msg, sum / iter / 1000000)
end

bench("perf", function()
  hash(t)
end, 10000)

Results

branch time ms
main 0.4782018948
perf 0.0033633242

@EdenEast EdenEast self-assigned this Feb 15, 2023
@EdenEast EdenEast added enhancement New feature or request compiler Change relating to style compiler labels Feb 15, 2023
@EdenEast
Copy link
Owner Author

There is an issue with this solution as it is not reproducible over different lua sessions.

@nullchilly
Copy link
Collaborator

We can replace this PR with catppuccin/nvim#410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Change relating to style compiler enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants