Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

perf(impact): bucket the nodes impact already holds, in input order

KageBinary1f73065eab65
diff --git a/ix-cli/src/cli/__tests__/hierarchy.test.ts b/ix-cli/src/cli/__tests__/hierarchy.test.ts
index 49d15b95..05c69e9a 100644
--- a/ix-cli/src/cli/__tests__/hierarchy.test.ts
+++ b/ix-cli/src/cli/__tests__/hierarchy.test.ts
@@ -1,7 +1,7 @@
// Copyright 2026 Ix Infrastructure Inc.
import { describe, expect, it, vi } from "vitest";
-import { getEffectiveSystemPath, hasMapData } from "../hierarchy.js";
+import { bucketByHierarchy, getEffectiveSystemPath, hasMapData } from "../hierarchy.js";
describe("getEffectiveSystemPath", () => {
it("uses the direct system path when it already has map data", async () => {
@@ -80,3 +80,42 @@ describe("getEffectiveSystemPath", () => {
expect(hasMapData(path)).toBe(false);
});
});
+
+describe("bucketByHierarchy", () => {
+ // Regions answer in reverse input order, so a result built in completion
+ // order -- as it once was -- comes out reversed.
+ function regionClient() {
+ const regionOf: Record<string, string> = { a: "r1", b: "r2", c: "r1", d: "r3" };
+ const delay: Record<string, number> = { a: 30, b: 20, c: 10, d: 0 };
+ const entity = vi.fn(async (id: string) => ({ node: { id, name: `entity-${id}`, kind: "function" }, claims: [], edges: [] }));
+ const expand = vi.fn(async (id: string) => {
+ await new Promise((resolve) => setTimeout(resolve, delay[id]));
+ const region = regionOf[id];
+ return { nodes: [{ id: "sys", kind: "system", name: "System" }, { id: region, kind: "region", name: region.toUpperCase() }], edges: [] };
+ });
+ return { client: { entity, expand } as any, entity, expand };
+ }
+
+ it("orders buckets and their members by input, not by which lookup finished first", async () => {
+ const { client } = regionClient();
+ const nodes = ["a", "b", "c", "d"].map((id) => ({ id, name: id, kind: "function" }));
+
+ const buckets = await bucketByHierarchy(client, nodes);
+
+ expect(buckets).toEqual([
+ { region: { name: "R1", kind: "region" }, members: [{ name: "a", kind: "function" }, { name: "c", kind: "function" }] },
+ { region: { name: "R2", kind: "region" }, members: [{ name: "b", kind: "function" }] },
+ { region: { name: "R3", kind: "region" }, members: [{ name: "d", kind: "function" }] },
+ ]);
+ });
+
+ it("reads no entity for a node the caller already holds, and one for a bare id", async () => {
+ const { client, entity, expand } = regionClient();
+
+ const buckets = await bucketByHierarchy(client, [{ id: "a", name: "a", kind: "function" }, "b"]);
+
+ expect(entity.mock.calls.map((c) => c[0])).toEqual(["b"]);
+ expect(expand).toHaveBeenCalledTimes(2);
+ expect(buckets.flatMap((b) => b.members.map((m) => m.name))).toEqual(["a", "entity-b"]);
+ });
+});
diff --git a/ix-cli/src/cli/commands/impact.ts b/ix-cli/src/cli/commands/impact.ts
index 0f06497a..a6ad1c8a 100644
--- a/ix-cli/src/cli/commands/impact.ts
+++ b/ix-cli/src/cli/commands/impact.ts
@@ -328,7 +328,18 @@ async function containerImpact(
}
});
- const callerResults = await Promise.all(callerPromises);
+ // Bucket all dependents by hierarchy. First occurrence of each id wins, as
+ // the id list this replaced did; the nodes go in whole so their names and
+ // kinds need no second read.
+ const uniqueDependents = new Map<string, any>();
+ for (const n of [...directImporters, ...directDependents]) {
+ if (!uniqueDependents.has(n.id)) uniqueDependents.set(n.id, n);
+ }
+ // Independent of the member counts, so the two run together.
+ const [callerResults, propagationBuckets] = await Promise.all([
+ Promise.all(callerPromises),
+ uniqueDependents.size > 0 ? bucketByHierarchy(client, [...uniqueDependents.values()]) : Promise.resolve([]),
+ ]);
for (const r of callerResults) {
memberCallerCounts.push(r);
totalMemberCallers += r.callerCount;
@@ -337,16 +348,6 @@ async function containerImpact(
memberCallerCounts.sort((a, b) => b.callerCount - a.callerCount);
const topMembers = memberCallerCounts.filter((m) => m.callerCount > 0).slice(0, limit);
- // Bucket all dependents by hierarchy
- const allDependentIds = [
- ...directImporters.map((n: any) => n.id),
- ...directDependents.map((n: any) => n.id),
- ];
- const uniqueDependentIds = [...new Set(allDependentIds)];
- const propagationBuckets = uniqueDependentIds.length > 0
- ? await bucketByHierarchy(client, uniqueDependentIds)
- : [];
-
const systemPathMapped = systemPath.map((n) => ({ name: n.name, kind: n.kind }));
// Infer risk semantics
@@ -478,9 +479,8 @@ async function leafImpact(
}));
// Bucket callers by hierarchy
- const callerIds = callersResult.nodes.map((n: any) => n.id);
- const propagationBuckets = callerIds.length > 0
- ? await bucketByHierarchy(client, callerIds)
+ const propagationBuckets = callersResult.nodes.length > 0
+ ? await bucketByHierarchy(client, callersResult.nodes)
: [];
const systemPathMapped = systemPath.map((n) => ({ name: n.name, kind: n.kind }));
diff --git a/ix-cli/src/cli/hierarchy.ts b/ix-cli/src/cli/hierarchy.ts
index 3b55f569..f778a173 100644
--- a/ix-cli/src/cli/hierarchy.ts
+++ b/ix-cli/src/cli/hierarchy.ts
@@ -96,21 +96,36 @@ export async function getEffectiveSystemPath(
return directPath;
}
-/** Group a set of node IDs by their containing region (IN_REGION). */
+/** A node to bucket: its id, or the node itself when the caller already holds it. */
+export type BucketInput = string | { id: string; name?: string; kind?: string; attrs?: { name?: string } };
+
+/**
+ * Group a set of nodes by their containing region (IN_REGION).
+ *
+ * A caller that already holds the nodes -- every caller does: they come from
+ * an expand -- passes them rather than their ids, and the entity read per
+ * node, which only fetched the name and kind the caller already had, is
+ * skipped. On `ix impact IxClient` that was 69 of the command's 166 requests.
+ *
+ * Buckets, and the members in each, are in input order. They used to be in
+ * the order the lookups finished, so the same graph printed differently from
+ * one run to the next.
+ */
export async function bucketByHierarchy(
client: IxClient,
- nodeIds: string[],
+ nodes: BucketInput[],
): Promise<Array<{ region: { name: string; kind: string }; members: Array<{ name: string; kind: string }> }>> {
- if (nodeIds.length === 0) return [];
+ if (nodes.length === 0) return [];
// For each node, find its immediate region
- const buckets = new Map<string, { region: { name: string; kind: string }; members: Array<{ name: string; kind: string }> }>();
-
- await Promise.all(
- nodeIds.map(async (nodeId) => {
+ const kindOrder: Record<string, number> = { file: 0, module: 1, region: 1, subsystem: 2, system: 3 };
+ const placed = await Promise.all(
+ nodes.map(async (input) => {
+ const nodeId = typeof input === "string" ? input : input.id;
try {
- const nodeDetails = await client.entity(nodeId);
- const node = nodeDetails.node as any;
+ const node = typeof input === "string" || !input.kind
+ ? ((await client.entity(nodeId)).node as any)
+ : input;
const memberName = node.name || node.attrs?.name || "(unnamed)";
const memberKind = node.kind || "unknown";
@@ -118,23 +133,24 @@ export async function bucketByHierarchy(
const regionNodes = regionResult.nodes as Array<{ id: string; name?: string; attrs?: { name?: string }; kind?: string }>;
// Pick the most specific region (lowest in hierarchy)
- const kindOrder: Record<string, number> = { file: 0, module: 1, region: 1, subsystem: 2, system: 3 };
const region = regionNodes.sort((a, b) => (kindOrder[(a as any).kind ?? ""] ?? 4) - (kindOrder[(b as any).kind ?? ""] ?? 4))[0];
-
- if (region) {
- const regionName = (region as any).name || region.attrs?.name || "(unnamed)";
- const regionKind = (region as any).kind || "region";
- const key = region.id;
- if (!buckets.has(key)) {
- buckets.set(key, { region: { name: regionName, kind: regionKind }, members: [] });
- }
- buckets.get(key)!.members.push({ name: memberName, kind: memberKind });
- }
+ if (!region) return undefined;
+ return {
+ key: region.id,
+ region: { name: (region as any).name || region.attrs?.name || "(unnamed)", kind: (region as any).kind || "region" },
+ member: { name: memberName, kind: memberKind },
+ };
} catch {
- /* skip nodes that can't be expanded */
+ return undefined; /* skip nodes that can't be expanded */
}
}),
);
+ const buckets = new Map<string, { region: { name: string; kind: string }; members: Array<{ name: string; kind: string }> }>();
+ for (const p of placed) {
+ if (!p) continue;
+ if (!buckets.has(p.key)) buckets.set(p.key, { region: p.region, members: [] });
+ buckets.get(p.key)!.members.push(p.member);
+ }
return [...buckets.values()];
}