Skip to content

Commit 51770de

Browse files
committed
perf(ts): memoize expression-type evaluation per node
ts_eval_expr_type and ts_signature_for_call are mutually recursive: resolving a call evaluates its argument expressions once per lookup path (method dispatch + namespace fallback), and in tsc-compiled spread files the first argument is itself the next nested Object.assign(...) call — the same subtree re-evaluates once per enclosing level, 2^n total. The TS suite's objectSpreadRepeatedComplexity.js (3.6 KB, 5 nodes) measured 20.4 s; with the memo its eval cost is zero within measurement noise of a one-file control, and the microsoft/TypeScript corpus drops 37.5 -> ~24 s warm (nodes byte-identical, edges within the known scheduler jitter). Expression types are position-pure within a file pass (one node = one scope path; the per-file walk is single-threaded and deterministic), so one eval per node is the correct semantics, not a cache trade-off. The memo is a per-file, arena-backed, linear-probe table keyed on TSNode.id. Results produced under a depth-cap or budget bail are never stored: both bail sites bump a degradation counter, and a store only happens when the subtree completed clean — a degraded UNKNOWN can therefore never shadow a later full evaluation. The regression guard asserts work, not wall-clock: the nested-Object.assign shape must complete without exhausting the deterministic eval budget, read back through a new CBM_ENABLE_TEST_SEAMS accessor pair. The seam lives in the lsp_all unity object, so GRAMMAR_CFLAGS_TEST/TSAN now carry the seams define (test artifacts always have seams; prod never does). Verified RED without the memo (budget exhausted, suite 47.8 s) and green with it (3.2 s). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 459be8b commit 51770de

4 files changed

Lines changed: 179 additions & 3 deletions

File tree

‎Makefile.cbm‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,9 @@ BUILD_DIR = build/c
713713
# Grammar + tree-sitter runtime: compiled without -Werror (upstream code has warnings)
714714
GRAMMAR_CFLAGS = -std=c11 -D_DEFAULT_SOURCE -O2 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC)
715715
GRAMMAR_CFLAGS_TEST = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
716-
$(SANITIZED_DEFINE) $(SANITIZE)
716+
-DCBM_ENABLE_TEST_SEAMS=1 $(SANITIZED_DEFINE) $(SANITIZE)
717717
GRAMMAR_CFLAGS_TSAN = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
718-
-DCBM_SANITIZED_BUILD=1 $(TSAN_SANITIZE)
718+
-DCBM_ENABLE_TEST_SEAMS=1 -DCBM_SANITIZED_BUILD=1 $(TSAN_SANITIZE)
719719

720720
# Object files for grammars + ts_runtime + lsp_all + preprocessor
721721
GRAMMAR_OBJS_TEST = $(patsubst $(CBM_DIR)/%.c,$(BUILD_DIR)/%.o,$(GRAMMAR_SRCS))

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,107 @@ static void ts_type_budget_reset(size_t source_len) {
7070
g_ts_type_budget_warned = false;
7171
}
7272

