Skip to content

Commit 6ef8155

Browse files
authored
Merge pull request DeusData#2422 from Fieldnote-Echo/fix/py-definitions-oom
fix(py-lsp): walk each lambda body at most once per call chain
2 parents aaf9021 + 5be2e88 commit 6ef8155

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static void py_register_instance_field(PyLSPContext *ctx, const char *class_qn,
245245
static void py_bind_for_target(PyLSPContext *ctx, TSNode left, const CBMType *elem_type);
246246
static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambda_node);
247247
static void py_register_dict_literal(PyLSPContext *ctx, const char *name, TSNode dict_node);
248-
static TSNode py_lookup_lambda(PyLSPContext *ctx, const char *name);
248+
static int py_lookup_lambda(PyLSPContext *ctx, const char *name);
249249
static const char *py_lookup_dict_dispatch(PyLSPContext *ctx, const char *var, const char *key);
250250
static void py_process_function(PyLSPContext *ctx, TSNode func_node, const char *container_qn);
251251
static void py_process_class(PyLSPContext *ctx, TSNode class_node);
@@ -895,19 +895,21 @@ static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambd
895895
}
896896
ctx->lambdas[ctx->lambda_count].name = cbm_arena_strdup(ctx->arena, name);
897897
ctx->lambdas[ctx->lambda_count].lambda_node = lambda_node;
898+
ctx->lambdas[ctx->lambda_count].expanding = false;
898899
ctx->lambda_count++;
899900
}
900901

