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
4 changes: 4 additions & 0 deletions .Jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2025-06-27 - [Map Initialization Overhead]
**Learning:** Initializing Maps with `new Map(array.map(...))` creates unnecessary intermediate arrays, consuming memory and triggering garbage collection overhead, especially noticeable when dealing with many nodes.
**Action:** Use a `for...of` loop to directly `map.set()` elements rather than creating an intermediate array of tuples, especially in frequently executed or rendering paths.

## 2024-07-13 - Optimize Data Dictionary Export Algorithm
**Learning:** Found an O(N * C * E) bottleneck in `frontend/src/erd/exportDataDictionary.ts` where `isForeignKeyColumn` did an `edges.some()` search per column inside a loop over nodes and columns.
**Action:** When writing complex export algorithms over a large graph (Nodes + Edges), always pre-compute search spaces using Maps or Sets (e.g. `fkHandles`) upfront (O(E)) to achieve O(1) lookups during deeply nested loops (O(N * C + E)).
24 changes: 20 additions & 4 deletions frontend/src/erd/exportDataDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ function foreignKeyColumnsByNode(edges: Edge[]): Map<string, Set<string>> {

function isForeignKeyColumn(
edgeColumnsByNode: Map<string, Set<string>>,
fkHandles: Set<string>,
node: Node<TableNodeData>,
columnName: string,
edges: Edge[],
): boolean {
if (edgeColumnsByNode.get(node.id)?.has(columnName)) {
return true;
}

const handleId = sourceColumnHandleId(columnName);
return edges.some((edge) => edge.source === node.id && edge.sourceHandle === handleId);
return fkHandles.has(`${node.id}:${handleId}`);
}

function exampleValue(value: TableNodeData['columns'][number]['example_value']): string {
Expand All @@ -91,7 +91,15 @@ export function exportDictionaryCsv(
'Example Value',
];
const rows: unknown[][] = [header];
// ⚡ Bolt: Pre-compute foreign key handle sets to avoid O(E) array search per column,
// reducing complexity from O(N * C * E) to O(N * C + E).
const fkColumnsByNode = foreignKeyColumnsByNode(edges);
const fkHandles = new Set<string>();
for (const edge of edges) {
if (edge.sourceHandle) {
fkHandles.add(`${edge.source}:${edge.sourceHandle}`);
}
}

for (const node of nodes) {
const tableName = node.data.title || node.id;
Expand All @@ -110,7 +118,7 @@ export function exportDictionaryCsv(
column.column_name,
column.data_type,
column.is_pk ? 'Y' : 'N',
isForeignKeyColumn(fkColumnsByNode, node, column.column_name, edges) ? 'Y' : 'N',
isForeignKeyColumn(fkColumnsByNode, fkHandles, node, column.column_name) ? 'Y' : 'N',
column.is_not_null ? 'Y' : 'N',
column.column_comment || '',
exampleValue(column.example_value),
Expand All @@ -126,7 +134,15 @@ export function exportDictionaryMarkdown(
edges: Edge[],
): string {
const lines: string[] = ['# Data Dictionary', ''];
// ⚡ Bolt: Pre-compute foreign key handle sets to avoid O(E) array search per column,
// reducing complexity from O(N * C * E) to O(N * C + E).
const fkColumnsByNode = foreignKeyColumnsByNode(edges);
const fkHandles = new Set<string>();
for (const edge of edges) {
if (edge.sourceHandle) {
fkHandles.add(`${edge.source}:${edge.sourceHandle}`);
}
}

if (nodes.length === 0) {
lines.push('No tables found.');
Expand All @@ -149,7 +165,7 @@ export function exportDictionaryMarkdown(

for (const column of columns) {
const pk = column.is_pk ? 'Y' : 'N';
const fk = isForeignKeyColumn(fkColumnsByNode, node, column.column_name, edges) ? 'Y' : 'N';
const fk = isForeignKeyColumn(fkColumnsByNode, fkHandles, node, column.column_name) ? 'Y' : 'N';
const notNull = column.is_not_null ? 'Y' : 'N';
const comment = column.column_comment || '';
lines.push(
Expand Down
Loading