Skip to content

Pre-build adjacency list for relation graph traversal #5

@devin-ai-integration

Description

@devin-ai-integration

Objective

Pre-build an adjacency list (Map<string, RelationEdge[]>) during compilation or at query-engine initialization so that buildFrontier() uses O(1) per-node lookups instead of scanning the full relationGraph array.

Current behavior: buildFrontier() iterates over the entire relationGraph for every selected node — O(selected_nodes × total_edges).

Proposed fix:

const adjacency = new Map<string, RelationEdge[]>();
for (const edge of snapshot.relationGraph) {
  if (!adjacency.has(edge.source)) adjacency.set(edge.source, []);
  adjacency.get(edge.source)!.push(edge);
  if (!adjacency.has(edge.target)) adjacency.set(edge.target, []);
  adjacency.get(edge.target)!.push(edge);
}

Then buildFrontier() calls adjacency.get(nodeId) instead of scanning.

"Done" looks like: buildFrontier() uses a pre-built adjacency list; same results, faster execution; no API changes.

Kind

refactor

Affected area

src/runtime/ (compiler & query engine)

Acceptance criteria

  • Adjacency list built once from snapshot.relationGraph
  • buildFrontier() uses map lookup instead of full array scan
  • No behavioral change — identical results
  • All existing tests pass

Agent-delegable?

yes

Additional context

  • src/runtime/query-engine.ts: buildFrontier() method (line ~865), full scan at line ~912
  • src/runtime/types.ts: RelationEdge type
  • Low complexity, self-contained change with no API surface changes

Measurable impact

Metric Before After (target) How to measure
buildFrontier() uses adjacency map lookup No (full array scan) Yes (O(1) per-node) grep -c 'adjacency.get' src/runtime/query-engine.ts (before: 0, after: ≥1)
Full relationGraph iteration in buildFrontier() 1 (line ~912: for...of snapshot.relationGraph) 0 grep -c 'for.*of.*\.relationGraph' src/runtime/query-engine.ts
Existing test pass rate all pass all pass (no regressions) bun run test
Behavioral change N/A none — identical query results bun run cli -- query --question "What is U.BoundedContext?" --mode verbose (diff output before/after)

Metadata

Metadata

Assignees

Labels

taskA concrete work item — research, refactor, chore, or agent-delegable task

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions