From b7e8d6d103d39aefd119e9870fa22755a8a2fb4d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 16 Dec 2024 20:29:27 +0530 Subject: [PATCH] Use an arena for TextCache as well --- kitty/text-cache.c | 16 ++++++++-------- kitty/text-cache.h | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/kitty/text-cache.c b/kitty/text-cache.c index 9339825b0..807d90700 100644 --- a/kitty/text-cache.c +++ b/kitty/text-cache.c @@ -20,10 +20,16 @@ static bool cmpr_chars(Chars a, Chars b); #define CMPR_FN cmpr_chars #include "kitty-verstable.h" +#define MA_NAME Chars +#define MA_BLOCK_SIZE 16u +#define MA_ARENA_NUM_BLOCKS 128u +#include "arena.h" + typedef struct TextCache { struct { Chars *items; size_t capacity; char_type count; } array; chars_map map; unsigned refcnt; + CharsMonotonicArena arena; } TextCache; static uint64_t hash_chars(Chars k) { return vt_hash_bytes(k.chars, sizeof(k.chars[0]) * k.count); } static bool cmpr_chars(Chars a, Chars b) { return a.count == b.count && memcmp(a.chars, b.chars, sizeof(a.chars[0]) * a.count) == 0; } @@ -43,16 +49,10 @@ tc_alloc(void) { return ans; } -void -tc_clear(TextCache *ans) { - ans->array.count = 0; - vt_cleanup(&ans->map); -} - static void free_text_cache(TextCache *self) { vt_cleanup(&self->map); - for (char_type i = 0; i < self->array.count; i++) free((char_type*)self->array.items[i].chars); + Chars_free_all(&self->arena); free(self->array.items); free(self); } @@ -128,7 +128,7 @@ static char_type copy_and_insert(TextCache *self, const Chars key) { if (self->array.count > MAX_CHAR_TYPE_VALUE) fatal("Too many items in TextCache"); ensure_space_for(&(self->array), items, Chars, self->array.count + 1, capacity, 256, false); - char_type *copy = malloc(key.count * sizeof(key.chars[0])); + char_type *copy = Chars_get(&self->arena, key.count * sizeof(key.chars[0])); if (!copy) fatal("Out of memory"); memcpy(copy, key.chars, key.count * sizeof(key.chars[0])); char_type ans = self->array.count; diff --git a/kitty/text-cache.h b/kitty/text-cache.h index 0e8224315..81de0f4cd 100644 --- a/kitty/text-cache.h +++ b/kitty/text-cache.h @@ -48,7 +48,6 @@ typedef struct {int x; } *TextCache; TextCache* tc_alloc(void); TextCache* tc_incref(TextCache *self); TextCache* tc_decref(TextCache *self); -void tc_clear(TextCache *ans); void tc_chars_at_index(const TextCache *self, char_type idx, ListOfChars *ans); unsigned tc_chars_at_index_ansi(const TextCache *self, char_type idx, ANSIBuf *output); char_type tc_get_or_insert_chars(TextCache *self, const ListOfChars *chars);