73+
/* Bumped on every degraded eval return (depth cap or budget exhaustion). An
74+
* evaluation whose subtree bumped this counter must NOT be memoized: its value
75+
* embeds the degradation, and first-eval-wins would freeze it for call sites
76+
* that could have evaluated the node fully. */
77+
static _Thread_local unsigned long g_ts_eval_degraded;
78+
79+
/* Expression-type memo (per file): node.id -> evaluated CBMType.
80+
*
81+
* ts_signature_for_call re-evaluates a call's argument expressions while
82+
* resolving overloads, and boolean/spread chains re-enter the same
83+
* subexpression once per enclosing alternative — the TS suite's
84+
* objectSpreadRepeatedComplexity.js builds a union of 2^(n-1) members and
85+
* measured 11+ s for a 3.6 KB file while emitting the same 5-node graph
86+
* v0.9.0 emits in 0.14 s. Expression types are position-pure within a file
87+
* pass (one node = one scope path, and the per-file walk is single-threaded
88+
* and deterministic), so one eval per node is the correct semantics, not a
89+
* cache trade-off. Degraded results are never stored (see
90+
* g_ts_eval_degraded). Arena-backed, linear-probed, power-of-two capacity;
91+
* grows by rehash and dies with the file pass. */
92+
typedef struct TsEvalMemo {
93+
const void **keys; /* TSNode.id; NULL = empty slot */
94+
const CBMType **vals;
95+
uint32_t cap;
96+
uint32_t count;
97+
} TsEvalMemo;
98+
99+
static uint32_t ts_memo_slot(const TsEvalMemo *m, const void *id) {
100+
uint64_t x = (uint64_t)(uintptr_t)id;
101+
x ^= x >> 33;
102+
x *= 0xff51afd7ed558ccdULL;
103+
x ^= x >> 33;
104+
uint32_t i = (uint32_t)x & (m->cap - 1);
105+
while (m->keys[i] && m->keys[i] != id)
106+
i = (i + 1) & (m->cap - 1);
107+
return i;
108+
}
109+
110+
static const CBMType *ts_memo_get(const TsEvalMemo *m, const void *id) {
111+
if (!m || !m->cap)
112+
return NULL;
113+
uint32_t i = ts_memo_slot(m, id);
114+
return m->keys[i] ? m->vals[i] : NULL;
115+
}
116+
117+
static void ts_memo_put(TSLSPContext *ctx, const void *id, const CBMType *t) {
118+
TsEvalMemo *m = ctx->eval_memo;
119+
if (!m) {
120+
m = (TsEvalMemo *)cbm_arena_alloc(ctx->arena, sizeof(*m));
121+
if (!m)
122+
return;
123+
m->cap = 1024;
124+
m->count = 0;
125+
m->keys = (const void **)cbm_arena_alloc(ctx->arena, m->cap * sizeof(*m->keys));
126+
m->vals = (const CBMType **)cbm_arena_alloc(ctx->arena, m->cap * sizeof(*m->vals));
127+
if (!m->keys || !m->vals)
128+
return;
129+
memset(m->keys, 0, m->cap * sizeof(*m->keys));
130+
memset(m->vals, 0, m->cap * sizeof(*m->vals));
131+
ctx->eval_memo = m;
132+
}
133+
if (m->count * 10 >= m->cap * 7) { /* grow at 70% load */
134+
TsEvalMemo bigger = {0};
135+
bigger.cap = m->cap * 2;
136+
bigger.keys = (const void **)cbm_arena_alloc(ctx->arena, bigger.cap * sizeof(*bigger.keys));
137+
bigger.vals =
138+
(const CBMType **)cbm_arena_alloc(ctx->arena, bigger.cap * sizeof(*bigger.vals));
139+
if (!bigger.keys || !bigger.vals)
140+
return; /* keep serving from the full table; stores stop */
141+
memset(bigger.keys, 0, bigger.cap * sizeof(*bigger.keys));
142+
memset(bigger.vals, 0, bigger.cap * sizeof(*bigger.vals));
143+
for (uint32_t i = 0; i < m->cap; i++) {
144+
if (!m->keys[i])
145+
continue;
146+
uint32_t j = ts_memo_slot(&bigger, m->keys[i]);
147+
bigger.keys[j] = m->keys[i];
148+
bigger.vals[j] = m->vals[i];
149+
}
150+
bigger.count = m->count;
151+
*m = bigger; /* old arrays stay in the arena */
152+
}
153+
uint32_t i = ts_memo_slot(m, id);
154+
if (!m->keys[i]) {
155+
m->keys[i] = id;
156+
m->vals[i] = t;
157+
m->count++;
158+
}
159+
}
160+
161+
#ifdef CBM_ENABLE_TEST_SEAMS
162+
/* Complexity-regression seam: expose the thread's remaining eval budget and
163+
* the warned flag after a cbm_run_ts_lsp call. Budget units consumed are a
164+
* deterministic work counter — the memo turns the spread-bomb class from
165+
* O(2^n) evals into O(nodes), which tests assert without wall-clock. */
166+
long cbm_ts_lsp_test_budget_remaining(void) {
167+
return g_ts_type_budget;
168+
}
169+
bool cbm_ts_lsp_test_budget_warned(void) {
170+
return g_ts_type_budget_warned;
171+
}
172+
#endif
173+
73174
#define TS_LSP_MAX_EVAL_DEPTH 64
74175
#define TS_LSP_FIELD_LEN(s) ((uint32_t)(sizeof(s) - 1))
75176

