@@ -245,7 +245,7 @@ static void py_register_instance_field(PyLSPContext *ctx, const char *class_qn,
245245static void py_bind_for_target (PyLSPContext * ctx , TSNode left , const CBMType * elem_type );
246246static void py_register_lambda (PyLSPContext * ctx , const char * name , TSNode lambda_node );
247247static 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 );
249249static const char * py_lookup_dict_dispatch (PyLSPContext * ctx , const char * var , const char * key );
250250static void py_process_function (PyLSPContext * ctx , TSNode func_node , const char * container_qn );
251251static 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 );
0 commit comments