perf(ui): remove spurious allocations from mode_style_array()

This commit is contained in:
bfredl
2022-06-21 16:53:47 +02:00
parent a9442c532e
commit 8cd94e3bc0
2 changed files with 26 additions and 22 deletions

View File

@@ -43,43 +43,45 @@ cursorentry_T shape_table[SHAPE_IDX_COUNT] =
}; };
/// Converts cursor_shapes into an Array of Dictionaries /// Converts cursor_shapes into an Array of Dictionaries
/// @param arena initialized arena where memory will be alocated
///
/// @return Array of the form {[ "cursor_shape": ... ], ...} /// @return Array of the form {[ "cursor_shape": ... ], ...}
Array mode_style_array(void) Array mode_style_array(Arena *arena)
{ {
Array all = ARRAY_DICT_INIT; Array all = arena_array(arena, SHAPE_IDX_COUNT);
for (int i = 0; i < SHAPE_IDX_COUNT; i++) { for (int i = 0; i < SHAPE_IDX_COUNT; i++) {
Dictionary dic = ARRAY_DICT_INIT;
cursorentry_T *cur = &shape_table[i]; cursorentry_T *cur = &shape_table[i];
Dictionary dic = arena_dict(arena, 3 + ((cur->used_for & SHAPE_CURSOR) ? 9 : 0));
PUT_C(dic, "name", STRING_OBJ(cstr_as_string(cur->full_name)));
PUT_C(dic, "short_name", STRING_OBJ(cstr_as_string(cur->name)));
if (cur->used_for & SHAPE_MOUSE) { if (cur->used_for & SHAPE_MOUSE) {
PUT(dic, "mouse_shape", INTEGER_OBJ(cur->mshape)); PUT_C(dic, "mouse_shape", INTEGER_OBJ(cur->mshape));
} }
if (cur->used_for & SHAPE_CURSOR) { if (cur->used_for & SHAPE_CURSOR) {
String shape_str; String shape_str;
switch (cur->shape) { switch (cur->shape) {
case SHAPE_BLOCK: case SHAPE_BLOCK:
shape_str = cstr_to_string("block"); break; shape_str = cstr_as_string("block"); break;
case SHAPE_VER: case SHAPE_VER:
shape_str = cstr_to_string("vertical"); break; shape_str = cstr_as_string("vertical"); break;
case SHAPE_HOR: case SHAPE_HOR:
shape_str = cstr_to_string("horizontal"); break; shape_str = cstr_as_string("horizontal"); break;
default: default:
shape_str = cstr_to_string("unknown"); shape_str = cstr_as_string("unknown");
} }
PUT(dic, "cursor_shape", STRING_OBJ(shape_str)); PUT_C(dic, "cursor_shape", STRING_OBJ(shape_str));
PUT(dic, "cell_percentage", INTEGER_OBJ(cur->percentage)); PUT_C(dic, "cell_percentage", INTEGER_OBJ(cur->percentage));
PUT(dic, "blinkwait", INTEGER_OBJ(cur->blinkwait)); PUT_C(dic, "blinkwait", INTEGER_OBJ(cur->blinkwait));
PUT(dic, "blinkon", INTEGER_OBJ(cur->blinkon)); PUT_C(dic, "blinkon", INTEGER_OBJ(cur->blinkon));
PUT(dic, "blinkoff", INTEGER_OBJ(cur->blinkoff)); PUT_C(dic, "blinkoff", INTEGER_OBJ(cur->blinkoff));
PUT(dic, "hl_id", INTEGER_OBJ(cur->id)); PUT_C(dic, "hl_id", INTEGER_OBJ(cur->id));
PUT(dic, "id_lm", INTEGER_OBJ(cur->id_lm)); PUT_C(dic, "id_lm", INTEGER_OBJ(cur->id_lm));
PUT(dic, "attr_id", INTEGER_OBJ(cur->id ? syn_id2attr(cur->id) : 0)); PUT_C(dic, "attr_id", INTEGER_OBJ(cur->id ? syn_id2attr(cur->id) : 0));
PUT(dic, "attr_id_lm", INTEGER_OBJ(cur->id_lm ? syn_id2attr(cur->id_lm) : 0)); PUT_C(dic, "attr_id_lm", INTEGER_OBJ(cur->id_lm ? syn_id2attr(cur->id_lm) : 0));
} }
PUT(dic, "name", STRING_OBJ(cstr_to_string(cur->full_name)));
PUT(dic, "short_name", STRING_OBJ(cstr_to_string(cur->name)));
ADD(all, DICTIONARY_OBJ(dic)); ADD_C(all, DICTIONARY_OBJ(dic));
} }
return all; return all;

View File

@@ -507,10 +507,12 @@ void ui_flush(void)
pending_cursor_update = false; pending_cursor_update = false;
} }
if (pending_mode_info_update) { if (pending_mode_info_update) {
Array style = mode_style_array(); Arena arena = ARENA_EMPTY;
arena_start(&arena, &ui_ext_fixblk);
Array style = mode_style_array(&arena);
bool enabled = (*p_guicursor != NUL); bool enabled = (*p_guicursor != NUL);
ui_call_mode_info_set(enabled, style); ui_call_mode_info_set(enabled, style);
api_free_array(style); arena_mem_free(arena_finish(&arena), &ui_ext_fixblk);
pending_mode_info_update = false; pending_mode_info_update = false;
} }
if (pending_mode_update && !starting) { if (pending_mode_update && !starting) {