kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

commit 94e642e38295faa1c2724aaa0cd7a1d7ff0e4e2d
parent f368c7f86254e694ed3e3f4eafdc04e8512f8f67
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Sat, 13 Jun 2026 21:30:00 -0700

perf(lex): right-size per-file-open struct memset (PERF §4.1 #4 Cut B)

lex_open_mem_sym zeroed all ~288B of struct Lexer on every open (~6,000 per
sqlite compile), but lex_point_at immediately overwrites every scalar
(cur/end/line/line_start/dstate/at_bol/had_space) and lex_fold_splices sets
src/len/owns_src/splices/nsplices/next_splice. The only field that genuinely
needs the zero is punct_sym[PUNCT_SLOTS] (slot value 0 = not-yet-interned
sentinel). c/heap/file_id are assigned explicitly right after.

Replace the full-struct memset with memset(l->punct_sym, 0, sizeof
l->punct_sym). Audited: every other field is assigned before first read.

Gate: perf-gate byte-identical (60/60); sqlite -c object + -E text
byte-identical golden-vs-candidate.

Diffstat:
Mlang/cpp/lex/lex.c | 9++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lang/cpp/lex/lex.c b/lang/cpp/lex/lex.c @@ -372,7 +372,14 @@ Lexer* lex_open_mem_sym(Compiler* c, Sym name, const char* src, size_t len) { Heap* h = (Heap*)kit_compiler_context(c)->heap; Lexer* l = (Lexer*)h->alloc(h, sizeof(*l), _Alignof(Lexer)); if (!l) return NULL; - memset(l, 0, sizeof(*l)); + /* Right-sized zero-init: only punct_sym genuinely needs zeroing (slot value 0 + * is the not-yet-interned sentinel; see punct_spelling). Every other field is + * assigned before its first read below — c/heap/file_id explicitly here, then + * src/len/owns_src/splices/nsplices/next_splice via lex_fold_splices and + * cur/end/line/line_start/dstate/at_bol/had_space via lex_point_at. This + * replaces a full ~288B struct memset (run on ~6,000 opens) with a ~196B + * punct_sym clear. */ + memset(l->punct_sym, 0, sizeof l->punct_sym); l->c = c; l->heap = h; l->file_id = 0;