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.

perf(k8s): infra resolvers stay linear in the repository's copies

Martin Vogel1eb55031e628
diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.c
index 5c8394055..636db83b9 100644
--- a/internal/cbm/extract_k8s.c
+++ b/internal/cbm/extract_k8s.c
@@ -28,6 +28,51 @@ enum {
// Internal helpers
// ---------------------------------------------------------------------------
+// The children of `node` (named only when `named`), in order, through a
+// tree cursor: ts_node_child(n, i) / ts_node_named_child(n, i) walk from the
+// first child on every call, so an index loop over a wide node (a list of
+// 100k items, a file of thousands of documents) is quadratic. The cursor is
+// released when the children are exhausted, or by k8s_kids_done.
+typedef struct {
+ TSTreeCursor cur;
+ bool live;
+} k8s_kids_t;
+
+static k8s_kids_t k8s_kids(TSNode node) {
+ k8s_kids_t k = {.live = false};
+ if (ts_node_is_null(node)) {
+ return k;
+ }
+ k.cur = ts_tree_cursor_new(node);
+ k.live = ts_tree_cursor_goto_first_child(&k.cur);
+ if (!k.live) {
+ ts_tree_cursor_delete(&k.cur);
+ }
+ return k;
+}
+
+static bool k8s_kid(k8s_kids_t *k, bool named, TSNode *out) {
+ while (k->live) {
+ TSNode n = ts_tree_cursor_current_node(&k->cur);
+ if (!ts_tree_cursor_goto_next_sibling(&k->cur)) {
+ ts_tree_cursor_delete(&k->cur);
+ k->live = false;
+ }
+ if (!named || ts_node_is_named(n)) {
+ *out = n;
+ return true;
+ }
+ }
+ return false;
+}
+
+static void k8s_kids_done(k8s_kids_t *k) {
+ if (k->live) {
+ ts_tree_cursor_delete(&k->cur);
+ k->live = false;
+ }
+}
+
// The value inside a flow_node: its first named child that is not an anchor
// (`&name`), a tag (`!!str`) or a comment, all of which precede the value.
static TSNode flow_node_value(TSNode node) {
@@ -96,16 +141,16 @@ static int is_kustomize_list_key(const char *key) {
// scalar child, using key_name as the local_name.
static void emit_kustomize_sequence(CBMExtractCtx *ctx, TSNode seq_node, const char *key_name) {
CBMArena *a = ctx->arena;
- uint32_t n = ts_node_child_count(seq_node);
- for (uint32_t i = 0; i < n; i++) {
- TSNode item = ts_node_child(seq_node, i);
+ k8s_kids_t items = k8s_kids(seq_node);
+ TSNode item;
+ while (k8s_kid(&items, false, &item)) {
if (strcmp(ts_node_type(item), "block_sequence_item") != 0) {
continue;
}
// block_sequence_item has one named child: the value
- uint32_t ic = ts_node_child_count(item);
- for (uint32_t j = 0; j < ic; j++) {
- TSNode val = ts_node_child(item, j);
+ k8s_kids_t vals = k8s_kids(item);
+ TSNode val;
+ while (k8s_kid(&vals, false, &val)) {
const char *scalar = get_scalar_text(a, val, ctx->source);
if (!scalar) {
continue;
@@ -198,9 +243,9 @@ static void process_kustomize_pair(CBMExtractCtx *ctx, TSNode pair) {
// Mirrors the K8s manifest kind-def so Kustomize resources are also discoverable.
static void emit_kustomize_kind_def(CBMExtractCtx *ctx, TSNode mapping) {
CBMArena *a = ctx->arena;
- uint32_t pair_n = ts_node_child_count(mapping);
- for (uint32_t pi = 0; pi < pair_n; pi++) {
- TSNode pair = ts_node_child(mapping, pi);
+ k8s_kids_t pairs = k8s_kids(mapping);
+ TSNode pair;
+ while (k8s_kid(&pairs, false, &pair)) {
if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {
continue;
}
@@ -228,15 +273,15 @@ static void emit_kustomize_kind_def(CBMExtractCtx *ctx, TSNode mapping) {
def.start_line = ts_node_start_point(mapping).row + TS_LINE_OFFSET;
def.end_line = ts_node_end_point(mapping).row + TS_LINE_OFFSET;
cbm_defs_push(&ctx->result->defs, a, def);
+ k8s_kids_done(&pairs);
return;
}
}
static void extract_kustomize(CBMExtractCtx *ctx) {
- TSNode root = ctx->root;
- uint32_t root_n = ts_node_child_count(root);
- for (uint32_t si = 0; si < root_n; si++) {
- TSNode stream_child = ts_node_child(root, si);
+ k8s_kids_t docs = k8s_kids(ctx->root);
+ TSNode stream_child;
+ while (k8s_kid(&docs, false, &stream_child)) {
if (strcmp(ts_node_type(stream_child), "document") != 0) {
continue;
}
@@ -247,9 +292,10 @@ static void extract_kustomize(CBMExtractCtx *ctx) {
emit_kustomize_kind_def(ctx, mapping);
- uint32_t pair_n = ts_node_child_count(mapping);
- for (uint32_t pi = 0; pi < pair_n; pi++) {
- process_kustomize_pair(ctx, ts_node_child(mapping, pi));
+ k8s_kids_t pairs = k8s_kids(mapping);
+ TSNode pair;
+ while (k8s_kid(&pairs, false, &pair)) {
+ process_kustomize_pair(ctx, pair);
}
}
}
@@ -272,9 +318,9 @@ static void extract_metadata(CBMArena *a, TSNode meta_mapping, const char *sourc
if (ts_node_is_null(meta_mapping) || strcmp(ts_node_type(meta_mapping), "block_mapping") != 0) {
return;
}
- uint32_t mn = ts_node_child_count(meta_mapping);
- for (uint32_t mi = 0; mi < mn; mi++) {
- TSNode mpair = ts_node_child(meta_mapping, mi);
+ k8s_kids_t pairs = k8s_kids(meta_mapping);
+ TSNode mpair;
+ while (k8s_kid(&pairs, false, &mpair)) {
if (strcmp(ts_node_type(mpair), "block_mapping_pair") != 0) {
continue;
}
@@ -299,9 +345,9 @@ static void extract_metadata(CBMArena *a, TSNode meta_mapping, const char *sourc
// Read apiVersion, kind and metadata from a document's top-level mapping.
static void extract_k8s_scalars(CBMExtractCtx *ctx, TSNode mapping, k8s_doc_id_t *id) {
CBMArena *a = ctx->arena;
- uint32_t n = ts_node_child_count(mapping);
- for (uint32_t i = 0; i < n; i++) {
- TSNode pair = ts_node_child(mapping, i);
+ k8s_kids_t pairs = k8s_kids(mapping);
+ TSNode pair;
+ while (k8s_kid(&pairs, false, &pair)) {
if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {
continue;
}
@@ -781,9 +827,9 @@ static bool k8s_is_pair(TSNode n) {
// The scalar value of `field` in a mapping, or NULL.
static const char *k8s_field(k8s_walk_t *w, TSNode mapping, const char *field) {
- uint32_t n = ts_node_named_child_count(mapping);
- for (uint32_t i = 0; i < n; i++) {
- TSNode pair = ts_node_named_child(mapping, i);
+ k8s_kids_t pairs = k8s_kids(mapping);
+ TSNode pair;
+ while (k8s_kid(&pairs, true, &pair)) {
if (!k8s_is_pair(pair)) {
continue;
}
@@ -791,6 +837,7 @@ static const char *k8s_field(k8s_walk_t *w, TSNode mapping, const char *field) {
const char *k =
ts_node_is_null(key) ? NULL : get_scalar_text(w->ctx->arena, key, w->ctx->source);
if (k && strcmp(k, field) == 0) {
+ k8s_kids_done(&pairs);
TSNode v = unwrap_pair_value(pair);
return ts_node_is_null(v) ? NULL : get_scalar_text(w->ctx->arena, v, w->ctx->source);
}
@@ -804,9 +851,9 @@ static TSNode k8s_child(k8s_walk_t *w, TSNode mapping, const char *field) {
if (ts_node_is_null(mapping) || !k8s_is_mapping(mapping)) {
return none;
}
- uint32_t n = ts_node_named_child_count(mapping);
- for (uint32_t i = 0; i < n; i++) {
- TSNode pair = ts_node_named_child(mapping, i);
+ k8s_kids_t pairs = k8s_kids(mapping);
+ TSNode pair;
+ while (k8s_kid(&pairs, true, &pair)) {
if (!k8s_is_pair(pair)) {
continue;
}
@@ -814,6 +861,7 @@ static TSNode k8s_child(k8s_walk_t *w, TSNode mapping, const char *field) {
const char *k =
ts_node_is_null(key) ? NULL : get_scalar_text(w->ctx->arena, key, w->ctx->source);
if (k && strcmp(k, field) == 0) {
+ k8s_kids_done(&pairs);
TSNode v = unwrap_pair_value(pair);
return ts_node_is_null(v) ? none : k8s_unwrap(v);
}
@@ -821,25 +869,20 @@ static TSNode k8s_child(k8s_walk_t *w, TSNode mapping, const char *field) {
return none;
}
-// The items of a sequence node, unwrapped, comments skipped.
-typedef struct {
- TSNode seq;
- uint32_t i;
- uint32_t n;
-} k8s_iter_t;
+// The items of a sequence node, unwrapped, comments skipped (iterate to the
+// end: the cursor is released when the items are exhausted).
+typedef k8s_kids_t k8s_iter_t;
static k8s_iter_t k8s_iter(TSNode seq) {
- k8s_iter_t it = {.seq = seq};
- if (!ts_node_is_null(seq) && (strcmp(ts_node_type(seq), "block_sequence") == 0 ||
- strcmp(ts_node_type(seq), "flow_sequence") == 0)) {
- it.n = ts_node_named_child_count(seq);
- }
- return it;
+ TSNode none = {0};
+ bool is_seq = !ts_node_is_null(seq) && (strcmp(ts_node_type(seq), "block_sequence") == 0 ||
+ strcmp(ts_node_type(seq), "flow_sequence") == 0);
+ return k8s_kids(is_seq ? seq : none);
}
static bool k8s_next(k8s_iter_t *it, TSNode *out) {
- while (it->i < it->n) {
- TSNode item = ts_node_named_child(it->seq, it->i++);
+ TSNode item;
+ while (k8s_kid(it, true, &item)) {
if (strcmp(ts_node_type(item), "block_sequence_item") == 0) {
item = first_content_child(item);
}
@@ -985,9 +1028,9 @@ static void k8s_walk(k8s_walk_t *w, TSNode node, char *path, size_t len, size_t
const char *type = ts_node_type(node);
if (k8s_is_mapping(node)) {
k8s_mapping(w, path, node);
- uint32_t n = ts_node_named_child_count(node);
- for (uint32_t i = 0; i < n; i++) {
- TSNode pair = ts_node_named_child(node, i);
+ k8s_kids_t pairs = k8s_kids(node);
+ TSNode pair;
+ while (k8s_kid(&pairs, true, &pair)) {
if (!k8s_is_pair(pair)) {
continue;
}
@@ -1019,9 +1062,9 @@ static void k8s_walk(k8s_walk_t *w, TSNode node, char *path, size_t len, size_t
path[len] = '\0';
return;
}
- uint32_t n = ts_node_named_child_count(node);
- for (uint32_t i = 0; i < n; i++) {
- TSNode item = ts_node_named_child(node, i);
+ k8s_kids_t items = k8s_kids(node);
+ TSNode item;
+ while (k8s_kid(&items, true, &item)) {
if (strcmp(ts_node_type(item), "block_sequence_item") == 0) {
item = first_content_child(item);
}
@@ -1108,10 +1151,9 @@ static void extract_k8s_manifest(CBMExtractCtx *ctx) {
return;
}
- TSNode root = ctx->root;
- uint32_t root_n = ts_node_child_count(root);
- for (uint32_t si = 0; si < root_n; si++) {
- TSNode stream_child = ts_node_child(root, si);
+ k8s_kids_t docs = k8s_kids(ctx->root);
+ TSNode stream_child;
+ while (k8s_kid(&docs, false, &stream_child)) {
if (strcmp(ts_node_type(stream_child), "document") != 0) {
continue;
}
diff --git a/scripts/check-lsp-originality.sh b/scripts/check-lsp-originality.sh
index a50af9de4..a107d60f0 100755
--- a/scripts/check-lsp-originality.sh
+++ b/scripts/check-lsp-originality.sh
@@ -64,8 +64,21 @@ REFS=(
# Perl (PR #461) was authored clean-room. We scan against PerlNavigator (MIT)
# — the leading OSS Perl language server — for defensive copy-detection.
"perl|https://github.com/bscan/PerlNavigator|server/src"
+ # Infrastructure hybrid LSPs (K8s YAML, Kustomize bundles, Flux/Argo deploy
+ # scopes, the Go operator extension) were written from the Kubernetes, Flux,
+ # Argo, Cluster API, cert-manager and Kubermatic API types. Scanned against
+ # the YAML/K8s language server, helm-ls, kustomize, Flux's kustomization
+ # generator and controller-tools (the +groupName markers).
+ "yaml|https://github.com/redhat-developer/yaml-language-server|src"
+ "helm|https://github.com/mrjosh/helm-ls|internal"
+ "kustomize|https://github.com/kubernetes-sigs/kustomize|api/krusty,api/internal/target"
+ "flux|https://github.com/fluxcd/pkg|kustomize"
+ "kube|https://github.com/kubernetes-sigs/controller-tools|pkg"
)
+# Infrastructure hybrid LSP sources outside internal/cbm/lsp/ (scanned too).
+INFRA_SRC=(internal/cbm/extract_k8s.c src/pipeline/pass_k8s.c src/pipeline/pass_operator.c)
+
ONLY_LANG=""; REFRESH=0; LIST_ONLY=0
while [ $# -gt 0 ]; do
case "$1" in
@@ -87,6 +100,9 @@ fi
LSP_SRC=()
while IFS= read -r f; do LSP_SRC+=("$f"); done < <(
find "$LSP_DIR" -type f \( -name '*.c' -o -name '*.h' \) -not -path '*/generated/*' | sort)
+for f in "${INFRA_SRC[@]}"; do
+ [ -f "$ROOT/$f" ] && LSP_SRC+=("$ROOT/$f")
+done
if [ "${#LSP_SRC[@]}" -eq 0 ]; then
echo "$TAG ERROR: no LSP sources under $LSP_DIR" >&2; exit 2
fi
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index ca466b0ba..3dea7f979 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -1102,6 +1102,9 @@ static void k8s_apply_effective_namespaces(cbm_pipeline_ctx_t *ctx, k8s_bundles_
}
}
+/* Candidates examined by the resolvers below (pipeline_internal.h). */
+_Atomic uint64_t g_k8s_candidates = 0;
+
/* ── Object references (PHASE1.md) ───────────────────────────────── */
/* A reference fact of a manifest, kept past its file's extraction. */
@@ -1517,6 +1520,144 @@ static void k8s_deploys_build(cbm_pipeline_ctx_t *ctx, k8s_kusts_t *ks, k8s_bund
cbm_log_info("pass.k8s.deploys", "deploys", itoa_k8s(n), "roots", itoa_k8s(roots));
}
+/* ── Bundle-keyed candidate indexes ───────────────────────────────── */
+
+/* Candidates are looked up per bundle the source shares, never by name
+ * across the repository: copies of one name in independent bundles would
+ * otherwise make every lookup visit every copy (quadratic in copies). */
+
+typedef struct {
+ int64_t *items;
+ int count;
+ int cap;
+} k8s_ids_t;
+
+static void k8s_ids_push(k8s_ids_t *v, int64_t id) {
+ if (k8s_grow((void **)&v->items, &v->cap, v->count, sizeof(*v->items))) {
+ v->items[v->count++] = id;
+ }
+}
+
+static int k8s_cmp_id(const void *a, const void *b) {
+ int64_t x = *(const int64_t *)a;
+ int64_t y = *(const int64_t *)b;
+ return (x > y) - (x < y);
+}
+
+static void k8s_ids_sort_unique(k8s_ids_t *v) {
+ if (v->count < PAIR_LEN) {
+ return;
+ }
+ qsort(v->items, (size_t)v->count, sizeof(*v->items), k8s_cmp_id);
+ int w = SKIP_ONE;
+ for (int i = SKIP_ONE; i < v->count; i++) {
+ if (v->items[i] != v->items[w - SKIP_ONE]) {
+ v->items[w++] = v->items[i];
+ }
+ }
+ v->count = w;
+}
+
+static void k8s_free_ids_entry(const char *key, void *value, void *ud) {
+ (void)ud;
+ k8s_ids_t *v = value;
+ cbm_free(CBM_MEM_CLASS_OTHER, v->items);
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
+/* "<bundle>\x1f<name>": an interned bundle (by address) with an object
+ * name, a type or a label pair. */
+static void k8s_bkey(char *out, size_t out_sz, const char *bundle, const char *name) {
+ snprintf(out, out_sz, "%p\x1f%s", (const void *)bundle, name);
+}
+
+static void k8s_index_add(CBMHashTable *ix, const char *key, int64_t id) {
+ k8s_ids_t *v = cbm_ht_get(ix, key);
+ if (!v) {
+ v = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*v));
+ char *owned = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key);
+ if (!v || !owned) {
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, owned);
+ return;
+ }
+ cbm_ht_set(ix, owned, v);
+ }
+ if (v->count == 0 || v->items[v->count - SKIP_ONE] != id) {
+ k8s_ids_push(v, id);
+ }
+}
+
+/* Calls `fn(bundle, ud)` for every bundle of `file`: its build roots /
+ * chart / dir and, when `deploys`, the deploy scopes reaching it. */
+typedef void (*k8s_scope_fn)(const char *bundle, void *ud);
+
+static void k8s_each_scope(k8s_bundles_t *b, const char *file, bool deploys, k8s_scope_fn fn,
+ void *ud) {
+ k8s_member_t one[SKIP_ONE];
+ k8s_members_t single = {.items = one, .cap = SKIP_ONE};
+ const k8s_members_t *m = k8s_bundles_of(b, file, &single);
+ for (int i = 0; i < m->count; i++) {
+ fn(m->items[i].bundle, ud);
+ }
+ const k8s_members_t *d = deploys && b->deploys ? cbm_ht_get(b->deploys, file) : NULL;
+ for (int i = 0; d && i < d->count; i++) {
+ fn(d->items[i].bundle, ud);
+ }
+}
+
+typedef struct {
+ CBMHashTable *ix;
+ const char *name;
+ int64_t id;
+ k8s_ids_t *found;
+} k8s_scope_job_t;
+
+static void k8s_scope_add(const char *bundle, void *ud) {
+ k8s_scope_job_t *j = ud;
+ char key[CBM_SZ_1K];
+ k8s_bkey(key, sizeof(key), bundle, j->name);
+ k8s_index_add(j->ix, key, j->id);
+}
+
+static void k8s_scope_find(const char *bundle, void *ud) {
+ k8s_scope_job_t *j = ud;
+ char key[CBM_SZ_1K];
+ k8s_bkey(key, sizeof(key), bundle, j->name);
+ const k8s_ids_t *v = cbm_ht_get(j->ix, key);
+ for (int i = 0; v && i < v->count; i++) {
+ k8s_ids_push(j->found, v->items[i]);
+ }
+}
+
+/* Index every Resource by "<scope>\x1f<Kind/name>" over its bundles (and
+ * deploy scopes when `deploys`). */
+static CBMHashTable *k8s_index_objects(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, bool deploys) {
+ CBMHashTable *ix = cbm_ht_create(0);
+ const cbm_gbuf_node_t **nodes = NULL;
+ int count = 0;
+ if (!ix || cbm_gbuf_find_by_label(ctx->gbuf, "Resource", &nodes, &count) != 0) {
+ return ix;
+ }
+ for (int i = 0; i < count; i++) {
+ if (nodes[i]->file_path && nodes[i]->name) {
+ k8s_scope_job_t j = {ix, nodes[i]->name, nodes[i]->id, NULL};
+ k8s_each_scope(b, nodes[i]->file_path, deploys, k8s_scope_add, &j);
+ }
+ }
+ return ix;
+}
+
+/* The ids `name` has in any scope of `file`, sorted, unique. */
+static void k8s_find_in_scopes(k8s_bundles_t *b, CBMHashTable *ix, const char *file,
+ const char *name, bool deploys, k8s_ids_t *found) {
+ found->count = 0;
+ k8s_scope_job_t j = {ix, name, 0, found};
+ k8s_each_scope(b, file, deploys, k8s_scope_find, &j);
+ k8s_ids_sort_unique(found);
+}
+
/* The namespace of a Resource's logical key ("" when unknown `?` or
* cluster-scoped `_`), else its declared one. */
static void k8s_effective_ns(const cbm_gbuf_node_t *n, char *out, size_t out_sz) {
@@ -1624,7 +1765,11 @@ static int64_t k8s_route_node(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *sr
static void k8s_resolve_refs(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8s_facts_t *f) {
int edges = 0;
int unresolved = 0;
- for (int i = 0; i < f->ref_n; i++) {
+ CBMHashTable *ix = f->ref_n > 0 ? k8s_index_objects(ctx, b, true) : NULL;
+ k8s_ids_t found = {0};
+ const cbm_gbuf_node_t **cands = NULL;
+ int cand_cap = 0;
+ for (int i = 0; ix && i < f->ref_n; i++) {
const k8s_ref_t *ref = &f->refs[i];
const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(ctx->gbuf, ref->src);
int64_t from = src ? src->id : 0;
@@ -1636,11 +1781,17 @@ static void k8s_resolve_refs(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8
}
char name[CBM_SZ_1K];
snprintf(name, sizeof(name), "%s/%s", ref->kind, ref->name);
- const cbm_gbuf_node_t **cands = NULL;
int n = 0;
- if (!src || !src->file_path || cbm_gbuf_find_by_name(ctx->gbuf, name, &cands, &n) != 0) {
- n = 0;
+ if (src && src->file_path) {
+ k8s_find_in_scopes(b, ix, src->file_path, name, true, &found);
+ for (int c = 0; c < found.count; c++) {
+ const cbm_gbuf_node_t *t = cbm_gbuf_find_by_id(ctx->gbuf, found.items[c]);
+ if (t && k8s_grow((void **)&cands, &cand_cap, n, sizeof(*cands))) {
+ cands[n++] = t;
+ }
+ }
}
+ atomic_fetch_add_explicit(&g_k8s_candidates, (uint64_t)(n), memory_order_relaxed);
char props[CBM_SZ_256];
snprintf(props, sizeof(props), "{\"%s\":\"%s\"}",
strcmp(ref->edge, "INFRA_MAPS") == 0 ? "kind"
@@ -1671,6 +1822,12 @@ static void k8s_resolve_refs(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8
edges += hits;
unresolved += hits == 0;
}
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)cands);
+ cbm_free(CBM_MEM_CLASS_OTHER, found.items);
+ if (ix) {
+ cbm_ht_foreach(ix, k8s_free_ids_entry, NULL);
+ cbm_ht_free(ix);
+ }
if (f->ref_n > 0) {
cbm_log_info("pass.k8s.refs", "refs", itoa_k8s(f->ref_n), "edges", itoa_k8s(edges),
"unresolved", itoa_k8s(unresolved));
@@ -1690,24 +1847,6 @@ static int k8s_cmp_crd_key(const void *a, const void *b) {
return c ? c : (x->crd > y->crd) - (x->crd < y->crd);
}
-/* Do the files at `a` and `b` share a bundle? */
-static bool k8s_same_bundle(k8s_bundles_t *b, const char *a, const char *c) {
- k8s_member_t one_a[SKIP_ONE];
- k8s_member_t one_c[SKIP_ONE];
- k8s_members_t single_a = {.items = one_a, .cap = SKIP_ONE};
- k8s_members_t single_c = {.items = one_c, .cap = SKIP_ONE};
- const k8s_members_t *ma = k8s_bundles_of(b, a, &single_a);
- const k8s_members_t *mc = k8s_bundles_of(b, c, &single_c);
- for (int i = 0; i < ma->count; i++) {
- for (int j = 0; j < mc->count; j++) {
- if (ma->items[i].bundle == mc->items[j].bundle) {
- return true;
- }
- }
- }
- return false;
-}
-
/* A custom resource is an instance of the CRD declaring its group and
* kind. A CRD vendored in several places: the copy deployed with the
* resource (one bundle) is the one; with no copy beside it, the resource
@@ -1761,10 +1900,20 @@ static void k8s_link_crds(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8s_f
for (int i = f->crd_n - SKIP_ONE; i >= 0; i--) {
cbm_ht_set(by_type, order[i].key, (void *)(intptr_t)(i + SKIP_ONE)); /* first of run */
}
+ /* "<bundle>\x1f<group/kind>" -> the CRDs of that type in that bundle */
+ CBMHashTable *in_bundle = cbm_ht_create(0);
+ for (int i = 0; in_bundle && i < f->crd_n; i++) {
+ const cbm_gbuf_node_t *crd = cbm_gbuf_find_by_id(ctx->gbuf, f->crds[order[i].crd].node);
+ if (crd && crd->file_path) {
+ k8s_scope_job_t j = {in_bundle, order[i].key, crd->id, NULL};
+ k8s_each_scope(b, crd->file_path, false, k8s_scope_add, &j);
+ }
+ }
+ k8s_ids_t beside = {0};
const cbm_gbuf_node_t **nodes = NULL;
int count = 0;
int edges = 0;
- if (cbm_gbuf_find_by_label(ctx->gbuf, "Resource", &nodes, &count) == 0) {
+ if (in_bundle && cbm_gbuf_find_by_label(ctx->gbuf, "Resource", &nodes, &count) == 0) {
for (int i = 0; i < count; i++) {
const char *key =
nodes[i]->qualified_name ? strstr(nodes[i]->qualified_name, "::") : NULL;
@@ -1783,30 +1932,40 @@ static void k8s_link_crds(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8s_f
if (first < 0) {
continue;
}
+ k8s_find_in_scopes(b, in_bundle, nodes[i]->file_path, type, false, &beside);
+ if (beside.count > 0) {
+ atomic_fetch_add_explicit(&g_k8s_candidates, (uint64_t)beside.count,
+ memory_order_relaxed);
+ for (int r = 0; r < beside.count; r++) {
+ if (beside.items[r] != nodes[i]->id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, nodes[i]->id, beside.items[r], "USAGE",
+ "{\"ref\":\"crd\"}");
+ edges++;
+ }
+ }
+ continue;
+ }
int end = first;
while (end < f->crd_n && strcmp(order[end].key, type) == 0) {
end++;
}
- bool beside = false;
- for (int r = first; r < end && !beside; r++) {
- const cbm_gbuf_node_t *crd =
- cbm_gbuf_find_by_id(ctx->gbuf, f->crds[order[r].crd].node);
- beside = crd && crd->file_path &&
- k8s_same_bundle(b, nodes[i]->file_path, crd->file_path);
- }
+ atomic_fetch_add_explicit(&g_k8s_candidates, (uint64_t)(end - first),
+ memory_order_relaxed);
for (int r = first; r < end; r++) {
int64_t crd_id = f->crds[order[r].crd].node;
- const cbm_gbuf_node_t *crd = cbm_gbuf_find_by_id(ctx->gbuf, crd_id);
- if (crd_id == nodes[i]->id ||
- (beside && (!crd || !crd->file_path ||
- !k8s_same_bundle(b, nodes[i]->file_path, crd->file_path)))) {
- continue;
+ if (crd_id != nodes[i]->id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, nodes[i]->id, crd_id, "USAGE",
+ "{\"ref\":\"crd\"}");
+ edges++;
}
- cbm_gbuf_insert_edge(ctx->gbuf, nodes[i]->id, crd_id, "USAGE", "{\"ref\":\"crd\"}");
- edges++;
}
}
}
+ cbm_free(CBM_MEM_CLASS_OTHER, beside.items);
+ if (in_bundle) {
+ cbm_ht_foreach(in_bundle, k8s_free_ids_entry, NULL);
+ cbm_ht_free(in_bundle);
+ }
cbm_ht_free(by_type);
cbm_free(CBM_MEM_CLASS_OTHER, order);
if (edges > 0) {
@@ -1814,6 +1973,60 @@ static void k8s_link_crds(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, const k8s_f
}
}
+/* Selector postings keyed by "<bundle>\x1f<key=value>" (owned keys): the
+ * workloads (record indices) of one bundle carrying one label pair. */
+static void k8s_posting_free_owned(const char *key, void *value, void *userdata) {
+ k8s_posting_free(key, value, userdata);
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
+typedef struct {
+ CBMHashTable *index;
+ const k8s_record_array_t *recs;
+ const k8s_record_t *rec;
+ int rec_idx;
+ k8s_ids_t *cand; /* record indices */
+} k8s_sel_job_t;
+
+static void k8s_sel_post(const char *bundle, void *ud) {
+ k8s_sel_job_t *j = ud;
+ for (int p = 0; p < j->rec->pair_n; p++) {
+ char key[CBM_SZ_1K];
+ k8s_bkey(key, sizeof(key), bundle, j->recs->pairs[j->rec->pair_off + p]);
+ if (!cbm_ht_get(j->index, key)) {
+ k8s_posting_t *posting = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*posting));
+ char *owned = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key);
+ if (!posting || !owned) {
+ cbm_free(CBM_MEM_CLASS_OTHER, posting);
+ cbm_free(CBM_MEM_CLASS_OTHER, owned);
+ continue;
+ }
+ cbm_ht_set(j->index, owned, posting);
+ }
+ (void)k8s_posting_add(j->index, key, j->rec_idx);
+ }
+}
+
+/* The workloads of `bundle` carrying the selector's rarest pair there. */
+static void k8s_sel_rarest(const char *bundle, void *ud) {
+ k8s_sel_job_t *j = ud;
+ const k8s_posting_t *rarest = NULL;
+ for (int p = 0; p < j->rec->pair_n; p++) {
+ char key[CBM_SZ_1K];
+ k8s_bkey(key, sizeof(key), bundle, j->recs->pairs[j->rec->pair_off + p]);
+ const k8s_posting_t *posting = cbm_ht_get(j->index, key);
+ if (!posting) {
+ return;
+ }
+ if (!rarest || posting->count < rarest->count) {
+ rarest = posting;
+ }
+ }
+ for (int c = 0; rarest && c < rarest->count; c++) {
+ k8s_ids_push(j->cand, rarest->items[c]);
+ }
+}
+
/* A workload a selector matched: its object name, declaring file and
* whether that is the selector's file. */
typedef struct {
@@ -1882,12 +2095,13 @@ static void k8s_link_selectors(cbm_pipeline_ctx_t *ctx, const k8s_record_array_t
int match_cap = 0;
for (int i = 0; i < recs->count; i++) {
const k8s_record_t *wl = &recs->items[i];
- if (wl->kind == K8S_REC_WORKLOAD || wl->kind == K8S_REC_POD) {
- for (int p = 0; p < wl->pair_n; p++) {
- (void)k8s_posting_add(index, recs->pairs[wl->pair_off + p], i);
- }
+ const cbm_gbuf_node_t *wn = cbm_gbuf_find_by_id(ctx->gbuf, wl->node_id);
+ if ((wl->kind == K8S_REC_WORKLOAD || wl->kind == K8S_REC_POD) && wn && wn->file_path) {
+ k8s_sel_job_t j = {index, recs, wl, i, NULL};
+ k8s_each_scope(bundles, wn->file_path, false, k8s_sel_post, &j);
}
}
+ k8s_ids_t cand = {0};
int edges = 0;
for (int i = 0; i < recs->count; i++) {
/* a Service, PodDisruptionBudget or NetworkPolicy selector; one with
@@ -1899,22 +2113,20 @@ static void k8s_link_selectors(cbm_pipeline_ctx_t *ctx, const k8s_record_array_t
svc->pair_n == 0 || svc->has_expr) {
continue;
}
- /* Candidates: the workloads carrying the Service's rarest pair. */
- const k8s_posting_t *rarest = NULL;
- for (int p = 0; p < svc->pair_n; p++) {
- const k8s_posting_t *posting = cbm_ht_get(index, recs->pairs[svc->pair_off + p]);
- if (!posting) {
- rarest = NULL;
- break;
- }
- if (!rarest || posting->count < rarest->count) {
- rarest = posting;
- }
- }
+ /* Candidates: per bundle of the selector, the workloads there
+ * carrying its rarest pair. */
const cbm_gbuf_node_t *sn = cbm_gbuf_find_by_id(ctx->gbuf, svc->node_id);
+ cand.count = 0;
+ if (sn && sn->file_path) {
+ k8s_sel_job_t j = {index, recs, svc, i, &cand};
+ k8s_each_scope(bundles, sn->file_path, false, k8s_sel_rarest, &j);
+ }
+ k8s_ids_sort_unique(&cand);
+ atomic_fetch_add_explicit(&g_k8s_candidates, (uint64_t)cand.count, memory_order_relaxed);
int n = 0;
- for (int c = 0; sn && rarest && c < rarest->count; c++) {
- const k8s_record_t *wl = &recs->items[rarest->items[c]];
+ for (int c = 0; c < cand.count; c++) {
+ int rec = (int)cand.items[c];
+ const k8s_record_t *wl = &recs->items[rec];
bool all = k8s_share_bundle(bundles, ctx, svc, wl);
for (int p = 0; all && p < svc->pair_n; p++) {
all = k8s_has_pair(recs, wl, recs->pairs[svc->pair_off + p]);
@@ -1922,15 +2134,15 @@ static void k8s_link_selectors(cbm_pipeline_ctx_t *ctx, const k8s_record_array_t
const cbm_gbuf_node_t *wn = all ? cbm_gbuf_find_by_id(ctx->gbuf, wl->node_id) : NULL;
if (wn && wn->name && wn->file_path &&
k8s_grow((void **)&matches, &match_cap, n, sizeof(*matches))) {
- matches[n++] = (k8s_match_t){
- wn->name, wl->ns, sn->file_path && strcmp(wn->file_path, sn->file_path) == 0,
- rarest->items[c]};
+ matches[n++] =
+ (k8s_match_t){wn->name, wl->ns, strcmp(wn->file_path, sn->file_path) == 0, rec};
}
}
edges += k8s_draw_matches(ctx, recs, svc, matches, n);
}
cbm_free(CBM_MEM_CLASS_OTHER, matches);
- cbm_ht_foreach(index, k8s_posting_free, NULL);
+ cbm_free(CBM_MEM_CLASS_OTHER, cand.items);
+ cbm_ht_foreach(index, k8s_posting_free_owned, NULL);
cbm_ht_free(index);
if (edges > 0) {
cbm_log_info("pass.k8s.selectors", "linked", itoa_k8s(edges));
diff --git a/src/pipeline/pass_operator.c b/src/pipeline/pass_operator.c
index 0c0bdc468..e4b96e900 100644
--- a/src/pipeline/pass_operator.c
+++ b/src/pipeline/pass_operator.c
@@ -28,6 +28,7 @@
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
+#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -37,6 +38,9 @@
enum { OP_GROW_MIN = 16, OP_IDENT_MAX = 128 };
+/* Candidates examined by the resolvers below (pipeline_internal.h). */
+_Atomic uint64_t g_op_candidates = 0;
+
/* Read a whole file into a core-owned NUL-terminated buffer, NULL on error
* or past the indexer's file-size policy. */
static char *op_read_file(const char *path) {
@@ -251,6 +255,82 @@ static bool op_import_dir(const op_modules_t *m, const char *import, char *out,
return true;
}
+/* ── Indexes: Go types by package dir, module roots ───────────────────── */
+
+/* Graph-owned nodes. */
+typedef struct {
+ const cbm_gbuf_node_t **items;
+ int count;
+ int cap;
+} op_nodes_t;
+
+static void op_nodes_push(op_nodes_t *v, const cbm_gbuf_node_t *n) {
+ if (v->count == v->cap) {
+ int cap = v->cap ? v->cap * PAIR_LEN : OP_GROW_MIN;
+ const cbm_gbuf_node_t **grown =
+ cbm_realloc(CBM_MEM_CLASS_OTHER, (void *)v->items, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ v->items = grown;
+ v->cap = cap;
+ }
+ v->items[v->count++] = n;
+}
+
+static void op_free_nodes_entry(const char *key, void *value, void *ud) {
+ (void)ud;
+ op_nodes_t *v = value;
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)v->items);
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
+/* "<a>\x1f<b>": a directory (or module root) with a name. */
+static void op_key(char *out, size_t out_sz, const char *a, const char *b) {
+ snprintf(out, out_sz, "%s\x1f%s", a, b);
+}
+
+/* "<dir>\x1f<Name>" -> the Struct / Class / Type nodes Name names in dir. */
+static CBMHashTable *op_types_by_dir(const cbm_gbuf_t *gb) {
+ CBMHashTable *ix = cbm_ht_create(0);
+ static const char *const labels[] = {"Struct", "Class", "Type"};
+ for (size_t l = 0; ix && l < sizeof(labels) / sizeof(labels[0]); l++) {
+ const cbm_gbuf_node_t **nodes = NULL;
+ int count = 0;
+ if (cbm_gbuf_find_by_label(gb, labels[l], &nodes, &count) != 0) {
+ continue;
+ }
+ for (int i = 0; i < count; i++) {
+ if (!nodes[i]->file_path || !nodes[i]->name) {
+ continue;
+ }
+ char dir[CBM_SZ_1K];
+ char key[PAIR_LEN * CBM_SZ_1K];
+ op_dirname(nodes[i]->file_path, dir, sizeof(dir));
+ op_key(key, sizeof(key), dir, nodes[i]->name);
+ op_nodes_t *v = cbm_ht_get(ix, key);
+ if (!v) {
+ v = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*v));
+ char *owned = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key);
+ if (!v || !owned) {
+ cbm_free(CBM_MEM_CLASS_OTHER, v);
+ cbm_free(CBM_MEM_CLASS_OTHER, owned);
+ continue;
+ }
+ cbm_ht_set(ix, owned, v);
+ }
+ op_nodes_push(v, nodes[i]);
+ }
+ }
+ return ix;
+}
+
+typedef struct {
+ const op_modules_t *mods;
+ CBMHashTable *dir_types; /* op_types_by_dir */
+} op_index_t;
+
/* ── Controllers: For / Owns / Watches / source.Kind ──────────────────── */
static int op_line_of(const char *text, const char *at) {
@@ -419,30 +499,46 @@ static bool op_dir_is_import(const char *dir, const char *path) {
}
/* HANDLES from `fn` to the Go type `type` resolved as Go resolves it: the
- * package the file imports as `qual`, or (unqualified) the file's own. */
-static int op_handle(cbm_gbuf_t *gb, const op_modules_t *mods, const cbm_gbuf_node_t *fn,
+ * package the file imports as `qual`, or (unqualified) the file's own -- one
+ * directory, looked up in the type index. Only an import no module of the
+ * repository has falls back to the same-named types whose directory ends
+ * the import path. */
+static int op_handle(cbm_gbuf_t *gb, const op_index_t *ix, const cbm_gbuf_node_t *fn,
const char *file, const char *text, const char *qual, const char *type,
const char *role) {
char import[CBM_SZ_1K] = "";
- char own_dir[CBM_SZ_1K];
char pkg_dir[CBM_SZ_1K] = "";
- op_dirname(file, own_dir, sizeof(own_dir));
bool in_repo = false;
if (qual[0]) {
op_import_path(text, qual, import, sizeof(import));
if (!import[0]) {
return 0;
}
- in_repo = op_import_dir(mods, import, pkg_dir, sizeof(pkg_dir));
+ in_repo = op_import_dir(ix->mods, import, pkg_dir, sizeof(pkg_dir));
+ } else {
+ op_dirname(file, pkg_dir, sizeof(pkg_dir));
+ }
+ char props[CBM_SZ_128];
+ snprintf(props, sizeof(props), "{\"via\":\"controller\",\"role\":\"%s\"}", role);
+ int linked = 0;
+ if (!qual[0] || in_repo) {
+ char key[PAIR_LEN * CBM_SZ_1K];
+ op_key(key, sizeof(key), pkg_dir, type);
+ const op_nodes_t *v = cbm_ht_get(ix->dir_types, key);
+ atomic_fetch_add_explicit(&g_op_candidates, (uint64_t)(v ? v->count : 0),
+ memory_order_relaxed);
+ for (int i = 0; v && i < v->count; i++) {
+ cbm_gbuf_insert_edge(gb, fn->id, v->items[i]->id, "HANDLES", props);
+ linked++;
+ }
+ return linked;
}
const cbm_gbuf_node_t **types = NULL;
int tn = 0;
if (cbm_gbuf_find_by_name(gb, type, &types, &tn) != 0) {
return 0;
}
- char props[CBM_SZ_128];
- snprintf(props, sizeof(props), "{\"via\":\"controller\",\"role\":\"%s\"}", role);
- int linked = 0;
+ atomic_fetch_add_explicit(&g_op_candidates, (uint64_t)(tn), memory_order_relaxed);
for (int i = 0; i < tn; i++) {
const cbm_gbuf_node_t *t = types[i];
if (!t->label || !t->file_path ||
@@ -452,10 +548,7 @@ static int op_handle(cbm_gbuf_t *gb, const op_modules_t *mods, const cbm_gbuf_no
}
char dir[CBM_SZ_1K];
op_dirname(t->file_path, dir, sizeof(dir));
- bool same = !qual[0] ? strcmp(dir, own_dir) == 0
- : in_repo ? strcmp(dir, pkg_dir) == 0
- : op_dir_is_import(dir, import);
- if (same) {
+ if (op_dir_is_import(dir, import)) {
cbm_gbuf_insert_edge(gb, fn->id, t->id, "HANDLES", props);
linked++;
}
@@ -464,7 +557,7 @@ static int op_handle(cbm_gbuf_t *gb, const op_modules_t *mods, const cbm_gbuf_no
}
/* Every `&[pkg.]Type{` in the arguments of a controller builder call. */
-static int op_controller_calls(cbm_gbuf_t *gb, CBMHashTable *by_file, const op_modules_t *mods,
+static int op_controller_calls(cbm_gbuf_t *gb, CBMHashTable *by_file, const op_index_t *ix,
const char *file, const char *text) {
/* method name + "(", called on a receiver outside a comment: the previous
* code char is '.', and Kind's receiver is the `source` package. */
@@ -529,7 +622,7 @@ static int op_controller_calls(cbm_gbuf_t *gb, CBMHashTable *by_file, const op_m
snprintf(type, sizeof(type), "%.*s", (int)(q - last), last);
snprintf(qual, sizeof(qual), "%.*s",
last > first ? (int)(last - first - SKIP_ONE) : 0, first);
- linked += op_handle(gb, mods, fn, file, text, qual, type, calls[c].role);
+ linked += op_handle(gb, ix, fn, file, text, qual, type, calls[c].role);
}
}
}
@@ -571,37 +664,42 @@ static void op_strs_push(op_strs_t *v, const char *s) {
}
}
-/* group -> the package directories that declare it (owned keys and lists). */
-static void op_note_group(CBMHashTable *groups, const char *group, const char *dir) {
- if (!group[0]) {
+/* group -> the package directories that declare it (owned keys and lists);
+ * `seen` holds "group\x1fdir" (owned keys) so a repeat is one lookup. */
+static void op_note_group(CBMHashTable *groups, CBMHashTable *seen, const char *group,
+ const char *dir) {
+ char key[PAIR_LEN * CBM_SZ_1K];
+ op_key(key, sizeof(key), group, dir);
+ if (!group[0] || cbm_ht_get(seen, key)) {
return;
}
+ char *seen_key = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key);
+ if (!seen_key) {
+ return;
+ }
+ cbm_ht_set(seen, seen_key, seen_key);
op_strs_t *dirs = cbm_ht_get(groups, group);
if (!dirs) {
dirs = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*dirs));
- char *key = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, group);
- if (!dirs || !key) {
+ char *gkey = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, group);
+ if (!dirs || !gkey) {
cbm_free(CBM_MEM_CLASS_OTHER, dirs);
- cbm_free(CBM_MEM_CLASS_OTHER, key);
- return;
- }
- cbm_ht_set(groups, key, dirs);
- }
- for (int i = 0; i < dirs->count; i++) {
- if (strcmp(dirs->items[i], dir) == 0) {
+ cbm_free(CBM_MEM_CLASS_OTHER, gkey);
return;
}
+ cbm_ht_set(groups, gkey, dirs);
}
op_strs_push(dirs, dir);
}
-static void op_scan_groups(CBMHashTable *groups, const char *file, const char *text) {
+static void op_scan_groups(CBMHashTable *groups, CBMHashTable *seen, const char *file,
+ const char *text) {
char dir[CBM_SZ_1K];
op_dirname(file, dir, sizeof(dir));
for (const char *p = strstr(text, "+groupName="); p; p = strstr(p + SKIP_ONE, "+groupName=")) {
char g[CBM_SZ_256];
op_token(p + OP_SLEN("+groupName="), g, sizeof(g));
- op_note_group(groups, g, dir);
+ op_note_group(groups, seen, g, dir);
}
for (const char *p = strstr(text, "GroupName"); p; p = strstr(p + SKIP_ONE, "GroupName")) {
const char *q = p + OP_SLEN("GroupName");
@@ -618,11 +716,17 @@ static void op_scan_groups(CBMHashTable *groups, const char *file, const char *t
if (*q == '"') {
char g[CBM_SZ_256];
op_token(q + SKIP_ONE, g, sizeof(g));
- op_note_group(groups, g, dir);
+ op_note_group(groups, seen, g, dir);
}
}
}
+static void op_free_seen(const char *key, void *value, void *ud) {
+ (void)value;
+ (void)ud;
+ cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
+}
+
static void op_free_groups(const char *key, void *value, void *ud) {
(void)ud;
op_strs_t *dirs = value;
@@ -651,8 +755,10 @@ static bool op_prop(const char *props, const char *key, char *out, size_t out_sz
return true;
}
-/* CRD -> the Go type declared under its group, in a package marking it. */
-static int op_link_crd_types(cbm_gbuf_t *gb, CBMHashTable *groups) {
+/* CRD -> the Go type declared under its group, in every package marking it:
+ * an API group is one identity, and its CRDs and Go types routinely sit in
+ * different modules (cluster-api's api/, kubermatic's sdk/). */
+static int op_link_crd_types(cbm_gbuf_t *gb, const op_index_t *ix, CBMHashTable *groups) {
const cbm_gbuf_node_t **res = NULL;
int count = 0;
int linked = 0;
@@ -662,29 +768,25 @@ static int op_link_crd_types(cbm_gbuf_t *gb, CBMHashTable *groups) {
for (int i = 0; i < count; i++) {
char group[CBM_SZ_256];
char kind[CBM_SZ_256];
- if (!op_prop(res[i]->properties_json, "crd_group", group, sizeof(group)) ||
+ if (!res[i]->file_path ||
+ !op_prop(res[i]->properties_json, "crd_group", group, sizeof(group)) ||
!op_prop(res[i]->properties_json, "crd_kind", kind, sizeof(kind))) {
continue;
}
+ char key[PAIR_LEN * CBM_SZ_1K];
const op_strs_t *dirs = cbm_ht_get(groups, group);
- const cbm_gbuf_node_t **types = NULL;
- int tn = 0;
- if (!dirs || cbm_gbuf_find_by_name(gb, kind, &types, &tn) != 0) {
+ if (!dirs) {
continue;
}
- for (int t = 0; t < tn; t++) {
- if (!types[t]->label || strcmp(types[t]->label, "Struct") != 0 ||
- !types[t]->file_path) {
- continue;
- }
- char dir[CBM_SZ_1K];
- op_dirname(types[t]->file_path, dir, sizeof(dir));
- for (int d = 0; d < dirs->count; d++) {
- if (strcmp(dirs->items[d], dir) == 0) {
- cbm_gbuf_insert_edge(gb, res[i]->id, types[t]->id, "USAGE",
+ atomic_fetch_add_explicit(&g_op_candidates, (uint64_t)dirs->count, memory_order_relaxed);
+ for (int d = 0; d < dirs->count; d++) {
+ op_key(key, sizeof(key), dirs->items[d], kind);
+ const op_nodes_t *v = cbm_ht_get(ix->dir_types, key);
+ for (int t = 0; v && t < v->count; t++) {
+ if (v->items[t]->label && strcmp(v->items[t]->label, "Struct") == 0) {
+ cbm_gbuf_insert_edge(gb, res[i]->id, v->items[t]->id, "USAGE",
"{\"ref\":\"crd-type\"}");
linked++;
- break;
}
}
}
@@ -886,6 +988,7 @@ int cbm_pipeline_pass_operator(cbm_pipeline_ctx_t *ctx) {
qsort((void *)files, (size_t)nfiles, sizeof(*files), op_cmp_path);
CBMHashTable *by_file = op_functions_by_file(gb);
CBMHashTable *groups = cbm_ht_create(0);
+ CBMHashTable *seen = cbm_ht_create(0);
op_modules_t mods = {0};
for (int i = 0; i < nfiles; i++) {
const char *slash = strrchr(files[i]->file_path, '/');
@@ -899,9 +1002,11 @@ int cbm_pipeline_pass_operator(cbm_pipeline_ctx_t *ctx) {
}
}
}
+ op_index_t ix = {.mods = &mods, .dir_types = op_types_by_dir(gb)};
int handles = 0;
int embeds = 0;
- for (int i = 0; by_file && groups && i < nfiles; i++) {
+ bool ready = by_file && groups && seen && ix.dir_types;
+ for (int i = 0; ready && i < nfiles; i++) {
if (!op_is_go(files[i]->file_path) || cbm_pipeline_check_cancel(ctx)) {
continue;
}
@@ -912,20 +1017,28 @@ int cbm_pipeline_pass_operator(cbm_pipeline_ctx_t *ctx) {
continue;
}
if (strstr(text, "controller-runtime") || strstr(text, "source.Kind(")) {
- handles += op_controller_calls(gb, by_file, &mods, files[i]->file_path, text);
+ handles += op_controller_calls(gb, by_file, &ix, files[i]->file_path, text);
}
- op_scan_groups(groups, files[i]->file_path, text);
+ op_scan_groups(groups, seen, files[i]->file_path, text);
if (strstr(text, "//go:embed ")) {
embeds += op_embed(gb, files[i], text, files, nfiles);
}
cbm_free(CBM_MEM_CLASS_OTHER, text);
}
- int crd_types = groups ? op_link_crd_types(gb, groups) : 0;
+ int crd_types = ready ? op_link_crd_types(gb, &ix, groups) : 0;
+ if (ix.dir_types) {
+ cbm_ht_foreach(ix.dir_types, op_free_nodes_entry, NULL);
+ cbm_ht_free(ix.dir_types);
+ }
op_modules_free(&mods);
if (groups) {
cbm_ht_foreach(groups, op_free_groups, NULL);
cbm_ht_free(groups);
}
+ if (seen) {
+ cbm_ht_foreach(seen, op_free_seen, NULL);
+ cbm_ht_free(seen);
+ }
if (by_file) {
cbm_ht_foreach(by_file, op_free_spans, NULL);
cbm_ht_free(by_file);
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 19a9d3a08..7b20b2787 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -1118,6 +1118,13 @@ extern _Atomic uint64_t g_importance_name_visits;
* closure-delta route actually took that path instead of passing vacuously. */
extern _Atomic uint64_t g_importance_store_rows;
+/* Work counters of the infra resolvers, gated by the complexity suite:
+ * candidate objects the K8s pass examines for references, selectors and
+ * CRDs (pass_k8s.c), and candidate Go types / package dirs the operator pass
+ * examines (pass_operator.c). Independent copies must grow them linearly. */
+extern _Atomic uint64_t g_k8s_candidates;
+extern _Atomic uint64_t g_op_candidates;
+
/* ── Incremental pipeline (pipeline_incremental.c) ───────────────── */
/* Run incremental re-index on an existing disk DB.
diff --git a/tests/fixtures/complexity/Go/controller.go b/tests/fixtures/complexity/Go/controller.go
new file mode 100644
index 000000000..bc88e26b7
--- /dev/null
+++ b/tests/fixtures/complexity/Go/controller.go
@@ -0,0 +1,14 @@
+package op
+
+import (
+ ctrl "sigs.k8s.io/controller-runtime"
+)
+
+type WidgetReconciler struct{}
+
+func (r *WidgetReconciler) SetupWithManager(mgr ctrl.Manager) error {
+ return ctrl.NewControllerManagedBy(mgr).
+ For(&Widget{}).
+ Owns(&Gadget{}).
+ Complete(r)
+}
diff --git a/tests/fixtures/complexity/Go/crd.yaml b/tests/fixtures/complexity/Go/crd.yaml
new file mode 100644
index 000000000..9e1d2174f
--- /dev/null
+++ b/tests/fixtures/complexity/Go/crd.yaml
@@ -0,0 +1,10 @@
+apiVersion: apiextensions.k8s.io/v1
+kind: CustomResourceDefinition
+metadata:
+ name: widgets.embedded.example.org
+spec:
+ group: embedded.example.org
+ names:
+ kind: Widget
+ plural: widgets
+ scope: Namespaced
diff --git a/tests/fixtures/complexity/Go/embed.go b/tests/fixtures/complexity/Go/embed.go
new file mode 100644
index 000000000..99d9f2142
--- /dev/null
+++ b/tests/fixtures/complexity/Go/embed.go
@@ -0,0 +1,6 @@
+package op
+
+import "embed"
+
+//go:embed crd.yaml
+var CRDs embed.FS
diff --git a/tests/fixtures/complexity/Go/go.mod b/tests/fixtures/complexity/Go/go.mod
new file mode 100644
index 000000000..2e5105206
--- /dev/null
+++ b/tests/fixtures/complexity/Go/go.mod
@@ -0,0 +1,3 @@
+module example.com/op
+
+go 1.22
diff --git a/tests/fixtures/complexity/Go/types.go b/tests/fixtures/complexity/Go/types.go
new file mode 100644
index 000000000..625acb8e5
--- /dev/null
+++ b/tests/fixtures/complexity/Go/types.go
@@ -0,0 +1,10 @@
+// +groupName=example.com
+package op
+
+type Widget struct {
+ Name string
+}
+
+type Gadget struct {
+ Name string
+}
diff --git a/tests/fixtures/complexity/Kubernetes/app.yaml b/tests/fixtures/complexity/Kubernetes/app.yaml
new file mode 100644
index 000000000..c4d0a839d
--- /dev/null
+++ b/tests/fixtures/complexity/Kubernetes/app.yaml
@@ -0,0 +1,117 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: web
+spec:
+ selector:
+ matchLabels:
+ app: web
+ template:
+ metadata:
+ labels:
+ app: web
+ spec:
+ serviceAccountName: web
+ volumes:
+ - name: config
+ configMap:
+ name: web-config
+ containers:
+ - name: web
+ image: example.com/web:1.0
+ envFrom:
+ - secretRef:
+ name: web-env
+---
+apiVersion: v1
+kind: Service
+metadata:
+ name: web
+spec:
+ selector:
+ app: web
+ ports:
+ - port: 80
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: web-config
+data:
+ mode: production
+---
+apiVersion: v1
+kind: Secret
+metadata:
+ name: web-env
+type: Opaque
+---
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+ name: web
+---
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+ name: web
+spec:
+ rules:
+ - host: web.example.com
+ http:
+ paths:
+ - path: /api
+ pathType: Prefix
+ backend:
+ service:
+ name: web
+ port:
+ number: 80
+---
+apiVersion: policy/v1
+kind: PodDisruptionBudget
+metadata:
+ name: web
+spec:
+ minAvailable: 1
+ selector:
+ matchLabels:
+ app: web
+---
+apiVersion: networking.k8s.io/v1
+kind: NetworkPolicy
+metadata:
+ name: web
+spec:
+ podSelector:
+ matchLabels:
+ app: web
+---
+apiVersion: autoscaling/v2
+kind: HorizontalPodAutoscaler
+metadata:
+ name: web
+spec:
+ scaleTargetRef:
+ apiVersion: apps/v1
+ kind: Deployment
+ name: web
+ minReplicas: 1
+ maxReplicas: 3
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: Role
+metadata:
+ name: web
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: RoleBinding
+metadata:
+ name: web
+roleRef:
+ apiGroup: rbac.authorization.k8s.io
+ kind: Role
+ name: web
+subjects:
+ - kind: ServiceAccount
+ name: web
diff --git a/tests/fixtures/complexity/Kubernetes/crd.yaml b/tests/fixtures/complexity/Kubernetes/crd.yaml
new file mode 100644
index 000000000..e2617023a
--- /dev/null
+++ b/tests/fixtures/complexity/Kubernetes/crd.yaml
@@ -0,0 +1,37 @@
+apiVersion: apiextensions.k8s.io/v1
+kind: CustomResourceDefinition
+metadata:
+ name: widgets.k8s.example.org
+spec:
+ group: k8s.example.org
+ names:
+ kind: Widget
+ plural: widgets
+ scope: Namespaced
+---
+apiVersion: k8s.example.org/v1
+kind: Widget
+metadata:
+ name: sample
+---
+apiVersion: cert-manager.io/v1
+kind: Issuer
+metadata:
+ name: web
+spec:
+ ca:
+ secretName: web-ca
+---
+apiVersion: v1
+kind: Secret
+metadata:
+ name: web-ca
+---
+apiVersion: cert-manager.io/v1
+kind: Certificate
+metadata:
+ name: web-tls
+spec:
+ secretName: web-tls
+ issuerRef:
+ name: web
diff --git a/tests/fixtures/complexity/Kubernetes/kustomization.yaml b/tests/fixtures/complexity/Kubernetes/kustomization.yaml
new file mode 100644
index 000000000..b8ebf3724
--- /dev/null
+++ b/tests/fixtures/complexity/Kubernetes/kustomization.yaml
@@ -0,0 +1,6 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+namespace: shop
+resources:
+ - app.yaml
+ - crd.yaml
diff --git a/tests/test_complexity.c b/tests/test_complexity.c
index f20eac16e..16c707f56 100644
--- a/tests/test_complexity.c
+++ b/tests/test_complexity.c
@@ -67,6 +67,9 @@
* lsp_resolve.h — re-declared here to avoid pulling that header's statics). */
extern _Atomic uint64_t g_lsp_tail_lookups;
extern _Atomic uint64_t g_lsp_tail_candidates;
+/* Infra resolver candidate counters (pass_k8s.c, pass_operator.c). */
+extern _Atomic uint64_t g_k8s_candidates;
+extern _Atomic uint64_t g_op_candidates;
/* ── Corpus scale ──────────────────────────────────────────────────────
* Small on purpose: the gate reads exponents from ratios, not magnitudes.
@@ -230,15 +233,8 @@ static int cx_build_corpus(const char *root, int k_modules) {
* k module copies. Distinct paths make distinct modules/QNs, which is all
* the independence argument needs. */
for (int lang = 0; lang < CBM_LANG_COUNT; lang++) {
- bool templated = false;
- for (int t = 0; t < CX_TEMPLATE_COUNT; t++) {
- if (CX_TEMPLATES[t].lang == (CBMLanguage)lang) {
- templated = true;
- }
- }
- if (templated) {
- continue;
- }
+ /* a templated language may add fixtures too (Go: the operator
+ * corpus); they land in their own fx_<lang> root */
char fixdir[512];
if (!cx_fixture_dir_for((CBMLanguage)lang, fixdir, sizeof(fixdir))) {
continue;
@@ -342,6 +338,16 @@ static void cx_pass_sink(const char *line) {
g_cx_passes[n].ms = atol(ee + 11);
}
+/* Fixture-provided infra corpora (tests/fixtures/complexity/<name>/), scoped
+ * by their copy root: Kubernetes manifests with a kustomization, and a Go
+ * operator (controller, API types with +groupName, an embedded CRD). An API
+ * group/kind is one identity across a repository: copies declaring the same
+ * one ARE joined (a CRD links to every package marking its group), so the
+ * fixtures' CRDs use groups no copy's Go code declares -- the guard measures
+ * accidental growth, not that by-design join. */
+static const char *const CX_INFRA_DIRS[] = {"fx_Kubernetes", "fx_Go"};
+enum { CX_INFRA_COUNT = sizeof(CX_INFRA_DIRS) / sizeof(CX_INFRA_DIRS[0]) };
+
typedef struct {
int nodes;
int edges;
@@ -357,6 +363,10 @@ typedef struct {
uint64_t fallback_rows;
uint64_t imp_nodes; /* symbols scored by the importance pass */
uint64_t imp_name_visits; /* same-name-group members visited by that pass */
+ uint64_t k8s_candidates; /* objects the K8s resolvers examined */
+ uint64_t op_candidates; /* Go types / dirs the operator pass examined */
+ int infra_nodes[CX_INFRA_COUNT];
+ int infra_edges[CX_INFRA_COUNT];
double wall_s;
} CxMetrics;
@@ -379,6 +389,8 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
uint64_t fb0 = cbm_pp_lsp_linear_fallback_rows();
uint64_t in0 = atomic_load_explicit(&g_importance_nodes, memory_order_relaxed);
uint64_t iv0 = atomic_load_explicit(&g_importance_name_visits, memory_order_relaxed);
+ uint64_t kc0 = atomic_load_explicit(&g_k8s_candidates, memory_order_relaxed);
+ uint64_t oc0 = atomic_load_explicit(&g_op_candidates, memory_order_relaxed);
atomic_store_explicit(&g_cx_pass_count, 0, memory_order_relaxed);
cbm_log_set_sink_ex(cx_pass_sink, CBM_LOG_SINK_TEE);
@@ -416,6 +428,8 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
out->imp_nodes = atomic_load_explicit(&g_importance_nodes, memory_order_relaxed) - in0;
out->imp_name_visits =
atomic_load_explicit(&g_importance_name_visits, memory_order_relaxed) - iv0;
+ out->k8s_candidates = atomic_load_explicit(&g_k8s_candidates, memory_order_relaxed) - kc0;
+ out->op_candidates = atomic_load_explicit(&g_op_candidates, memory_order_relaxed) - oc0;
cbm_store_t *s = cbm_store_open_path(db_path);
if (!s) {
@@ -427,6 +441,10 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
out->lang_nodes[t] = cbm_store_count_nodes_scoped(s, project, CX_TEMPLATES[t].dirname);
out->lang_edges[t] = cbm_store_count_edges_scoped(s, project, CX_TEMPLATES[t].dirname);
}
+ for (int i = 0; i < CX_INFRA_COUNT; i++) {
+ out->infra_nodes[i] = cbm_store_count_nodes_scoped(s, project, CX_INFRA_DIRS[i]);
+ out->infra_edges[i] = cbm_store_count_edges_scoped(s, project, CX_INFRA_DIRS[i]);
+ }
cbm_store_close(s);
return 0;
}
@@ -529,6 +547,41 @@ TEST(complexity_replicated_modules_scale_linearly) {
PASS();
}
+/* The infra resolvers (K8s references / selectors / CRDs, the Go operator's
+ * controller kinds and CRD types) examine candidates by name. Independent
+ * copies share every name, so a candidate set that is not scoped to the
+ * bundle / package / module grows with the copy count: the examined count
+ * goes ~4x per doubling while the edges stay ~2x. Both are gated, on the
+ * fixture corpora's own subtrees, and neither may be vacuous. */
+TEST(complexity_infra_resolution_is_linear) {
+ if (cx_measure_pair() != 0) {
+ FAIL("failed to build/run the complexity corpus pair");
+ }
+ const CxMetrics *a = &g_cx_base;
+ const CxMetrics *b = &g_cx_doubled;
+ printf(" k8s_candidates %llu -> %llu op_candidates %llu -> %llu\n",
+ (unsigned long long)a->k8s_candidates, (unsigned long long)b->k8s_candidates,
+ (unsigned long long)a->op_candidates, (unsigned long long)b->op_candidates);
+ ASSERT_GT((long long)a->k8s_candidates, 0);
+ ASSERT_GT((long long)a->op_candidates, 0);
+ double kr = cx_ratio((double)b->k8s_candidates, (double)a->k8s_candidates);
+ double orr = cx_ratio((double)b->op_candidates, (double)a->op_candidates);
+ printf(" k8s_candidates ratio %.2f op_candidates ratio %.2f\n", kr, orr);
+ ASSERT_TRUE(kr >= CX_RATIO_LO && kr <= CX_RATIO_HI);
+ ASSERT_TRUE(orr >= CX_RATIO_LO && orr <= CX_RATIO_HI);
+ for (int i = 0; i < CX_INFRA_COUNT; i++) {
+ ASSERT_GT(a->infra_nodes[i], 0);
+ ASSERT_GT(a->infra_edges[i], 0);
+ double nr = cx_ratio(b->infra_nodes[i], a->infra_nodes[i]);
+ double er = cx_ratio(b->infra_edges[i], a->infra_edges[i]);
+ printf(" %-14s nodes %d -> %d (%.2f) edges %d -> %d (%.2f)\n", CX_INFRA_DIRS[i],
+ a->infra_nodes[i], b->infra_nodes[i], nr, a->infra_edges[i], b->infra_edges[i], er);
+ ASSERT_TRUE(nr >= CX_RATIO_LO && nr <= CX_RATIO_HI);
+ ASSERT_TRUE(er >= CX_RATIO_LO && er <= CX_RATIO_HI);
+ }
+ PASS();
+}
+
/* Σ per-file registry work must be linear in independent copies. This is the
* counter that measured ~4x/doubling on the v0.10.x Java path (defs_per_file
* tracked defs_total — #1669). Guarded against vacuous passes: the base run
@@ -845,6 +898,7 @@ SUITE(complexity) {
RUN_TEST(complexity_rescript_error_recovery_lexing_is_linear);
RUN_TEST(complexity_replicated_modules_scale_linearly);
RUN_TEST(complexity_perfile_registry_work_is_linear);
+ RUN_TEST(complexity_infra_resolution_is_linear);
RUN_TEST(complexity_importance_scoring_is_linear);
RUN_TEST(complexity_shared_package_growth_stays_linear);
RUN_TEST(complexity_throughput_report_written);