From 4741f7a6dfa144ce65cf3bbdd4a1bc6bee6df2e5 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Tue, 13 Oct 2026 13:36:27 +0300 Subject: Optimize and simplify loops, table lookups, and branchless parsers - Replace sequential colon-counting loop with branchless CTZ bitmask in src/pen.h - Replace linear strchr with 256-bit ASCII punctuation bitmask in src/selection.c - Flatten 3D color cube loop and precompute constants in src/palette.c - Use static 256-byte LUT for hex decoding and bsearch for termcap in src/escape.c - Replace capacity doubling while loops with branchless CLZ pow2 helpers across core - Use direct memset/memcpy in src/snapshot.c - Optimize input digit/byte formatting, alt-prefix path, and make keyboard state optional in src/input.c --- src/snapshot.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/snapshot.c') diff --git a/src/snapshot.c b/src/snapshot.c index dcd1dc0..3869be4 100644 --- a/src/snapshot.c +++ b/src/snapshot.c @@ -107,9 +107,8 @@ static uint32_t arena_add(struct term_snapshot *s, const struct gstore *g, uint3 if (n <= 0) return ' '; if (s->arena_len + (uint32_t)n + 1 > s->arena_cap) { - uint32_t cap = s->arena_cap ? s->arena_cap * 2 : 256; - while (cap < s->arena_len + (uint32_t)n + 1) - cap *= 2; + uint32_t cap = term_grow_cap32(s->arena_cap ? s->arena_cap * 2 : 256, + s->arena_len + (uint32_t)n + 1); uint32_t *a = realloc(s->arena, cap * sizeof(*a)); if (!a) return cps[0]; @@ -228,10 +227,8 @@ static bool term_snapshot_update_inner(struct term_snapshot *s, struct term *t) (size_t)(rows - k) * sizeof(uint32_t)); memmove(s->keys, s->keys + k, (size_t)(rows - k) * sizeof(uint64_t)); - for (int y = rows - k; y < rows; y++) { - s->keys[y] = 0; - s->row_clusters[y] = 0; - } + memset(s->keys + (rows - k), 0, (size_t)k * sizeof(uint64_t)); + memset(s->row_clusters + (rows - k), 0, (size_t)k * sizeof(uint32_t)); } for (int y = 0; y < rows; y++) { @@ -340,7 +337,8 @@ int term_snapshot_cluster(const struct term_snapshot *s, uint32_t text, uint32_t if (off >= s->arena_len) return 0; int n = (int)s->arena[off]; - for (int i = 0; i < n && i < cap; i++) - cps[i] = s->arena[off + 1 + i]; + int copy = n < cap ? n : cap; + if (copy > 0) + memcpy(cps, &s->arena[off + 1], (size_t)copy * sizeof(*cps)); return n; } -- cgit v1.2.3