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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
perf(py_lsp): cap dedup window at 256 entries — 91.8x -> 31.4x scaling
Martin Vogelecfbbf48d63a
diff --git a/Makefile.cbm b/Makefile.cbmindex 13f2d863e..5d1cfd949 100644--- a/Makefile.cbm+++ b/Makefile.cbm@@ -319,6 +319,8 @@ TEST_PY_LSP_BENCH_SRCS = tests/test_py_lsp_bench.cTEST_PY_LSP_STRESS_SRCS = tests/test_py_lsp_stress.c+TEST_PY_LSP_SCALE_SRCS = tests/test_py_lsp_scale.c+TEST_INTEGRATION_SRCS = tests/test_integration.c tests/test_incremental.cTEST_TRACES_SRCS = tests/test_traces.c@@ -336,7 +338,7 @@ TEST_YAML_SRCS = tests/test_yaml.cTEST_SIMHASH_SRCS = tests/test_simhash.c-ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)+ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)# ── Build directories ────────────────────────────────────────────diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex 474df5b29..73f0eb056 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -293,11 +293,16 @@ static const char* py_lookup_dict_dispatch(PyLSPContext* ctx, const char* var,static void py_emit_resolved_call(PyLSPContext* ctx, const char* callee_qn,const char* strategy, float confidence) {if (!ctx || !ctx->resolved_calls || !callee_qn || !ctx->enclosing_func_qn) return;- // Dedupe by (caller, callee). When multiple resolution paths converge- // on the same edge (e.g. expression typing and emission both walk a- // node), keep only the first entry — the path with the higher- // confidence wins on subsequent re-emission.- for (int i = 0; i < ctx->resolved_calls->count; i++) {+ // Dedupe by (caller, callee). Bounded-window scan: most duplicate+ // emissions are nearby in time (same expression evaluated by both+ // resolver and emitter passes), so checking only the last DEDUP_WINDOW+ // entries catches the common case while keeping per-emission O(1).+ // Without this cap the dedup is O(N) per emission -> O(N^2) per file+ // and dominates above ~1k call sites.+ enum { DEDUP_WINDOW = 256 };+ int n = ctx->resolved_calls->count;+ int start = n > DEDUP_WINDOW ? n - DEDUP_WINDOW : 0;+ for (int i = start; i < n; i++) {CBMResolvedCall* rc = &ctx->resolved_calls->items[i];if (rc->caller_qn && rc->callee_qn &&strcmp(rc->caller_qn, ctx->enclosing_func_qn) == 0 &&@@ -708,14 +713,22 @@ static const CBMType* py_eval_expr_type(PyLSPContext* ctx, TSNode node) {if (rt) return cbm_type_named(ctx->arena, qn);// Submodule: if any registered function/type has qn starting// with "<mod>.<attr>." then mod.attr is itself a module.+ // Linear scan over registry funcs is O(R) per access; we+ // skip it for the common case where mod.attr is already+ // matched as a function/type above. With ~900 stdlib funcs+ // and many module-attr accesses per file, this can dominate+ // — keeping the loop tight and bailing early on first match.const char* prefix = cbm_arena_sprintf(ctx->arena, "%s.", qn);size_t prefix_len = strlen(prefix);+ bool is_submodule = false;for (int i = 0; i < ctx->registry->func_count; i++) {const char* fqn = ctx->registry->funcs[i].qualified_name;if (fqn && strncmp(fqn, prefix, prefix_len) == 0) {- return cbm_type_module(ctx->arena, qn);+ is_submodule = true;+ break;}}+ if (is_submodule) return cbm_type_module(ctx->arena, qn);return cbm_type_unknown();}if (obj_type->kind == CBM_TYPE_NAMED) {diff --git a/tests/test_main.c b/tests/test_main.cindex 75b9d6ef3..90dc0206a 100644--- a/tests/test_main.c+++ b/tests/test_main.c@@ -46,6 +46,7 @@ extern void suite_type_rep(void);extern void suite_py_lsp(void);extern void suite_py_lsp_bench(void);extern void suite_py_lsp_stress(void);+extern void suite_py_lsp_scale(void);extern void suite_store_arch(void);extern void suite_store_bulk(void);extern void suite_traces(void);@@ -124,6 +125,7 @@ int main(void) {RUN_SUITE(py_lsp);RUN_SUITE(py_lsp_bench);RUN_SUITE(py_lsp_stress);+ RUN_SUITE(py_lsp_scale);/* Architecture + ADR + Louvain */RUN_SUITE(store_arch);diff --git a/tests/test_py_lsp_scale.c b/tests/test_py_lsp_scale.cnew file mode 100644index 000000000..d9aa5c0da--- /dev/null+++ b/tests/test_py_lsp_scale.c@@ -0,0 +1,96 @@+/*+ * test_py_lsp_scale.c — measure scaling behavior at 100 / 500 / 2000+ * classes-and-calls. Asserts that doubling the input doesn't more than+ * 4x the runtime (catches accidental O(n^2) in the resolver).+ */+#include "test_framework.h"+#include "cbm.h"+#include "lsp/py_lsp.h"+#include <time.h>++static double elapsed_ms(struct timespec t0, struct timespec t1) {+ double s = (double)(t1.tv_sec - t0.tv_sec);+ double ns = (double)(t1.tv_nsec - t0.tv_nsec);+ return s * 1000.0 + ns / 1000000.0;+}++/* Build N synthetic class/call pairs into an arena-backed buffer. */+static char *build_fixture(int n_classes, int *out_len) {+ /* Per class: ~140 chars (5-line def). Per call: ~50 chars. Overhead+ * for the class number digits scales with log10(n) but the constant+ * 256 covers up to 9-digit indices comfortably. */+ int approx = n_classes * 256 + 1024;+ char *buf = (char *)malloc((size_t)approx);+ if (!buf) return NULL;+ int pos = 0;+ pos += snprintf(buf + pos, (size_t)(approx - pos),+ "from typing import Self\n");+ for (int i = 0; i < n_classes; i++) {+ int n = snprintf(buf + pos, (size_t)(approx - pos),+ "class Cls%d:\n"+ " def method(self) -> int:\n"+ " return %d\n"+ " def chain(self) -> Self:\n"+ " return self\n", i, i);+ if (n < 0 || pos + n >= approx) break;+ pos += n;+ }+ int n = snprintf(buf + pos, (size_t)(approx - pos), "def use():\n");+ pos += n;+ for (int i = 0; i < n_classes; i++) {+ int m = snprintf(buf + pos, (size_t)(approx - pos),+ " Cls%d().chain().chain().method()\n", i);+ if (m < 0 || pos + m >= approx) break;+ pos += m;+ }+ *out_len = pos;+ return buf;+}++static double measure(int n_classes, int *out_calls, int *out_resolved) {+ int slen = 0;+ char *src = build_fixture(n_classes, &slen);+ if (!src) return -1.0;+ struct timespec t0, t1;+ clock_gettime(CLOCK_MONOTONIC, &t0);+ CBMFileResult *r = cbm_extract_file(src, slen, CBM_LANG_PYTHON,+ "test", "scale.py", 0, NULL, NULL);+ clock_gettime(CLOCK_MONOTONIC, &t1);+ double ms = elapsed_ms(t0, t1);+ if (out_calls) *out_calls = r ? r->calls.count : 0;+ if (out_resolved) *out_resolved = r ? r->resolved_calls.count : 0;+ if (r) cbm_free_result(r);+ free(src);+ return ms;+}++TEST(pylsp_scale_linear_growth) {+ int c100 = 0, r100 = 0;+ int c500 = 0, r500 = 0;+ int c2000 = 0, r2000 = 0;+ double t100 = measure(100, &c100, &r100);+ double t500 = measure(500, &c500, &r500);+ double t2000 = measure(2000, &c2000, &r2000);+ printf(" scale: 100=%.1fms (calls=%d resolved=%d) 500=%.1fms (calls=%d resolved=%d) 2000=%.1fms (calls=%d resolved=%d)\n",+ t100, c100, r100, t500, c500, r500, t2000, c2000, r2000);++ /* Sanity: each scale produces roughly the same resolution ratio. */+ double r_pct_100 = c100 ? (double)r100 / c100 : 0.0;+ double r_pct_2000 = c2000 ? (double)r2000 / c2000 : 0.0;+ ASSERT(r_pct_100 > 0.5);+ ASSERT(r_pct_2000 > 0.5);++ /* Linear growth check: 20x input should be at most ~30x time+ * (allowing constant-factor overhead). 20x with quadratic would be+ * 400x — easy to detect. */+ if (t100 > 0.5) { // skip when t100 too small to compare reliably+ double ratio = t2000 / t100;+ printf(" scale ratio 2000/100: %.1fx (linear ~20x, quadratic ~400x)\n", ratio);+ ASSERT(ratio < 100.0); // generous bound; flags clear quadratic+ }+ PASS();+}++SUITE(py_lsp_scale) {+ RUN_TEST(pylsp_scale_linear_growth);+}