mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-19 23:14:55 +02:00
Use an arena for TextCache as well
This commit is contained in:
@@ -20,10 +20,16 @@ static bool cmpr_chars(Chars a, Chars b);
|
|||||||
#define CMPR_FN cmpr_chars
|
#define CMPR_FN cmpr_chars
|
||||||
#include "kitty-verstable.h"
|
#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 {
|
typedef struct TextCache {
|
||||||
struct { Chars *items; size_t capacity; char_type count; } array;
|
struct { Chars *items; size_t capacity; char_type count; } array;
|
||||||
chars_map map;
|
chars_map map;
|
||||||
unsigned refcnt;
|
unsigned refcnt;
|
||||||
|
CharsMonotonicArena arena;
|
||||||
} TextCache;
|
} TextCache;
|
||||||
static uint64_t hash_chars(Chars k) { return vt_hash_bytes(k.chars, sizeof(k.chars[0]) * k.count); }
|
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; }
|
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;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
tc_clear(TextCache *ans) {
|
|
||||||
ans->array.count = 0;
|
|
||||||
vt_cleanup(&ans->map);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
free_text_cache(TextCache *self) {
|
free_text_cache(TextCache *self) {
|
||||||
vt_cleanup(&self->map);
|
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->array.items);
|
||||||
free(self);
|
free(self);
|
||||||
}
|
}
|
||||||
@@ -128,7 +128,7 @@ static char_type
|
|||||||
copy_and_insert(TextCache *self, const Chars key) {
|
copy_and_insert(TextCache *self, const Chars key) {
|
||||||
if (self->array.count > MAX_CHAR_TYPE_VALUE) fatal("Too many items in TextCache");
|
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);
|
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");
|
if (!copy) fatal("Out of memory");
|
||||||
memcpy(copy, key.chars, key.count * sizeof(key.chars[0]));
|
memcpy(copy, key.chars, key.count * sizeof(key.chars[0]));
|
||||||
char_type ans = self->array.count;
|
char_type ans = self->array.count;
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ typedef struct {int x; } *TextCache;
|
|||||||
TextCache* tc_alloc(void);
|
TextCache* tc_alloc(void);
|
||||||
TextCache* tc_incref(TextCache *self);
|
TextCache* tc_incref(TextCache *self);
|
||||||
TextCache* tc_decref(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);
|
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);
|
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);
|
char_type tc_get_or_insert_chars(TextCache *self, const ListOfChars *chars);
|
||||||
|
|||||||
Reference in New Issue
Block a user