Files
neovim/src/nvim/map_value_impl.c.h
bfredl 8da986ea87 refactor(grid): change schar_T representation to be more compact
Previously, a screen cell would occupy 28+4=32 bytes per cell
as we always made space for up to MAX_MCO+1 codepoints in a cell.

As an example, even a pretty modest 50*80 screen would consume

50*80*2*32 = 256000, i e a quarter megabyte

With the factor of two due to the TUI side buffer, and even more when
using msg_grid and/or ext_multigrid.

This instead stores a 4-byte union of either:
- a valid UTF-8 sequence up to 4 bytes
- an escape char which is invalid UTF-8 (0xFF) plus a 24-bit index to a
  glyph cache

This avoids allocating space for huge composed glyphs _upfront_, while
still keeping rendering such glyphs reasonably fast (1 hash table lookup
+ one plain index lookup). If the same large glyphs are using repeatedly
on the screen, this is still a net reduction of memory/cache
consumption. The only case which really gets worse is if you blast
the screen full with crazy emojis and zalgo text and even this case
only leads to 4 extra bytes per char.

When only <= 4-byte glyphs are used, plus the 4-byte attribute code,
i e 8 bytes in total there is a factor of four reduction of memory use.
Memory which will be quite hot in cache as the screen buffer is scanned
over in win_line() buffer text drawing

A slight complication is that the representation depends on host byte
order. I've tested this manually by compling and running this
in qemu-s390x and it works fine. We might add a qemu based solution
to CI at some point.
2023-09-19 11:25:31 +02:00

65 lines
1.6 KiB
C

#include "nvim/assert.h"
#include "nvim/map.h"
#if !defined(KEY_NAME) || !defined(VAL_NAME)
// Don't error out. it is nice to type-check the file in isolation, in clangd or otherwise
# define KEY_NAME(x) x##int
# define VAL_NAME(x) quasiquote(x, ptr_t)
#endif
#define MAP_NAME(x) VAL_NAME(KEY_NAME(x))
#define MAP_TYPE MAP_NAME(Map_)
#define KEY_TYPE KEY_NAME()
#define VALUE_TYPE VAL_NAME()
#define INITIALIZER VAL_NAME(value_init_)
VALUE_TYPE *MAP_NAME(map_ref_)(MAP_TYPE *map, KEY_TYPE key, KEY_TYPE **key_alloc)
{
uint32_t k = KEY_NAME(mh_get_)(&map->set, key);
if (k == MH_TOMBSTONE) {
return NULL;
}
if (key_alloc) {
*key_alloc = &map->set.keys[k];
}
return &map->values[k];
}
VALUE_TYPE *MAP_NAME(map_put_ref_)(MAP_TYPE *map, KEY_TYPE key, KEY_TYPE **key_alloc,
bool *new_item)
{
MHPutStatus status;
uint32_t k = KEY_NAME(mh_put_)(&map->set, key, &status);
if (status != kMHExisting) {
if (status == kMHNewKeyRealloc) {
map->values = xrealloc(map->values, map->set.h.keys_capacity * sizeof(VALUE_TYPE));
}
map->values[k] = INITIALIZER;
}
if (new_item) {
*new_item = (status != kMHExisting);
}
if (key_alloc) {
*key_alloc = &map->set.keys[k];
}
return &map->values[k];
}
VALUE_TYPE MAP_NAME(map_del_)(MAP_TYPE *map, KEY_TYPE key, KEY_TYPE *key_alloc)
{
VALUE_TYPE rv = INITIALIZER;
uint32_t k = KEY_NAME(mh_delete_)(&map->set, &key);
if (k == MH_TOMBSTONE) {
return rv;
}
if (key_alloc) {
*key_alloc = key;
}
rv = map->values[k];
if (k != map->set.h.n_keys) {
map->values[k] = map->values[map->set.h.n_keys];
}
return rv;
}