Skip to content

Commit ffa0b32

Browse files
fix(py-lsp): walk each lambda body at most once per call chain
py_emit_call_for re-walked a called lambda's body without tracking lambdas already being walked, so a lambda that reaches itself was re-walked once per nesting level up to the walk-depth cap, doubling with two calls per body. mpmath's calculus/extrapolation.py grows memory at about 1.9 GB/s until it is killed. Mark the registry entry as expanding during its re-walk and skip re-entry. The mpmath file now indexes in 1.1 s at about 80 MB peak RSS; the graph of the Python 3.14 stdlib + site-packages is byte-identical. Signed-off-by: Nelson Spence <nelson@projectnavi.ai>
1 parent 2278498 commit ffa0b32

3 files changed

Lines changed: 72 additions & 8 deletions

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static void py_register_instance_field(PyLSPContext *ctx, const char *class_qn,
165165
static void py_bind_for_target(PyLSPContext *ctx, TSNode left, const CBMType *elem_type);
166166
static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambda_node);
167167
static void py_register_dict_literal(PyLSPContext *ctx, const char *name, TSNode dict_node);
168-
static TSNode py_lookup_lambda(PyLSPContext *ctx, const char *name);
168+
static int py_lookup_lambda(PyLSPContext *ctx, const char *name);
169169
static const char *py_lookup_dict_dispatch(PyLSPContext *ctx, const char *var, const char *key);
170170

171171
/* ── Scope mutation wrappers ─────────────────────────────────────────────
@@ -690,19 +690,21 @@ static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambd
690690
}
691691
ctx->lambdas[ctx->lambda_count].name = cbm_arena_strdup(ctx->arena, name);
692692
ctx->lambdas[ctx->lambda_count].lambda_node = lambda_node;
693+
ctx->lambdas[ctx->lambda_count].expanding = false;
693694
ctx->lambda_count++;
694695
}
695696

696-
static TSNode py_lookup_lambda(PyLSPContext *ctx, const char *name) {
697-
TSNode null_node = {0};
697+
/* Registry index of the lambda bound to `name`, or -1. An index, not a
698+
* pointer: the registry array is replaced when it grows. */
699+
static int py_lookup_lambda(PyLSPContext *ctx, const char *name) {
698700
if (!ctx || !name)
699-
return null_node;
701+
return -1;
700702
for (int i = 0; i < ctx->lambda_count; i++) {
701703
if (ctx->lambdas[i].name && strcmp(ctx->lambdas[i].name, name) == 0) {
702-
return ctx->lambdas[i].lambda_node;
704+
return i;
703705
}
704706
}
705-
return null_node;
707+
return -1;
706708
}
707709

