Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cbm): convert 7 O(N²) import extractors to O(N) TSTreeCursor iteration (#131)

Shane McCarron178bea2cd865
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index ae6c67c28..87f8021bf 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -55,9 +55,13 @@ static const char *path_last(CBMArena *a, const char *path) {
static void parse_go_imports(CBMExtractCtx *ctx) {
CBMArena *a = ctx->arena;
- uint32_t root_count = ts_node_child_count(ctx->root);
- for (uint32_t i = 0; i < root_count; i++) {
- TSNode decl = ts_node_child(ctx->root, i);
+ TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
+ if (!ts_tree_cursor_goto_first_child(&cursor)) {
+ ts_tree_cursor_delete(&cursor);
+ return;
+ }
+ do {
+ TSNode decl = ts_tree_cursor_current_node(&cursor);
if (strcmp(ts_node_type(decl), "import_declaration") != 0) {
continue;
}
@@ -121,7 +125,8 @@ static void parse_go_imports(CBMExtractCtx *ctx) {
}
}
}
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- Python imports ---
@@ -131,9 +136,13 @@ static void parse_go_imports(CBMExtractCtx *ctx) {
static void parse_python_imports(CBMExtractCtx *ctx) {
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);
+ 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);
const char *kind = ts_node_type(node);
if (strcmp(kind, "import_statement") == 0) {
@@ -227,7 +236,8 @@ static void parse_python_imports(CBMExtractCtx *ctx) {
}
}
}
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- ES module imports (JS/TS/TSX) ---
@@ -347,9 +357,13 @@ static void parse_es_imports(CBMExtractCtx *ctx) {
static void parse_java_imports(CBMExtractCtx *ctx) {
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);
+ 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), "import_declaration") != 0) {
continue;
}
@@ -368,7 +382,8 @@ static void parse_java_imports(CBMExtractCtx *ctx) {
break;
}
}
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- Rust imports ---
@@ -377,9 +392,13 @@ static void parse_java_imports(CBMExtractCtx *ctx) {
static void parse_rust_imports(CBMExtractCtx *ctx) {
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);
+ 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), "use_declaration") != 0) {
continue;
}
@@ -399,7 +418,8 @@ static void parse_rust_imports(CBMExtractCtx *ctx) {
CBMImport imp = {.local_name = path_last(a, full), .module_path = full};
cbm_imports_push(&ctx->result->imports, a, imp);
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- C/C++ imports ---
@@ -408,9 +428,13 @@ static void parse_rust_imports(CBMExtractCtx *ctx) {
static void parse_c_imports(CBMExtractCtx *ctx) {
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);
+ 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);
const char *kind = ts_node_type(node);
if (strcmp(kind, "preproc_include") != 0 && strcmp(kind, "preproc_import") != 0) {
continue;
@@ -447,7 +471,8 @@ static void parse_c_imports(CBMExtractCtx *ctx) {
CBMImport imp = {.local_name = path_last(a, path), .module_path = path};
cbm_imports_push(&ctx->result->imports, a, imp);
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- Ruby imports ---
@@ -458,9 +483,13 @@ static void parse_ruby_imports(CBMExtractCtx *ctx) {
// Walk for call nodes with "require" or "require_relative"
// Simple: walk top-level children
- 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);
+ 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);
const char *kind = ts_node_type(node);
if (strcmp(kind, "call") != 0 && strcmp(kind, "command_call") != 0) {
continue;
@@ -511,7 +540,8 @@ static void parse_ruby_imports(CBMExtractCtx *ctx) {
CBMImport imp = {.local_name = path_last(a, arg_text), .module_path = arg_text};
cbm_imports_push(&ctx->result->imports, a, imp);
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- Lua imports ---
@@ -520,9 +550,13 @@ static void parse_ruby_imports(CBMExtractCtx *ctx) {
static void parse_lua_imports(CBMExtractCtx *ctx) {
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);
+ 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);
// Lua: local X = require("Y") → assignment_statement or variable_declaration
// containing function_call(require, "Y")
char *text = cbm_node_text(a, node, ctx->source);
@@ -567,7 +601,8 @@ static void parse_lua_imports(CBMExtractCtx *ctx) {
char *mod = cbm_arena_strndup(a, start, (size_t)(end - start));
CBMImport imp = {.local_name = path_last(a, mod), .module_path = mod};
cbm_imports_push(&ctx->result->imports, a, imp);
- }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_tree_cursor_delete(&cursor);
}
// --- Generic import parsing for languages with simple import_declaration ---
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index c6e73fbe1..6eb89ba28 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -1671,6 +1671,104 @@ TEST(js_imports) {
PASS();
}
+TEST(go_imports) {
+ CBMFileResult *r =
+ extract("package main\n\nimport \"fmt\"\nimport (\n \"os\"\n net \"net/http\"\n)\n",
+ CBM_LANG_GO, "t", "main.go");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_GT(r->imports.count, 0);
+ ASSERT(has_import(r, "fmt"));
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(java_imports) {
+ CBMFileResult *r =
+ extract("import java.util.List;\nimport java.util.ArrayList;\nimport static java.lang.Math.PI;\n"
+ "public class Foo {}\n",
+ CBM_LANG_JAVA, "t", "Foo.java");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_GT(r->imports.count, 0);
+ ASSERT(has_import(r, "java.util.List"));
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(rust_imports) {
+ CBMFileResult *r =
+ extract("use std::collections::HashMap;\nuse std::io::{self, Write};\nuse serde::Serialize;\n"
+ "fn main() {}\n",
+ CBM_LANG_RUST, "t", "main.rs");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_GT(r->imports.count, 0);
+ ASSERT(has_import(r, "std::collections::HashMap"));
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(c_imports) {
+ CBMFileResult *r =
+ extract("#include <stdio.h>\n#include <stdlib.h>\n#include \"mylib.h\"\n\nint main() { return 0; }\n",
+ CBM_LANG_C, "t", "main.c");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_GT(r->imports.count, 0);
+ ASSERT(has_import(r, "stdio.h"));
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(ruby_imports) {
+ CBMFileResult *r =
+ extract("require 'json'\nrequire 'net/http'\nrequire_relative 'helpers'\n\nclass Foo; end\n",
+ CBM_LANG_RUBY, "t", "app.rb");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_GT(r->imports.count, 0);
+ ASSERT(has_import(r, "json"));
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(lua_imports) {
+ CBMFileResult *r =
+ extract("local json = require(\"dkjson\")\nlocal http = require(\"socket.http\")\n\nlocal function greet() end\n",
+ CBM_LANG_LUA, "t", "main.lua");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_GT(r->imports.count, 0);
+ ASSERT(has_import(r, "dkjson"));
+ cbm_free_result(r);
+ PASS();
+}
+
+TEST(import_stress_go) {
+ /* Stress test: 5,000 single-line Go imports.
+ * Verifies O(N) behaviour — would hang indefinitely with the O(N²) loop. */
+ const int N = 5000;
+ /* Each line: import "pkg/NNNNN"\n = ~20 chars; total ~100KB */
+ int buf_size = N * 24 + 64;
+ char *src = malloc((size_t)buf_size);
+ ASSERT_NOT_NULL(src);
+
+ int pos = 0;
+ pos += snprintf(src + pos, (size_t)(buf_size - pos), "package stress\n");
+ for (int k = 0; k < N; k++) {
+ pos += snprintf(src + pos, (size_t)(buf_size - pos), "import \"pkg/%05d\"\n", k);
+ }
+
+ CBMFileResult *r = extract(src, CBM_LANG_GO, "t", "stress.go");
+ free(src);
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT_EQ(r->imports.count, N);
+ cbm_free_result(r);
+ PASS();
+}
+
/* ═══════════════════════════════════════════════════════════════════
* config_extraction_test.go ports (25 tests)
* ═══════════════════════════════════════════════════════════════════ */
@@ -2192,6 +2290,13 @@ SUITE(extraction) {
RUN_TEST(go_calls);
RUN_TEST(python_imports);
RUN_TEST(js_imports);
+ RUN_TEST(go_imports);
+ RUN_TEST(java_imports);
+ RUN_TEST(rust_imports);
+ RUN_TEST(c_imports);
+ RUN_TEST(ruby_imports);
+ RUN_TEST(lua_imports);
+ RUN_TEST(import_stress_go);
/* config_extraction_test.go ports */
RUN_TEST(toml_basic_table_and_pair);