refactor(hashy): use case labels instead of TOLOWER_ASC() (#32795)

Follow-up to #32768

This is slightly faster according to the benchmark.

This also makes it a build error if hashy is used incorrectly
(generating a case-insensitive hash function from mixed-case strings),
as duplicate case labels aren't allowed.
This commit is contained in:
zeertzjq
2025-03-09 08:58:18 +08:00
committed by GitHub
parent 8ea18119e7
commit 5ee62906a3

View File

@@ -54,7 +54,7 @@ function M.build_pos_hash(strings)
return len_pos_buckets, maxlen, worst_buck_size return len_pos_buckets, maxlen, worst_buck_size
end end
function M.switcher(put, tab, maxlen, worst_buck_size, lower) function M.switcher(put, tab, maxlen, worst_buck_size, icase)
local neworder = {} --- @type string[] local neworder = {} --- @type string[]
put ' switch (len) {\n' put ' switch (len) {\n'
local bucky = worst_buck_size > 1 local bucky = worst_buck_size > 1
@@ -66,13 +66,17 @@ function M.switcher(put, tab, maxlen, worst_buck_size, lower)
local keys = vim.tbl_keys(posbuck) local keys = vim.tbl_keys(posbuck)
if #keys > 1 then if #keys > 1 then
table.sort(keys) table.sort(keys)
put(('switch (%s(str[%s])) {\n'):format(lower and 'TOLOWER_ASC' or '', pos - 1)) put('switch (str[' .. (pos - 1) .. ']) {\n')
for _, c in ipairs(keys) do for _, c in ipairs(keys) do
local buck = posbuck[c] local buck = posbuck[c]
local startidx = #neworder local startidx = #neworder
vim.list_extend(neworder, buck) vim.list_extend(neworder, buck)
local endidx = #neworder local endidx = #neworder
put(" case '" .. c .. "': ") if icase and c:upper() ~= c:lower() then
put((" case '%s': case '%s': "):format(c:upper(), c:lower()))
else
put((" case '%s': "):format(c))
end
if len == 1 then if len == 1 then
put('return ' .. startidx .. ';\n') put('return ' .. startidx .. ';\n')
else else
@@ -102,7 +106,9 @@ function M.switcher(put, tab, maxlen, worst_buck_size, lower)
return neworder return neworder
end end
function M.hashy_hash(name, strings, access, lower) --- @param icase boolean generate a case-insensitive hash function.
--- `strings` must not have mixed case when using this.
function M.hashy_hash(name, strings, access, icase)
local stats = {} local stats = {}
local put = function(str) local put = function(str)
table.insert(stats, str) table.insert(stats, str)
@@ -116,7 +122,7 @@ function M.hashy_hash(name, strings, access, lower)
else else
put(' int low = -1;\n') put(' int low = -1;\n')
end end
local neworder = M.switcher(put, len_pos_buckets, maxlen, worst_buck_size, lower) local neworder = M.switcher(put, len_pos_buckets, maxlen, worst_buck_size, icase)
if maxlen == 1 then if maxlen == 1 then
put([[ put([[
return -1; return -1;
@@ -129,14 +135,14 @@ function M.hashy_hash(name, strings, access, lower)
} }
} }
return -1; return -1;
]]):format(lower and 'vim_strnicmp_asc' or 'memcmp', access('i'))) ]]):format(icase and 'vim_strnicmp_asc' or 'memcmp', access('i')))
else else
put(([[ put(([[
if (low < 0 || %s(str, %s, len)) { if (low < 0 || %s(str, %s, len)) {
return -1; return -1;
} }
return low; return low;
]]):format(lower and 'vim_strnicmp_asc' or 'memcmp', access('low'))) ]]):format(icase and 'vim_strnicmp_asc' or 'memcmp', access('low')))
end end
put '}\n\n' put '}\n\n'
return neworder, table.concat(stats) return neworder, table.concat(stats)