Skip to content

armgit / Ix

Public Imported from GitHub
0

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

perf(rank): score candidates through a sliding window, not lockstep batches

KageBinaryb790d5083257
diff --git a/ix-cli/src/cli/__tests__/rank-path-scope.test.ts b/ix-cli/src/cli/__tests__/rank-path-scope.test.ts
index 482dd6f7..c4525d5a 100644
--- a/ix-cli/src/cli/__tests__/rank-path-scope.test.ts
+++ b/ix-cli/src/cli/__tests__/rank-path-scope.test.ts
@@ -58,3 +58,39 @@ describe("rank path filtering before the candidate limit", () => {
expect(expand).not.toHaveBeenCalled();
});
});
+
+describe("rank scoring order", () => {
+ afterEach(() => vi.restoreAllMocks());
+
+ it("scores every candidate once and reports in candidate order, however the expands finish", async () => {
+ const nodes = Array.from({ length: 45 }, (_, i) => ({ id: `n${i}`, name: `N${i}`, kind: "class", provenance: { sourceUri: "src/x.ts" } }));
+ listByKind.mockReset().mockResolvedValue(nodes);
+ let inFlight = 0;
+ let peak = 0;
+ expand.mockReset().mockImplementation(async (id: string) => {
+ inFlight++;
+ peak = Math.max(peak, inFlight);
+ const i = Number(id.slice(1));
+ // Later candidates answer first; every fifth one fails.
+ await new Promise((resolve) => setTimeout(resolve, (45 - i) % 7));
+ inFlight--;
+ if (i % 5 === 0) throw new Error("down");
+ // Score i % 4, with a duplicate id that must count once.
+ const members = Array.from({ length: i % 4 }, (_, k) => ({ id: `m${k}` }));
+ return { nodes: [...members, ...members.slice(0, 1)], edges: [] };
+ });
+
+ const output = await run(["--top", "45"]);
+
+ expect(expand).toHaveBeenCalledTimes(45);
+ expect(peak).toBeLessThanOrEqual(20);
+ // A stable sort of scores computed in candidate order: ties keep that order.
+ const expected = nodes
+ .map((n, i) => ({ name: n.name, kind: "class", score: i % 5 === 0 ? 0 : i % 4 }))
+ .sort((a, b) => b.score - a.score);
+ expect(output.results).toEqual(expected);
+ expect(output.diagnostics).toEqual(
+ nodes.filter((_, i) => i % 5 === 0).map((n) => `Failed to expand entity ${n.id}`),
+ );
+ });
+});
diff --git a/ix-cli/src/cli/commands/rank.ts b/ix-cli/src/cli/commands/rank.ts
index d96e2bdb..5c590c23 100644
--- a/ix-cli/src/cli/commands/rank.ts
+++ b/ix-cli/src/cli/commands/rank.ts
@@ -8,6 +8,7 @@ import { activeReadScope, ensureReadScope } from "../resolve.js";
import { llmLine } from "../llm.js";
import { normalizePathSeparators } from "../path-match.js";
import { printJson } from "../format.js";
+import { mapLimit } from "../../client/request-memo.js";
type Metric = "dependents" | "callers" | "importers" | "members";
@@ -94,7 +95,15 @@ interface ScoredEntity {
score: number;
}
-/** Score all candidates in batches to avoid overwhelming the backend */
+/**
+ * Requests in flight while scoring. The same peak load as the old batches of
+ * 20, but a sliding window: a batch waited for its slowest expand before the
+ * next one started, so one slow node held nineteen slots idle. Measured on the
+ * Ix repository's graph (1284 functions): 4.3 s to ~1.9 s, same requests.
+ */
+const SCORE_CONCURRENCY = 20;
+
+/** Score all candidates, a bounded number at a time, in candidate order. */
async function scoreAllCandidates(
candidates: any[],
config: { direction: string; predicates: string[] },
@@ -102,38 +111,35 @@ async function scoreAllCandidates(
diagnostics: string[],
entityKind: string,
): Promise<ScoredEntity[]> {
- const batchSize = 20;
- const results: ScoredEntity[] = [];
- for (let i = 0; i < candidates.length; i += batchSize) {
- const batch = candidates.slice(i, i + batchSize);
- const batchResults = await Promise.all(
- batch.map(async (node: any) => {
- try {
- const result = await client.expand(node.id, {
- direction: config.direction,
- predicates: config.predicates,
- });
- const seen = new Set<string>();
- for (const n of result.nodes) seen.add(n.id);
- return {
- id: node.id,
- name: node.name || node.attrs?.name || "(unnamed)",
- kind: node.kind || entityKind,
- score: seen.size,
- };
- } catch {
- diagnostics.push(`Failed to expand entity ${node.id}`);
- return {
- id: node.id,
- name: node.name || node.attrs?.name || "(unnamed)",
- kind: node.kind || entityKind,
- score: 0,
- };
- }
- })
- );
- results.push(...batchResults);
- }
+ const failed = new Array<boolean>(candidates.length).fill(false);
+ const results = await mapLimit(candidates, SCORE_CONCURRENCY, async (node: any, index): Promise<ScoredEntity> => {
+ try {
+ const result = await client.expand(node.id, {
+ direction: config.direction,
+ predicates: config.predicates,
+ });
+ const seen = new Set<string>();
+ for (const n of result.nodes) seen.add(n.id);
+ return {
+ id: node.id,
+ name: node.name || node.attrs?.name || "(unnamed)",
+ kind: node.kind || entityKind,
+ score: seen.size,
+ };
+ } catch {
+ failed[index] = true;
+ return {
+ id: node.id,
+ name: node.name || node.attrs?.name || "(unnamed)",
+ kind: node.kind || entityKind,
+ score: 0,
+ };
+ }
+ });
+ // In candidate order, not in the order the failures happened to land.
+ candidates.forEach((node: any, index) => {
+ if (failed[index]) diagnostics.push(`Failed to expand entity ${node.id}`);
+ });
return results;
}