Skip to content
Merged
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
26 changes: 16 additions & 10 deletions src/components/chart/ha-sankey-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,26 @@ export class HaSankeyChart extends LitElement {
}

private _findParentIndex(id: string, links: Link[], sections: Node[][]) {
const parent = links.find((l) => l.target === id)?.source;
if (!parent) {
const parents = links.filter((l) => l.target === id).map((l) => l.source);
if (parents.length === 0) {
return -1;
}
let offset = 0;
for (let i = sections.length - 1; i >= 0; i--) {
const section = sections[i];
const index = section.findIndex((n) => n.id === parent);
if (index !== -1) {
return offset + index;
let sum = 0;
let count = 0;
for (const parent of parents) {
let offset = 0;
for (let i = sections.length - 1; i >= 0; i--) {
const section = sections[i];
const index = section.findIndex((n) => n.id === parent);
Comment on lines 293 to +304
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the seed sort, _findParentIndex() is called inside the Array.sort() comparator, and it does a full links.filter(...) scan for every comparator call. This can blow up to O(nodes log nodes * links) work per section. Consider precomputing a Map<targetId, sourceIds[]> (or directly Map<targetId, avgParentIndex>) once before sorting nodesWithIndex, so the comparator becomes O(1) lookups instead of repeatedly scanning links.

Copilot uses AI. Check for mistakes.
if (index !== -1) {
sum += offset + index;
count++;
break;
}
offset += section.length;
}
offset += section.length;
}
return -1;
return count > 0 ? sum / count : -1;
}

static styles = css`
Expand Down
Loading
Loading