901-
static TSNode py_lookup_lambda(PyLSPContext *ctx, const char *name) {
902-
TSNode null_node = {0};
902+
/* Registry index of the lambda bound to `name`, or -1. An index, not a
903+
* pointer: the registry array is replaced when it grows. */
904+
static int py_lookup_lambda(PyLSPContext *ctx, const char *name) {
903905
if (!ctx || !name)
904-
return null_node;
906+
return -1;
905907
for (int i = 0; i < ctx->lambda_count; i++) {
906908
if (ctx->lambdas[i].name && strcmp(ctx->lambdas[i].name, name) == 0) {
907-
return ctx->lambdas[i].lambda_node;
909+
return i;
908910
}
909911
}
910-
return null_node;
912+
return -1;
911913
}
912914

913915
/* Helper for stripping quotes from a tree-sitter `string` node text. */
@@ -3157,12 +3159,18 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
31573159
/* These legacy entries have no lexical frame identity. During class
31583160
* execution, leave lambda replay unresolved instead of bypassing a
31593161
* local shadow or running the lambda against class locals. */
3160-
TSNode lambda_node =
3161-
ctx->class_body_outer_scope ? (TSNode){0} : py_lookup_lambda(ctx, fname);
3162-
if (!ts_node_is_null(lambda_node)) {
3162+
int lambda_idx = ctx->class_body_outer_scope ? -1 : py_lookup_lambda(ctx, fname);
3163+
if (lambda_idx >= 0) {
3164+
TSNode lambda_node = ctx->lambdas[lambda_idx].lambda_node;
31633165
TSNode params = ts_node_child_by_field_name(lambda_node, "parameters", 10);
31643166
TSNode body = ts_node_child_by_field_name(lambda_node, "body", 4);
31653167
TSNode args = ts_node_child_by_field_name(call_node, "arguments", 9);
3168+
// A lambda that reaches itself again (directly or through another
3169+
// lambda) would re-walk its body once per nesting level up to the
3170+
// walk-depth cap, and two such calls in one body double the work
3171+
// per level. Walk each body at most once per chain.
3172+
if (ctx->lambdas[lambda_idx].expanding)
3173+
return;
31663174
CBMScope *saved = ctx->current_scope;
31673175
ctx->current_scope = py_scope_push_checked(ctx);
31683176
// Bind each lambda param to the call-site arg's type.
@@ -3195,7 +3203,9 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
31953203
ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.<lambda>",
31963204
prev_func ? prev_func : ctx->module_qn);
31973205
if (!ts_node_is_null(body)) {
3206+
ctx->lambdas[lambda_idx].expanding = true;
31983207
py_resolve_calls_in(ctx, body);
3208+
ctx->lambdas[lambda_idx].expanding = false;
31993209
}
32003210
ctx->enclosing_func_qn = prev_func;
32013211
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
@@ -2045,6 +2045,56 @@ TEST(pylsp_lambda_rewalk_dedupes_dunder_occurrence) {
20452045
PASS();
20462046
}
20472047

2048+
/* A lambda body is also re-walked for calls made inside another lambda's
2049+
* re-walk, so a lambda that reaches itself again (directly or through another
2050+
* lambda) was re-walked once per nesting level until the walk-depth cap. With
2051+
* two such calls in one body the work doubled per level, and a two-line
2052+
* `fib = lambda n: fib(n - 1) + fib(n - 2)` drove extraction to many GB. A
2053+
* lambda already being re-walked must not be re-entered, so each helper()
2054+
* site below resolves once. The recursion here is linear, so without the
2055+
* guard this stays small and fails the count instead of exhausting memory. */
2056+
TEST(pylsp_recursive_lambda_rewalk_is_bounded) {
2057+
CBMFileResult *r = extract_py("def helper():\n"
2058+
" return 0\n"
2059+
"count = lambda n: helper() if n == 0 else count(n - 1)\n"
2060+
"even = lambda n: helper() if n == 0 else odd(n - 1)\n"
2061+
"odd = lambda n: even(n - 1)\n"
2062+
"count(3)\n"
2063+
"even(4)\n");
2064+
ASSERT_NOT_NULL(r);
2065+
ASSERT_FALSE(r->has_error || r->parse_incomplete);
2066+
2067+
int helper_resolutions = 0;
2068+
for (int i = 0; i < r->resolved_calls.count; i++) {
2069+
const CBMResolvedCall *rc = &r->resolved_calls.items[i];
2070+
if (rc->callee_qn && strstr(rc->callee_qn, ".helper"))
2071+
helper_resolutions++;
2072+
}
2073+
ASSERT_EQ(helper_resolutions, 2);
2074+
2075+
cbm_free_result(r);
2076+
PASS();
2077+
}
2078+
2079+
/* The re-walk guard is released after each walk, so a later call to the same
2080+
* lambda still binds its own argument type: both A.m and B.m resolve. */
2081+
TEST(pylsp_lambda_rewalk_guard_released_between_calls) {
2082+
CBMFileResult *r = extract_py("class A:\n"
2083+
" def m(self):\n"
2084+
" return 1\n"
2085+
"class B:\n"
2086+
" def m(self):\n"
2087+
" return 2\n"
2088+
"call_m = lambda x: x.m()\n"
2089+
"call_m(A())\n"
2090+
"call_m(B())\n");
2091+
ASSERT_NOT_NULL(r);
2092+
ASSERT_GTE(require_resolved(r, "<lambda>", ".A.m"), 0);
2093+
ASSERT_GTE(require_resolved(r, "<lambda>", ".B.m"), 0);
2094+
cbm_free_result(r);
2095+
PASS();
2096+
}
2097+
20482098
/* Parser-backed Python calls have the same occurrence-identity requirement as
20492099
* synthetic dunders. Distinct typed receivers can expose the same method leaf
20502100
* in one caller; each CBMCall must join the semantic record for its own site. */
@@ -4885,6 +4935,8 @@ SUITE(py_lsp) {
48854935
RUN_TEST(pylsp_round5_super_init);
48864936
RUN_TEST(pylsp_dunder_same_leaf_occurrences_join_by_exact_site);
48874937
RUN_TEST(pylsp_lambda_rewalk_dedupes_dunder_occurrence);
4938+
RUN_TEST(pylsp_recursive_lambda_rewalk_is_bounded);
4939+
RUN_TEST(pylsp_lambda_rewalk_guard_released_between_calls);
48884940
RUN_TEST(pylsp_ordinary_same_leaf_calls_join_by_exact_site);
48894941
/* Round 6 — generators, dataclasses, properties */
48904942
RUN_TEST(pylsp_round6_generator_yields_iterable);

0 commit comments

Comments
 (0)