mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	perf(events): use hashy for event name lookup (#32802)
This commit is contained in:
		@@ -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))
 | 
			
		||||
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()
 | 
			
		||||
 | 
			
		||||
names_tgt:write('\n  [NUM_EVENTS] = {0, NULL, (event_T)0},\n};\n')
 | 
			
		||||
names_tgt:write('\nstatic AutoCmdVec autocmds[NUM_EVENTS] = { 0 };\n')
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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}
 | 
			
		||||
 
 | 
			
		||||
@@ -618,37 +618,31 @@ 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 (event_T)abs(event_names[event_hash[hash_idx]].event);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Return the name for event
 | 
			
		||||
///
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user