refactor(syntax): don't use pointer indirected maps for no reason

This commit is contained in:
Björn Linse
2021-08-22 10:10:15 +02:00
parent 9e651a9d09
commit 4500253f60
5 changed files with 41 additions and 43 deletions

View File

@@ -25,22 +25,16 @@ static bool hlstate_active = false;
static kvec_t(HlEntry) attr_entries = KV_INITIAL_VALUE; static kvec_t(HlEntry) attr_entries = KV_INITIAL_VALUE;
static Map(HlEntry, int) *attr_entry_ids; static Map(HlEntry, int) attr_entry_ids = MAP_INIT;
static Map(int, int) *combine_attr_entries; static Map(int, int) combine_attr_entries = MAP_INIT;
static Map(int, int) *blend_attr_entries; static Map(int, int) blend_attr_entries = MAP_INIT;
static Map(int, int) *blendthrough_attr_entries; static Map(int, int) blendthrough_attr_entries = MAP_INIT;
/// highlight entries private to a namespace /// highlight entries private to a namespace
static Map(ColorKey, ColorItem) *ns_hl; static Map(ColorKey, ColorItem) ns_hl;
void highlight_init(void) void highlight_init(void)
{ {
attr_entry_ids = map_new(HlEntry, int)();
combine_attr_entries = map_new(int, int)();
blend_attr_entries = map_new(int, int)();
blendthrough_attr_entries = map_new(int, int)();
ns_hl = map_new(ColorKey, ColorItem)();
// index 0 is no attribute, add dummy entry: // index 0 is no attribute, add dummy entry:
kv_push(attr_entries, ((HlEntry){ .attr = HLATTRS_INIT, .kind = kHlUnknown, kv_push(attr_entries, ((HlEntry){ .attr = HLATTRS_INIT, .kind = kHlUnknown,
.id1 = 0, .id2 = 0 })); .id1 = 0, .id2 = 0 }));
@@ -71,7 +65,7 @@ static int get_attr_entry(HlEntry entry)
entry.id2 = 0; entry.id2 = 0;
} }
int id = map_get(HlEntry, int)(attr_entry_ids, entry); int id = map_get(HlEntry, int)(&attr_entry_ids, entry);
if (id > 0) { if (id > 0) {
return id; return id;
} }
@@ -104,7 +98,7 @@ static int get_attr_entry(HlEntry entry)
id = (int)next_id; id = (int)next_id;
kv_push(attr_entries, entry); kv_push(attr_entries, entry);
map_put(HlEntry, int)(attr_entry_ids, entry, id); map_put(HlEntry, int)(&attr_entry_ids, entry, id);
Array inspect = hl_inspect(id); Array inspect = hl_inspect(id);
@@ -154,7 +148,7 @@ void ns_hl_def(NS ns_id, int hl_id, HlAttrs attrs, int link_id)
{ {
DecorProvider *p = get_decor_provider(ns_id, true); DecorProvider *p = get_decor_provider(ns_id, true);
if ((attrs.rgb_ae_attr & HL_DEFAULT) if ((attrs.rgb_ae_attr & HL_DEFAULT)
&& map_has(ColorKey, ColorItem)(ns_hl, ColorKey(ns_id, hl_id))) { && map_has(ColorKey, ColorItem)(&ns_hl, ColorKey(ns_id, hl_id))) {
return; return;
} }
int attr_id = link_id > 0 ? -1 : hl_get_syn_attr(ns_id, hl_id, attrs); int attr_id = link_id > 0 ? -1 : hl_get_syn_attr(ns_id, hl_id, attrs);
@@ -162,7 +156,7 @@ void ns_hl_def(NS ns_id, int hl_id, HlAttrs attrs, int link_id)
.link_id = link_id, .link_id = link_id,
.version = p->hl_valid, .version = p->hl_valid,
.is_default = (attrs.rgb_ae_attr & HL_DEFAULT) }; .is_default = (attrs.rgb_ae_attr & HL_DEFAULT) };
map_put(ColorKey, ColorItem)(ns_hl, ColorKey(ns_id, hl_id), it); map_put(ColorKey, ColorItem)(&ns_hl, ColorKey(ns_id, hl_id), it);
} }
int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault) int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault)
@@ -177,7 +171,7 @@ int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault)
} }
DecorProvider *p = get_decor_provider(ns_id, true); DecorProvider *p = get_decor_provider(ns_id, true);
ColorItem it = map_get(ColorKey, ColorItem)(ns_hl, ColorKey(ns_id, hl_id)); ColorItem it = map_get(ColorKey, ColorItem)(&ns_hl, ColorKey(ns_id, hl_id));
// TODO(bfredl): map_ref true even this? // TODO(bfredl): map_ref true even this?
bool valid_cache = it.version >= p->hl_valid; bool valid_cache = it.version >= p->hl_valid;
@@ -220,7 +214,7 @@ int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault)
it.attr_id = fallback ? -1 : hl_get_syn_attr((int)ns_id, hl_id, attrs); it.attr_id = fallback ? -1 : hl_get_syn_attr((int)ns_id, hl_id, attrs);
it.version = p->hl_valid-tmp; it.version = p->hl_valid-tmp;
it.is_default = attrs.rgb_ae_attr & HL_DEFAULT; it.is_default = attrs.rgb_ae_attr & HL_DEFAULT;
map_put(ColorKey, ColorItem)(ns_hl, ColorKey(ns_id, hl_id), it); map_put(ColorKey, ColorItem)(&ns_hl, ColorKey(ns_id, hl_id), it);
} }
if (it.is_default && nodefault) { if (it.is_default && nodefault) {
@@ -395,28 +389,28 @@ void clear_hl_tables(bool reinit)
{ {
if (reinit) { if (reinit) {
kv_size(attr_entries) = 1; kv_size(attr_entries) = 1;
map_clear(HlEntry, int)(attr_entry_ids); map_clear(HlEntry, int)(&attr_entry_ids);
map_clear(int, int)(combine_attr_entries); map_clear(int, int)(&combine_attr_entries);
map_clear(int, int)(blend_attr_entries); map_clear(int, int)(&blend_attr_entries);
map_clear(int, int)(blendthrough_attr_entries); map_clear(int, int)(&blendthrough_attr_entries);
memset(highlight_attr_last, -1, sizeof(highlight_attr_last)); memset(highlight_attr_last, -1, sizeof(highlight_attr_last));
highlight_attr_set_all(); highlight_attr_set_all();
highlight_changed(); highlight_changed();
screen_invalidate_highlights(); screen_invalidate_highlights();
} else { } else {
kv_destroy(attr_entries); kv_destroy(attr_entries);
map_free(HlEntry, int)(attr_entry_ids); map_destroy(HlEntry, int)(&attr_entry_ids);
map_free(int, int)(combine_attr_entries); map_destroy(int, int)(&combine_attr_entries);
map_free(int, int)(blend_attr_entries); map_destroy(int, int)(&blend_attr_entries);
map_free(int, int)(blendthrough_attr_entries); map_destroy(int, int)(&blendthrough_attr_entries);
map_free(ColorKey, ColorItem)(ns_hl); map_destroy(ColorKey, ColorItem)(&ns_hl);
} }
} }
void hl_invalidate_blends(void) void hl_invalidate_blends(void)
{ {
map_clear(int, int)(blend_attr_entries); map_clear(int, int)(&blend_attr_entries);
map_clear(int, int)(blendthrough_attr_entries); map_clear(int, int)(&blendthrough_attr_entries);
highlight_changed(); highlight_changed();
update_window_hl(curwin, true); update_window_hl(curwin, true);
} }
@@ -437,7 +431,7 @@ int hl_combine_attr(int char_attr, int prim_attr)
// TODO(bfredl): could use a struct for clearer intent. // TODO(bfredl): could use a struct for clearer intent.
int combine_tag = (char_attr << 16) + prim_attr; int combine_tag = (char_attr << 16) + prim_attr;
int id = map_get(int, int)(combine_attr_entries, combine_tag); int id = map_get(int, int)(&combine_attr_entries, combine_tag);
if (id > 0) { if (id > 0) {
return id; return id;
} }
@@ -494,7 +488,7 @@ int hl_combine_attr(int char_attr, int prim_attr)
id = get_attr_entry((HlEntry){ .attr = new_en, .kind = kHlCombine, id = get_attr_entry((HlEntry){ .attr = new_en, .kind = kHlCombine,
.id1 = char_attr, .id2 = prim_attr }); .id1 = char_attr, .id2 = prim_attr });
if (id > 0) { if (id > 0) {
map_put(int, int)(combine_attr_entries, combine_tag, id); map_put(int, int)(&combine_attr_entries, combine_tag, id);
} }
return id; return id;
@@ -550,8 +544,8 @@ int hl_blend_attrs(int back_attr, int front_attr, bool *through)
int combine_tag = (back_attr << 16) + front_attr; int combine_tag = (back_attr << 16) + front_attr;
Map(int, int) *map = (*through Map(int, int) *map = (*through
? blendthrough_attr_entries ? &blendthrough_attr_entries
: blend_attr_entries); : &blend_attr_entries);
int id = map_get(int, int)(map, combine_tag); int id = map_get(int, int)(map, combine_tag);
if (id > 0) { if (id > 0) {
return id; return id;

View File

@@ -166,7 +166,6 @@ void early_init(mparm_T *paramp)
init_path(argv0 ? argv0 : "nvim"); init_path(argv0 ? argv0 : "nvim");
init_normal_cmds(); // Init the table of Normal mode commands. init_normal_cmds(); // Init the table of Normal mode commands.
highlight_init(); highlight_init();
syntax_init();
#ifdef WIN32 #ifdef WIN32
OSVERSIONINFO ovi; OSVERSIONINFO ovi;

View File

@@ -67,6 +67,11 @@
xfree(map); \ xfree(map); \
} \ } \
\ \
void map_##T##_##U##_destroy(Map(T, U) *map) \
{ \
kh_dealloc(T##_##U##_map, &map->table); \
} \
\
U map_##T##_##U##_get(Map(T, U) *map, T key) \ U map_##T##_##U##_get(Map(T, U) *map, T key) \
{ \ { \
khiter_t k; \ khiter_t k; \

View File

@@ -23,6 +23,7 @@
\ \
Map(T, U) *map_##T##_##U##_new(void); \ Map(T, U) *map_##T##_##U##_new(void); \
void map_##T##_##U##_free(Map(T, U) *map); \ void map_##T##_##U##_free(Map(T, U) *map); \
void map_##T##_##U##_destroy(Map(T, U) *map); \
U map_##T##_##U##_get(Map(T, U) *map, T key); \ U map_##T##_##U##_get(Map(T, U) *map, T key); \
bool map_##T##_##U##_has(Map(T, U) *map, T key); \ bool map_##T##_##U##_has(Map(T, U) *map, T key); \
T map_##T##_##U##_key(Map(T, U) *map, T key); \ T map_##T##_##U##_key(Map(T, U) *map, T key); \
@@ -58,8 +59,12 @@ MAP_DECLS(String, handle_T)
MAP_DECLS(ColorKey, ColorItem) MAP_DECLS(ColorKey, ColorItem)
#define MAP_INIT { { 0, 0, 0, 0, NULL, NULL, NULL } }
#define map_init(k, v, map) (*(map) = (Map(k, v))MAP_INIT)
#define map_new(T, U) map_##T##_##U##_new #define map_new(T, U) map_##T##_##U##_new
#define map_free(T, U) map_##T##_##U##_free #define map_free(T, U) map_##T##_##U##_free
#define map_destroy(T, U) map_##T##_##U##_destroy
#define map_get(T, U) map_##T##_##U##_get #define map_get(T, U) map_##T##_##U##_get
#define map_has(T, U) map_##T##_##U##_has #define map_has(T, U) map_##T##_##U##_has
#define map_key(T, U) map_##T##_##U##_key #define map_key(T, U) map_##T##_##U##_key

View File

@@ -91,7 +91,7 @@ typedef struct hl_group {
// builtin |highlight-groups| // builtin |highlight-groups|
static garray_T highlight_ga = GA_EMPTY_INIT_VALUE; static garray_T highlight_ga = GA_EMPTY_INIT_VALUE;
Map(cstr_t, int) *highlight_unames; Map(cstr_t, int) highlight_unames = MAP_INIT;
static inline struct hl_group * HL_TABLE(void) static inline struct hl_group * HL_TABLE(void)
{ {
@@ -385,11 +385,6 @@ static int current_line_id = 0; // unique number for current line
static int syn_time_on = FALSE; static int syn_time_on = FALSE;
# define IF_SYN_TIME(p) (p) # define IF_SYN_TIME(p) (p)
void syntax_init(void)
{
highlight_unames = map_new(cstr_t, int)();
}
// Set the timeout used for syntax highlighting. // Set the timeout used for syntax highlighting.
// Use NULL to reset, no timeout. // Use NULL to reset, no timeout.
void syn_set_timeout(proftime_T *tm) void syn_set_timeout(proftime_T *tm)
@@ -7119,7 +7114,7 @@ void free_highlight(void)
xfree(HL_TABLE()[i].sg_name_u); xfree(HL_TABLE()[i].sg_name_u);
} }
ga_clear(&highlight_ga); ga_clear(&highlight_ga);
map_free(cstr_t, int)(highlight_unames); map_destroy(cstr_t, int)(&highlight_unames);
} }
#endif #endif
@@ -7505,7 +7500,7 @@ int syn_name2id_len(const char_u *name, size_t len)
// map_get(..., int) returns 0 when no key is present, which is // map_get(..., int) returns 0 when no key is present, which is
// the expected value for missing highlight group. // the expected value for missing highlight group.
return map_get(cstr_t, int)(highlight_unames, name_u); return map_get(cstr_t, int)(&highlight_unames, name_u);
} }
/// Lookup a highlight group name and return its attributes. /// Lookup a highlight group name and return its attributes.
@@ -7609,7 +7604,7 @@ static int syn_add_group(char_u *name)
int id = highlight_ga.ga_len; // ID is index plus one int id = highlight_ga.ga_len; // ID is index plus one
map_put(cstr_t, int)(highlight_unames, name_up, id); map_put(cstr_t, int)(&highlight_unames, name_up, id);
return id; return id;
} }
@@ -7620,7 +7615,7 @@ static void syn_unadd_group(void)
{ {
highlight_ga.ga_len--; highlight_ga.ga_len--;
HlGroup *item = &HL_TABLE()[highlight_ga.ga_len]; HlGroup *item = &HL_TABLE()[highlight_ga.ga_len];
map_del(cstr_t, int)(highlight_unames, item->sg_name_u); map_del(cstr_t, int)(&highlight_unames, item->sg_name_u);
xfree(item->sg_name); xfree(item->sg_name);
xfree(item->sg_name_u); xfree(item->sg_name_u);
} }