@@ -1978,8 +2079,17 @@ static const CBMType *return_type_of(CBMArena *arena, const CBMType *sig) {
19782079
const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
19792080
if (!ctx || ts_node_is_null(node))
19802081
return cbm_type_unknown();
1981-
if (ctx->eval_depth > TS_LSP_MAX_EVAL_DEPTH)
2082+
/* Memo hit: O(1), charges no budget, ignores depth — the value came from a
2083+
* completed, non-degraded evaluation of this exact node. */
2084+
{
2085+
const CBMType *memo = ts_memo_get(ctx->eval_memo, node.id);
2086+
if (memo)
2087+
return memo;
2088+
}
2089+
if (ctx->eval_depth > TS_LSP_MAX_EVAL_DEPTH) {
2090+
g_ts_eval_degraded++;
19822091
return cbm_type_unknown();
2092+
}
19832093
/* Depth alone does not bound WORK: crafted expressions (e.g. the TS test
19842094
* suite's repeated object spreads) stay under the depth cap while fanning
19852095
* out exponentially — one 3.6 KB baseline file measured 11 s here. Charge
@@ -1998,10 +2108,14 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
19982108
fprintf(stderr, " [tslsp] expression-eval budget exhausted; degrading to "
19992109
"unknown\n");
20002110
}
2111+
g_ts_eval_degraded++;
20012112
return cbm_type_unknown();
20022113
}
20032114
}
20042115
ctx->eval_depth++;
2116+
/* Snapshot for the memo-store guard: any degraded return in this subtree
2117+
* bumps the counter and blocks the store below. */
2118+
unsigned long degraded_before = g_ts_eval_degraded;
20052119

20062120
const CBMType *result = cbm_type_unknown();
20072121
const char *kind = ts_node_type(node);
@@ -2261,6 +2375,11 @@ const CBMType *ts_eval_expr_type(TSLSPContext *ctx, TSNode node) {
22612375
}
22622376

22632377
ctx->eval_depth--;
2378+
/* Store only completed, non-degraded evaluations (see g_ts_eval_degraded);
2379+
* result is never NULL here (initialized to unknown), so NULL stays the
2380+
* empty-slot sentinel. */
2381+
if (result && g_ts_eval_degraded == degraded_before)
2382+
ts_memo_put(ctx, node.id, result);
22642383
return result;
22652384
}
22662385

‎internal/cbm/lsp/ts_lsp.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,23 @@ typedef struct {
6161

6262
// Recursion guard for ts_eval_expr_type (mirrors c_lsp).
6363
int eval_depth;
64+
// Expression-type memo: node.id -> evaluated type (lazily created on the
65+
// first completed eval; see TsEvalMemo in ts_lsp.c). Kills the exponential
66+
// re-evaluation of shared subexpressions under overload resolution.
67+
struct TsEvalMemo *eval_memo;
6468
// Recursion guard for lookup_member_type: cyclic type graphs (mutually
6569
// recursive unions/wrappers across registered types) otherwise recurse
6670
// without bound — stack overflow on real repos.
6771
int member_depth;
6872
} TSLSPContext;
6973

74+
#ifdef CBM_ENABLE_TEST_SEAMS
75+
// Complexity-regression seam: remaining expression-eval budget / warned flag
76+
// for the calling thread, valid after a cbm_run_ts_lsp on the same thread.
77+
long cbm_ts_lsp_test_budget_remaining(void);
78+
bool cbm_ts_lsp_test_budget_warned(void);
79+
#endif
80+
7081
// --- Initialization ---
7182

