aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2025-03-10 06:56:02 +0800
committerGitHub <noreply@github.com>2025-03-10 06:56:02 +0800
commit15f2da55a8ded9f1b7737cdd2d38c286fc581fd2 (patch)
treec2f43ab9e06fb3fc08ccc22d1842094739e938fc
parent37786ccfcf07706c7524cdeef321bb4defba45b2 (diff)
downloadrneovim-15f2da55a8ded9f1b7737cdd2d38c286fc581fd2.tar.gz
rneovim-15f2da55a8ded9f1b7737cdd2d38c286fc581fd2.tar.bz2
rneovim-15f2da55a8ded9f1b7737cdd2d38c286fc581fd2.zip
perf(events): use hashy for event name lookup (#32802)
-rw-r--r--src/gen/gen_events.lua25
-rw-r--r--src/nvim/CMakeLists.txt2
-rw-r--r--src/nvim/autocmd.c22
3 files changed, 31 insertions, 18 deletions
diff --git a/src/gen/gen_events.lua b/src/gen/gen_events.lua
index 020599301c..23ca81b684 100644
--- a/src/gen/gen_events.lua
+++ b/src/gen/gen_events.lua
@@ -1,6 +1,7 @@
local fileio_enum_file = arg[1]
local names_file = arg[2]
+local hashy = require('gen.hashy')
local auevents = require('nvim.auevents')
local events = auevents.events
local aliases = auevents.aliases
@@ -41,9 +42,27 @@ for i, name in ipairs(names) do
end
enum_tgt:write(('\n NUM_EVENTS = %u,'):format(#names))
+enum_tgt:write('\n} event_T;\n')
+enum_tgt:close()
+
names_tgt:write('\n [NUM_EVENTS] = {0, NULL, (event_T)0},\n};\n')
names_tgt:write('\nstatic AutoCmdVec autocmds[NUM_EVENTS] = { 0 };\n')
-names_tgt:close()
-enum_tgt:write('\n} event_T;\n')
-enum_tgt:close()
+local hashorder = vim.tbl_map(string.lower, names)
+table.sort(hashorder)
+local hashfun
+hashorder, hashfun = hashy.hashy_hash('event_name2nr', hashorder, function(idx)
+ return 'event_names[event_hash[' .. idx .. ']].name'
+end, true)
+
+names_tgt:write([[
+
+static const event_T event_hash[] = {]])
+
+for _, lower_name in ipairs(hashorder) do
+ names_tgt:write(('\n EVENT_%s,'):format(lower_name:upper()))
+end
+
+names_tgt:write('\n};\n\n')
+names_tgt:write('static ' .. hashfun)
+names_tgt:close()
diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt
index 355c3c2e6b..2a60eefe7c 100644
--- a/src/nvim/CMakeLists.txt
+++ b/src/nvim/CMakeLists.txt
@@ -677,7 +677,7 @@ add_custom_command(OUTPUT ${GENERATED_FUNCS} ${FUNCS_DATA}
add_custom_command(OUTPUT ${GENERATED_EVENTS_ENUM} ${GENERATED_EVENTS_NAMES_MAP}
COMMAND ${LUA_GEN} ${EVENTS_GENERATOR} ${GENERATED_EVENTS_ENUM} ${GENERATED_EVENTS_NAMES_MAP}
- DEPENDS ${LUA_GEN_DEPS} ${EVENTS_GENERATOR} ${CMAKE_CURRENT_LIST_DIR}/auevents.lua
+ DEPENDS ${LUA_GEN_DEPS} ${EVENTS_GENERATOR} ${GENERATOR_HASHY} ${CMAKE_CURRENT_LIST_DIR}/auevents.lua
)
add_custom_command(OUTPUT ${GENERATED_KEYCODE_NAMES}
diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c
index 51d952ed3e..28c3906ce3 100644
--- a/src/nvim/autocmd.c
+++ b/src/nvim/autocmd.c
@@ -618,36 +618,30 @@ bool is_aucmd_win(win_T *win)
event_T event_name2nr(const char *start, char **end)
{
const char *p;
- int i;
// the event name ends with end of line, '|', a blank or a comma
for (p = start; *p && !ascii_iswhite(*p) && *p != ',' && *p != '|'; p++) {}
- for (i = 0; event_names[i].name != NULL; i++) {
- int len = (int)event_names[i].len;
- if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) {
- break;
- }
- }
+
+ int hash_idx = event_name2nr_hash(start, (size_t)(p - start));
if (*p == ',') {
p++;
}
*end = (char *)p;
- if (event_names[i].name == NULL) {
+ if (hash_idx < 0) {
return NUM_EVENTS;
}
- return (event_T)abs(event_names[i].event);
+ return (event_T)abs(event_names[event_hash[hash_idx]].event);
}
/// Return the event number for event name "str".
/// Return NUM_EVENTS if the event name was not found.
event_T event_name2nr_str(String str)
{
- for (int i = 0; event_names[i].name != NULL; i++) {
- if (str.size == event_names[i].len && STRNICMP(str.data, event_names[i].name, str.size) == 0) {
- return (event_T)abs(event_names[i].event);
- }
+ int hash_idx = event_name2nr_hash(str.data, str.size);
+ if (hash_idx < 0) {
+ return NUM_EVENTS;
}
- return NUM_EVENTS;
+ return (event_T)abs(event_names[event_hash[hash_idx]].event);
}
/// Return the name for event