Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions internal/cbm/extract_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,18 @@ static void parse_lua_imports(CBMExtractCtx *ctx) {
static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type) {
CBMArena *a = ctx->arena;

uint32_t count = ts_node_child_count(ctx->root);
for (uint32_t i = 0; i < count; i++) {
TSNode node = ts_node_child(ctx->root, i);
/* Use TSTreeCursor for O(1)-per-step sibling traversal.
* ts_node_child(root, i) and ts_node_next_sibling() both call
* ts_node_child_with_descendant() internally, making naive iteration
* O(N²) on roots with thousands of children (e.g. generated .d.ts files).
* ts_tree_cursor_goto_next_sibling() maintains cursor state and is O(1). */
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
if (!ts_tree_cursor_goto_first_child(&cursor)) {
ts_tree_cursor_delete(&cursor);
return;
}
do {
TSNode node = ts_tree_cursor_current_node(&cursor);
if (strcmp(ts_node_type(node), node_type) != 0) {
continue;
}
Expand Down Expand Up @@ -616,7 +625,8 @@ static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type) {
}
}
}
}
} while (ts_tree_cursor_goto_next_sibling(&cursor));
ts_tree_cursor_delete(&cursor);
}

// --- Wolfram imports ---
Expand Down