Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/core/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ export class Graph {
});
}

/**
* Memory-efficient iteration over all edges without allocating intermediate arrays.
* Useful in high-frequency hot loops.
*/
forEachEdge(callback: (source: string, target: string, weight: number) => void): void {
for (const [key, weight] of this.edges) {
const { source, target } = Graph.parseEdgeKey(key);
callback(source, target, weight);
}
}

toJSON() {
return {
nodes: this.getNodes(),
Expand Down
17 changes: 8 additions & 9 deletions packages/core/src/graph/hits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,18 @@ export class HITSService {
const incoming: { sourceIndex: number, weight: number }[][] = new Array(N).fill(null).map(() => []);
const outgoing: { targetIndex: number, weight: number }[][] = new Array(N).fill(null).map(() => []);

const allEdges = graph.getEdges();
for (const edge of allEdges) {
if (edge.source === edge.target) continue;
graph.forEachEdge((source, target, weight) => {
if (source === target) return;

const sourceIndex = urlToIndex.get(edge.source);
const targetIndex = urlToIndex.get(edge.target);
const sourceIndex = urlToIndex.get(source);
const targetIndex = urlToIndex.get(target);

if (sourceIndex !== undefined && targetIndex !== undefined) {
const weight = edge.weight || 1.0;
incoming[targetIndex].push({ sourceIndex, weight });
outgoing[sourceIndex].push({ targetIndex, weight });
const w = weight || 1.0;
incoming[targetIndex].push({ sourceIndex, weight: w });
outgoing[sourceIndex].push({ targetIndex, weight: w });
}
}
});

// Initialize Scores
const authScores = new Float64Array(N).fill(1.0);
Expand Down
13 changes: 6 additions & 7 deletions packages/core/src/graph/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ export interface Metrics {

export function calculateMetrics(graph: Graph, _maxDepth: number): Metrics {
const nodes = graph.getNodes();
const edges = graph.getEdges();

const totalPages = nodes.length;
const totalEdges = edges.length;
const totalEdges = graph.edges.size;

// Identify broken nodes
const brokenNodes = new Set(nodes.filter(n => n.status >= 400 || n.status === 0).map(n => n.url));

// Pre-compute outgoing edges per node for faster lookup
const outgoingEdges = new Map<string, string[]>();
for (const edge of edges) {
let targets = outgoingEdges.get(edge.source);
graph.forEachEdge((source, target) => {
let targets = outgoingEdges.get(source);
if (!targets) {
targets = [];
outgoingEdges.set(edge.source, targets);
outgoingEdges.set(source, targets);
}
targets.push(edge.target);
}
targets.push(target);
});

// Populate brokenLinks per node
for (const node of nodes) {
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/graph/pagerank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class PageRankService {
const neutralScoreWhenFlat = options.neutralScoreWhenFlat ?? 50;

const allNodes = graph.getNodes();
const allEdges = graph.getEdges();

// 1. Filter Eligible Nodes
const eligibleNodes = allNodes.filter(node => {
Expand Down Expand Up @@ -71,16 +70,16 @@ export class PageRankService {
const outWeights = new Float64Array(nodeCount);
const incoming: { sourceIndex: number, weight: number }[][] = new Array(nodeCount).fill(null).map(() => []);

for (const edge of allEdges) {
const sourceIndex = urlToIndex.get(edge.source);
const targetIndex = urlToIndex.get(edge.target);
graph.forEachEdge((source, target, weight) => {
const sourceIndex = urlToIndex.get(source);
const targetIndex = urlToIndex.get(target);

if (sourceIndex !== undefined && targetIndex !== undefined) {
const weight = edge.weight || 1.0;
incoming[targetIndex].push({ sourceIndex, weight });
outWeights[sourceIndex] += weight;
const w = weight || 1.0;
incoming[targetIndex].push({ sourceIndex, weight: w });
outWeights[sourceIndex] += w;
}
}
});

// Identify sinks
const sinks: number[] = [];
Expand Down