708710
/* Helper for stripping quotes from a tree-sitter `string` node text. */
@@ -2519,11 +2521,18 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
25192521
// Walk the lambda body for any nested call sites with the params
25202522
// bound to the call's arg types. Resolved calls get caller_qn
25212523
// set to a synthetic <lambda> child of the enclosing function.
2522-
TSNode lambda_node = py_lookup_lambda(ctx, fname);
2523-
if (!ts_node_is_null(lambda_node)) {
2524+
int lambda_idx = py_lookup_lambda(ctx, fname);
2525+
if (lambda_idx >= 0) {
2526+
TSNode lambda_node = ctx->lambdas[lambda_idx].lambda_node;
25242527
TSNode params = ts_node_child_by_field_name(lambda_node, "parameters", 10);
25252528
TSNode body = ts_node_child_by_field_name(lambda_node, "body", 4);
25262529
TSNode args = ts_node_child_by_field_name(call_node, "arguments", 9);
2530+
// A lambda that reaches itself again (directly or through another
2531+
// lambda) would re-walk its body once per nesting level up to the
2532+
// walk-depth cap, and two such calls in one body double the work
2533+
// per level. Walk each body at most once per chain.
2534+
if (ctx->lambdas[lambda_idx].expanding)
2535+
return;
25272536
CBMScope *saved = ctx->current_scope;
25282537
ctx->current_scope = py_scope_push_checked(ctx);
25292538
// Bind each lambda param to the call-site arg's type.
@@ -2556,7 +2565,9 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
25562565
ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.<lambda>",
25572566
prev_func ? prev_func : ctx->module_qn);
25582567
if (!ts_node_is_null(body)) {
2568+
ctx->lambdas[lambda_idx].expanding = true;
25592569
py_resolve_calls_in(ctx, body);
2570+
ctx->lambdas[lambda_idx].expanding = false;
25602571
}
25612572
ctx->enclosing_func_qn = prev_func;
25622573
py_scope_restore(ctx, saved);

‎internal/cbm/lsp/py_lsp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
typedef struct {
1414
const char *name; // bound name, e.g. "fn"
1515
TSNode lambda_node; // the lambda AST node
16+
bool expanding; // body is being re-walked for a call site right now
1617
} CBMLambdaEntry;
1718

1819
// Function-as-dict-value entry. When `funcs = {"a": foo, "b": bar}` is

‎tests/test_py_lsp.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,56 @@ TEST(pylsp_lambda_rewalk_dedupes_dunder_occurrence) {
19311931
PASS();
19321932
}
19331933

1934+
/* A lambda body is also re-walked for calls made inside another lambda's
1935+
* re-walk, so a lambda that reaches itself again (directly or through another
1936+
* lambda) was re-walked once per nesting level until the walk-depth cap. With
1937+
* two such calls in one body the work doubled per level, and a two-line
1938+
* `fib = lambda n: fib(n - 1) + fib(n - 2)` drove extraction to many GB. A
1939+
* lambda already being re-walked must not be re-entered, so each helper()
1940+
* site below resolves once. The recursion here is linear, so without the
1941+
* guard this stays small and fails the count instead of exhausting memory. */
1942+
TEST(pylsp_recursive_lambda_rewalk_is_bounded) {
1943+
CBMFileResult *r = extract_py("def helper():\n"
1944+
" return 0\n"
1945+
"count = lambda n: helper() if n == 0 else count(n - 1)\n"
1946+
"even = lambda n: helper() if n == 0 else odd(n - 1)\n"
1947+
"odd = lambda n: even(n - 1)\n"
1948+
"count(3)\n"
1949+
"even(4)\n");
1950+
ASSERT_NOT_NULL(r);
1951+
ASSERT_FALSE(r->has_error || r->parse_incomplete);
1952+
1953+
int helper_resolutions = 0;
1954+
for (int i = 0; i < r->resolved_calls.count; i++) {
1955+
const CBMResolvedCall *rc = &r->resolved_calls.items[i];
1956+
if (rc->callee_qn && strstr(rc->callee_qn, ".helper"))
1957+
helper_resolutions++;
1958+
}
1959+
ASSERT_EQ(helper_resolutions, 2);
1960+
1961+
cbm_free_result(r);
1962+
PASS();
1963+
}
1964+
1965+
/* The re-walk guard is released after each walk, so a later call to the same
1966+
* lambda still binds its own argument type: both A.m and B.m resolve. */
1967+
TEST(pylsp_lambda_rewalk_guard_released_between_calls) {
1968+
CBMFileResult *r = extract_py("class A:\n"
1969+
" def m(self):\n"
1970+
" return 1\n"
1971+
"class B:\n"
1972+
" def m(self):\n"
1973+
" return 2\n"
1974+
"call_m = lambda x: x.m()\n"
1975+
"call_m(A())\n"
1976+
"call_m(B())\n");
1977+
ASSERT_NOT_NULL(r);
1978+
ASSERT_GTE(require_resolved(r, "<lambda>", ".A.m"), 0);
1979+
ASSERT_GTE(require_resolved(r, "<lambda>", ".B.m"), 0);
1980+
cbm_free_result(r);
1981+
PASS();
1982+
}
1983+
19341984
/* Parser-backed Python calls have the same occurrence-identity requirement as
19351985
* synthetic dunders. Distinct typed receivers can expose the same method leaf
19361986
* in one caller; each CBMCall must join the semantic record for its own site. */
@@ -2243,6 +2293,8 @@ SUITE(py_lsp) {
22432293
RUN_TEST(pylsp_round5_super_init);
22442294
RUN_TEST(pylsp_dunder_same_leaf_occurrences_join_by_exact_site);
22452295
RUN_TEST(pylsp_lambda_rewalk_dedupes_dunder_occurrence);
2296+
RUN_TEST(pylsp_recursive_lambda_rewalk_is_bounded);
2297+
RUN_TEST(pylsp_lambda_rewalk_guard_released_between_calls);
22462298
RUN_TEST(pylsp_ordinary_same_leaf_calls_join_by_exact_site);
22472299
/* Round 6 — generators, dataclasses, properties */
22482300
RUN_TEST(pylsp_round6_generator_yields_iterable);

0 commit comments

Comments
 (0)