mirror of
https://github.com/neovim/neovim.git
synced 2025-09-23 11:38:31 +00:00
perf(keycodes): use hashy for string lookup
This is slightly faster than the binary search as per the benchmark, and allows handling the vim/vim#16821 situation in generator code.
This commit is contained in:
@@ -1,49 +1,86 @@
|
||||
local names_file = arg[1]
|
||||
|
||||
local hashy = require('gen.hashy')
|
||||
local keycodes = require('nvim.keycodes')
|
||||
|
||||
local names_tgt = assert(io.open(names_file, 'w'))
|
||||
|
||||
--- @type [string, string, integer][]
|
||||
local keycode_names = {}
|
||||
for i, keycode in ipairs(keycodes.names) do
|
||||
table.insert(keycode_names, { keycode[1], keycode[2], i })
|
||||
end
|
||||
table.sort(keycode_names, function(keycode_a, keycode_b)
|
||||
return keycode_a[2]:lower() < keycode_b[2]:lower()
|
||||
end)
|
||||
local keycode_names = keycodes.names
|
||||
|
||||
--- @type table<string,integer>
|
||||
local alt_name_idx = {}
|
||||
for i, keycode in ipairs(keycode_names) do
|
||||
local key = keycode[1]
|
||||
local alt_idx = alt_name_idx[key]
|
||||
if alt_idx == nil or keycode_names[alt_idx][3] > keycode[3] then
|
||||
alt_name_idx[key] = i
|
||||
end
|
||||
end
|
||||
--- Maps lower-case key names to their original indexes.
|
||||
local name_orig_idx = {}
|
||||
|
||||
names_tgt:write([[
|
||||
static const struct key_name_entry {
|
||||
int key; ///< Special key code or ascii value
|
||||
String name; ///< Name of key
|
||||
const String *alt_name; ///< Pointer to alternative key name
|
||||
///< (may be NULL or point to the name in another entry)
|
||||
} key_names_table[] = {]])
|
||||
--- @type table<string,integer>
|
||||
--- Maps keys to the original indexes of their preferred names.
|
||||
local key_orig_idx = {}
|
||||
|
||||
--- @type [string, string][]
|
||||
--- When multiple keys have the same name (e.g. TAB and K_TAB), only the first one
|
||||
--- is added to the two tables above, and the other keys are added here.
|
||||
local extra_keys = {}
|
||||
|
||||
for i, keycode in ipairs(keycode_names) do
|
||||
local key = keycode[1]
|
||||
local name = keycode[2]
|
||||
local alt_idx = alt_name_idx[key]
|
||||
local name_lower = name:lower()
|
||||
if name_orig_idx[name_lower] == nil then
|
||||
name_orig_idx[name_lower] = i
|
||||
if key_orig_idx[key] == nil then
|
||||
key_orig_idx[key] = i
|
||||
end
|
||||
else
|
||||
table.insert(extra_keys, keycode)
|
||||
end
|
||||
end
|
||||
|
||||
local hashorder = vim.tbl_keys(name_orig_idx)
|
||||
table.sort(hashorder)
|
||||
local hashfun
|
||||
hashorder, hashfun = hashy.hashy_hash('get_special_key_code', hashorder, function(idx)
|
||||
return 'key_names_table[' .. idx .. '].name.data'
|
||||
end, true)
|
||||
|
||||
--- @type table<string,integer>
|
||||
--- Maps keys to the (after hash) indexes of the entries with preferred names.
|
||||
local key_hash_idx = {}
|
||||
|
||||
for i, lower_name in ipairs(hashorder) do
|
||||
local orig_idx = name_orig_idx[lower_name]
|
||||
local key = keycode_names[orig_idx][1]
|
||||
if key_orig_idx[key] == orig_idx then
|
||||
key_hash_idx[key] = i
|
||||
end
|
||||
end
|
||||
|
||||
local names_tgt = assert(io.open(names_file, 'w'))
|
||||
names_tgt:write([[
|
||||
static const struct key_name_entry {
|
||||
int key; ///< Special key code or ascii value
|
||||
String name; ///< Name of key
|
||||
const String *pref_name; ///< Pointer to preferred key name
|
||||
///< (may be NULL or point to the name in another entry)
|
||||
} key_names_table[] = {]])
|
||||
|
||||
for i, lower_name in ipairs(hashorder) do
|
||||
local keycode = keycode_names[name_orig_idx[lower_name]]
|
||||
local key = keycode[1]
|
||||
local name = keycode[2]
|
||||
local pref_idx = key_hash_idx[key]
|
||||
names_tgt:write(
|
||||
('\n {%s, {"%s", %d}, %s},'):format(
|
||||
('\n {%s, {"%s", %u}, %s},'):format(
|
||||
key,
|
||||
name,
|
||||
#name,
|
||||
alt_idx == i and 'NULL' or ('&key_names_table[%d].name'):format(alt_idx - 1)
|
||||
pref_idx == i and 'NULL' or ('&key_names_table[%u].name'):format(pref_idx - 1)
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
names_tgt:write('\n};\n')
|
||||
for _, keycode in ipairs(extra_keys) do
|
||||
local key = keycode[1]
|
||||
local name = keycode[2]
|
||||
names_tgt:write(('\n {%s, {"%s", %u}, NULL},'):format(key, name, #name))
|
||||
end
|
||||
|
||||
names_tgt:write('\n};\n\n')
|
||||
names_tgt:write('static ' .. hashfun)
|
||||
names_tgt:close()
|
||||
|
@@ -54,7 +54,7 @@ function M.build_pos_hash(strings)
|
||||
return len_pos_buckets, maxlen, worst_buck_size
|
||||
end
|
||||
|
||||
function M.switcher(put, tab, maxlen, worst_buck_size)
|
||||
function M.switcher(put, tab, maxlen, worst_buck_size, lower)
|
||||
local neworder = {} --- @type string[]
|
||||
put ' switch (len) {\n'
|
||||
local bucky = worst_buck_size > 1
|
||||
@@ -66,7 +66,7 @@ function M.switcher(put, tab, maxlen, worst_buck_size)
|
||||
local keys = vim.tbl_keys(posbuck)
|
||||
if #keys > 1 then
|
||||
table.sort(keys)
|
||||
put('switch (str[' .. (pos - 1) .. ']) {\n')
|
||||
put(('switch (%s(str[%s])) {\n'):format(lower and 'TOLOWER_ASC' or '', pos - 1))
|
||||
for _, c in ipairs(keys) do
|
||||
local buck = posbuck[c]
|
||||
local startidx = #neworder
|
||||
@@ -102,7 +102,7 @@ function M.switcher(put, tab, maxlen, worst_buck_size)
|
||||
return neworder
|
||||
end
|
||||
|
||||
function M.hashy_hash(name, strings, access)
|
||||
function M.hashy_hash(name, strings, access, lower)
|
||||
local stats = {}
|
||||
local put = function(str)
|
||||
table.insert(stats, str)
|
||||
@@ -116,27 +116,27 @@ function M.hashy_hash(name, strings, access)
|
||||
else
|
||||
put(' int low = -1;\n')
|
||||
end
|
||||
local neworder = M.switcher(put, len_pos_buckets, maxlen, worst_buck_size)
|
||||
local neworder = M.switcher(put, len_pos_buckets, maxlen, worst_buck_size, lower)
|
||||
if maxlen == 1 then
|
||||
put([[
|
||||
return -1;
|
||||
]])
|
||||
elseif worst_buck_size > 1 then
|
||||
put([[
|
||||
put(([[
|
||||
for (int i = low; i < high; i++) {
|
||||
if (!memcmp(str, ]] .. access('i') .. [[, len)) {
|
||||
if (!%s(str, %s, len)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
]])
|
||||
]]):format(lower and 'mb_strnicmp' or 'memcmp', access('i')))
|
||||
else
|
||||
put([[
|
||||
if (low < 0 || memcmp(str, ]] .. access('low') .. [[, len)) {
|
||||
put(([[
|
||||
if (low < 0 || %s(str, %s, len)) {
|
||||
return -1;
|
||||
}
|
||||
return low;
|
||||
]])
|
||||
]]):format(lower and 'mb_strnicmp' or 'memcmp', access('low')))
|
||||
end
|
||||
put '}\n\n'
|
||||
return neworder, table.concat(stats)
|
||||
|
Reference in New Issue
Block a user