7283
// Initialise a TSLSPContext for processing one file. Mode flags select dialect.

‎tests/test_ts_lsp.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,6 +4131,49 @@ TEST(tslsp_nested_class_resolution) {
41314131
/* JavaScript relies on local `new` inference here, while TypeScript and TSX use
41324132
* explicit parameter types below. All three parser modes must preserve the
41334133
* full ordinary call-expression span, independent of JSX-specific behavior. */
4134+
/* ── Expression-eval complexity guard ─────────────────────────────────────────
4135+
*
4136+
* The objectSpreadRepeatedComplexity class (TS conformance suite): a chain of
4137+
* `...(c[i] && c[j] && {...})` spreads makes the union fan out 2^(n-1) ways,
4138+
* and overload resolution re-evaluates shared subexpressions once per
4139+
* alternative. The per-node memo turns that into one eval per node. Guarded
4140+
* via the deterministic budget counter, not wall-clock: without the memo this
4141+
* source burns the entire eval budget (warned=true); with it, consumption
4142+
* stays around the node count. */
4143+
TEST(tslsp_eval_memo_bounds_spread_bomb_work) {
4144+
/* The tsc-compiled form of that file: nested Object.assign(...) member
4145+
* calls. Resolving the stdlib method evaluates every argument expression
4146+
* once per lookup path (method dispatch + namespace fallback), and the
4147+
* first argument is itself the next nested call — without the memo the
4148+
* shared subtree re-walks once per enclosing level: 2^n evals. */
4149+
char src[16384];
4150+
int off = snprintf(src, sizeof(src), "\"use strict\";\nfunction f(cnd) {\n return ");
4151+
for (int i = 18; i >= 0; i--) {
4152+
off += snprintf(src + off, sizeof(src) - (size_t)off, "Object.assign(");
4153+
}
4154+
off += snprintf(src + off, sizeof(src) - (size_t)off, "{}");
4155+
for (int i = 0; i < 19; i++) {
4156+
off += snprintf(src + off, sizeof(src) - (size_t)off,
4157+
", (cnd[%d] && cnd[%d] && {\n prop%da: 1,\n prop%db: 1,\n"
4158+
" }))",
4159+
2 * i + 1, 2 * i + 2, i, i);
4160+
}
4161+
snprintf(src + off, sizeof(src) - (size_t)off, ";\n}\n");
4162+
4163+
CBMFileResult *r = extract_js(src);
4164+
ASSERT_NOT_NULL(r);
4165+
ASSERT_GTE(r->defs.count, 1); /* f extracted; the graph itself is tiny */
4166+
4167+
/* Same thread ran the eval: the seam reads its final budget state. */
4168+
ASSERT_FALSE(cbm_ts_lsp_test_budget_warned());
4169+
long budget0 = 1000000 + (long)strlen(src) * 64;
4170+
long consumed = budget0 - cbm_ts_lsp_test_budget_remaining();
4171+
ASSERT_LT(consumed, 100000);
4172+
4173+
cbm_free_result(r);
4174+
PASS();
4175+
}
4176+
41344177
TEST(tslsp_js_ordinary_same_leaf_calls_join_by_exact_site) {
41354178
static const char source[] = "class Alpha { render() {} }\n"
41364179
"class Beta { render() {} }\n"
@@ -4641,6 +4684,9 @@ SUITE(ts_lsp) {
46414684
RUN_TEST(tslsp_void_returning_method);
46424685
RUN_TEST(tslsp_nested_class_resolution);
46434686

4687+
/* Expression-eval complexity guard. */
4688+
RUN_TEST(tslsp_eval_memo_bounds_spread_bomb_work);
4689+
46444690
/* Ordinary same-leaf occurrence identity (JS / TS / TSX). */
46454691
RUN_TEST(tslsp_js_ordinary_same_leaf_calls_join_by_exact_site);
46464692
RUN_TEST(tslsp_ts_ordinary_same_leaf_calls_join_by_exact_site);

0 commit comments

Comments
 (0)