perf(events): store autocommands in flat vectors (#23256)

Instead of nested linked lists, store autocommands in a flat, contiguous
kvec_t, with one kvec_t per event type. Previously patterns were stored
in each node of the outer linked list, so they can be matched only once
on repeating patterns. They are now reference counted and referenced in
each autocommand, and matching is skipped if the pattern repeats. Speeds
up creation and deletion, execution is not affected.

Co-authored-by: ii14 <ii14@users.noreply.github.com>
This commit is contained in:
ii14
2023-04-27 19:25:08 +02:00
committed by GitHub
parent eb4676c67f
commit 1cb6040554
8 changed files with 737 additions and 738 deletions

View File

@@ -12,9 +12,7 @@
#include "nvim/regexp_defs.h"
#include "nvim/types.h"
struct AutoCmd_S;
struct AutoPatCmd_S;
struct AutoPat_S;
// event_T definition
#ifdef INCLUDE_GENERATED_DECLARATIONS
@@ -35,49 +33,45 @@ typedef struct {
int save_State; ///< saved State
} aco_save_T;
typedef struct AutoCmd_S AutoCmd;
struct AutoCmd_S {
AucmdExecutable exec;
bool once; // "One shot": removed after execution
bool nested; // If autocommands nest here
bool last; // last command in list
int64_t id; // ID used for uniquely tracking an autocmd.
sctx_T script_ctx; // script context where it is defined
char *desc; // Description for the autocmd.
AutoCmd *next; // Next AutoCmd in list
};
typedef struct {
size_t refcount; ///< Reference count (freed when reaches zero)
char *pat; ///< Pattern as typed
regprog_T *reg_prog; ///< Compiled regprog for pattern
int group; ///< Group ID
int patlen; ///< strlen() of pat
int buflocal_nr; ///< !=0 for buffer-local AutoPat
char allow_dirs; ///< Pattern may match whole path
} AutoPat;
typedef struct AutoPat_S AutoPat;
struct AutoPat_S {
AutoPat *next; // next AutoPat in AutoPat list; MUST
// be the first entry
char *pat; // pattern as typed (NULL when pattern
// has been removed)
regprog_T *reg_prog; // compiled regprog for pattern
AutoCmd *cmds; // list of commands to do
int group; // group ID
int patlen; // strlen() of pat
int buflocal_nr; // !=0 for buffer-local AutoPat
char allow_dirs; // Pattern may match whole path
char last; // last pattern for apply_autocmds()
};
typedef struct {
AucmdExecutable exec; ///< Command or callback function
AutoPat *pat; ///< Pattern reference (NULL when autocmd was removed)
int64_t id; ///< ID used for uniquely tracking an autocmd
char *desc; ///< Description for the autocmd
sctx_T script_ctx; ///< Script context where it is defined
bool once; ///< "One shot": removed after execution
bool nested; ///< If autocommands nest here
} AutoCmd;
/// Struct used to keep status while executing autocommands for an event.
typedef struct AutoPatCmd_S AutoPatCmd;
struct AutoPatCmd_S {
AutoPat *curpat; // next AutoPat to examine
AutoCmd *nextcmd; // next AutoCmd to execute
int group; // group being used
char *fname; // fname to match with
char *sfname; // sfname to match with
char *tail; // tail of fname
event_T event; // current event
sctx_T script_ctx; // script context where it is defined
int arg_bufnr; // initially equal to <abuf>, set to zero when buf is deleted
Object *data; // arbitrary data
AutoPatCmd *next; // chain of active apc-s for auto-invalidation
AutoPat *lastpat; ///< Last matched AutoPat
size_t auidx; ///< Current autocmd index to execute
size_t ausize; ///< Saved AutoCmd vector size
char *fname; ///< Fname to match with
char *sfname; ///< Sfname to match with
char *tail; ///< Tail of fname
int group; ///< Group being used
event_T event; ///< Current event
sctx_T script_ctx; ///< Script context where it is defined
int arg_bufnr; ///< Initially equal to <abuf>, set to zero when buf is deleted
Object *data; ///< Arbitrary data
AutoPatCmd *next; ///< Chain of active apc-s for auto-invalidation
};
typedef kvec_t(AutoCmd) AutoCmdVec;
// Set by the apply_autocmds_group function if the given event is equal to
// EVENT_FILETYPE. Used by the readfile function in order to determine if
// EVENT_BUFREADPOST triggered the EVENT_FILETYPE.