@@ -49,36 +49,6 @@ static void walk_usages(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec
4949static bool is_direct_argument_value (TSNode node );
5050static TSNode python_direct_callable_attribute_site (TSNode node );
5151
52- // Check if a node is inside a call expression (to avoid double-counting as usage)
53- static bool is_inside_call (TSNode node , const CBMLangSpec * spec ) {
54- TSNode cur = ts_node_parent (node );
55- while (!ts_node_is_null (cur )) {
56- if (cbm_kind_in_set (cur , spec -> call_node_types )) {
57- return true;
58- }
59- cur = ts_node_parent (cur );
60- }
61- return false;
62- }
63-
64- // Check if a node is inside an import statement
65- static bool is_inside_import (TSNode node , const CBMLangSpec * spec ) {
66- bool has_imports = spec -> import_node_types && spec -> import_node_types [0 ];
67- bool has_from_imports = spec -> import_from_types && spec -> import_from_types [0 ];
68- if (!has_imports && !has_from_imports ) {
69- return false;
70- }
71- TSNode cur = ts_node_parent (node );
72- while (!ts_node_is_null (cur )) {
73- if ((has_imports && cbm_kind_in_set (cur , spec -> import_node_types )) ||
74- (has_from_imports && cbm_kind_in_set (cur , spec -> import_from_types ))) {
75- return true;
76- }
77- cur = ts_node_parent (cur );
78- }
79- return false;
80- }
81-
8252// Is this an identifier-like node that represents a reference?
8353static bool is_reference_node (TSNode node , CBMLanguage lang ) {
8454 const char * kind = ts_node_type (node );
@@ -2426,7 +2396,8 @@ static bool emit_direct_perl_coderef_usage(CBMExtractCtx *ctx, TSNode node,
24262396}
24272397
24282398// Try to emit a usage for a reference node. Returns early if the node should be skipped.
2429- static void try_emit_usage (CBMExtractCtx * ctx , TSNode node , const CBMLangSpec * spec ) {
2399+ static void try_emit_usage (CBMExtractCtx * ctx , TSNode node , const CBMLangSpec * spec ,
2400+ bool inside_call , bool inside_import ) {
24302401 if (emit_direct_perl_coderef_usage (ctx , node , cbm_enclosing_func_qn_cached (ctx , node ), 0 )) {
24312402 return ;
24322403 }
@@ -2439,7 +2410,7 @@ static void try_emit_usage(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
24392410 if (is_call_argument_label (node )) {
24402411 return ;
24412412 }
2442- if (is_inside_call ( node , spec ) || is_inside_import ( node , spec ) ) {
2413+ if (inside_call || inside_import ) {
24432414 return ;
24442415 }
24452416 if (is_binding_occurrence (ctx , node , spec , NULL ) ||
@@ -2456,18 +2427,79 @@ static void try_emit_usage(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
24562427 }
24572428}
24582429
2459- // Iterative usage walker — explicit stack
2430+ // Iterative usage walker — explicit stack.
2431+ //
2432+ // The call/import ancestry that gates usage emission is maintained as ENTER/
2433+ // EXIT counters on the walk instead of per-node ancestor re-walks: the old
2434+ // is_inside_call/is_inside_import helpers climbed every ancestor via
2435+ // ts_node_parent, and tree-sitter's ts_node_parent RE-DESCENDS from the root
2436+ // scanning siblings — O(depth x sibling-position) per node, which went
2437+ // quadratic on wide nodes (a 1,536-argument call in dotnet/runtime's JIT
2438+ // torture tests put 92% of extract time into these walks; 490 s for one
2439+ // 147 KB file). Counter semantics match the helpers exactly: strict ancestors
2440+ // only — a node is emitted BEFORE its own kind increments the counters, so a
2441+ // call node itself does not count as "inside a call".
24602442static void walk_usages (CBMExtractCtx * ctx , TSNode root , const CBMLangSpec * spec ) {
2461- TSNodeStack stack ;
2462- ts_nstack_init (& stack , ctx -> arena , 4096 );
2463- ts_nstack_push (& stack , ctx -> arena , root );
2464-
2465- while (stack .count > 0 ) {
2466- TSNode node = ts_nstack_pop (& stack );
2467- try_emit_usage (ctx , node , spec );
2468- uint32_t count = ts_node_child_count (node );
2469- for (int i = (int )count - LAST_IDX ; i >= 0 ; i -- ) {
2470- ts_nstack_push (& stack , ctx -> arena , ts_node_child (node , (uint32_t )i ));
2443+ typedef struct {
2444+ TSNode node ;
2445+ uint32_t next_child ;
2446+ bool counts_call ;
2447+ bool counts_import ;
2448+ } UsageFrame ;
2449+ int cap = 256 ;
2450+ UsageFrame * frames = (UsageFrame * )cbm_arena_alloc (ctx -> arena , (size_t )cap * sizeof (* frames ));
2451+ if (!frames ) {
2452+ return ;
2453+ }
2454+ bool has_imports = spec -> import_node_types && spec -> import_node_types [0 ];
2455+ bool has_from_imports = spec -> import_from_types && spec -> import_from_types [0 ];
2456+ int call_depth = 0 ;
2457+ int import_depth = 0 ;
2458+ int top = 0 ;
2459+ frames [top ++ ] = (UsageFrame ){root , 0 , false, false};
2460+ bool entering = true;
2461+
2462+ while (top > 0 ) {
2463+ UsageFrame * f = & frames [top - 1 ];
2464+ if (entering ) {
2465+ try_emit_usage (ctx , f -> node , spec , call_depth > 0 , import_depth > 0 );
2466+ f -> counts_call = cbm_kind_in_set (f -> node , spec -> call_node_types );
2467+ f -> counts_import =
2468+ (has_imports && cbm_kind_in_set (f -> node , spec -> import_node_types )) ||
2469+ (has_from_imports && cbm_kind_in_set (f -> node , spec -> import_from_types ));
2470+ if (f -> counts_call ) {
2471+ call_depth ++ ;
2472+ }
2473+ if (f -> counts_import ) {
2474+ import_depth ++ ;
2475+ }
2476+ }
2477+ uint32_t count = ts_node_child_count (f -> node );
2478+ if (f -> next_child < count ) {
2479+ TSNode child = ts_node_child (f -> node , f -> next_child );
2480+ f -> next_child ++ ;
2481+ if (top == cap ) {
2482+ int new_cap = cap * 2 ;
2483+ UsageFrame * grown =
2484+ (UsageFrame * )cbm_arena_alloc (ctx -> arena , (size_t )new_cap * sizeof (* grown ));
2485+ if (!grown ) {
2486+ return ;
2487+ }
2488+ memcpy (grown , frames , (size_t )cap * sizeof (* frames ));
2489+ frames = grown ;
2490+ cap = new_cap ;
2491+ }
2492+ frames [top ++ ] = (UsageFrame ){child , 0 , false, false};
2493+ entering = true;
2494+ } else {
2495+ if (f -> counts_call ) {
2496+ call_depth -- ;
2497+ }
2498+ if (f -> counts_import ) {
2499+ import_depth -- ;
2500+ }
2501+ top -- ;
2502+ entering = false;
24712503 }
24722504 }
24732505}
0 commit comments