Skip to content

Commit 476f113

Browse files
Aayush MainiCopilot
andcommitted
Address PR feedback: reduce allocations and normalize empty edges
- Change RewriteId to return IEnumerable<string> using Enumerable.Repeat instead of allocating a new HashSet for every non-bare id - Normalize empty edge sets to null after self-edge filtering to match the existing serialization convention in GraphTranslationUtility Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a83d0a8 commit 476f113

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/Microsoft.ComponentDetection.Orchestrator/Services/GraphTranslation/DefaultGraphTranslationService.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ private static void ReconcileGraph(
155155
}
156156

157157
// Rewrite a single Id: if it's a bare Id being merged, expand to its rich counterparts.
158-
HashSet<string> RewriteId(string id) =>
159-
bareToRich.TryGetValue(id, out var richIds) ? richIds : [id];
158+
// Returns the existing set for bare ids; yields a single element for non-bare ids to avoid allocation.
159+
IEnumerable<string> RewriteId(string id) =>
160+
bareToRich.TryGetValue(id, out var richIds) ? richIds : Enumerable.Repeat(id, 1);
160161

161162
// Rebuild graph: skip bare nodes being merged, rewrite edge targets.
162163
var newGraph = new Contracts.BcdeModels.DependencyGraph();
@@ -186,7 +187,7 @@ HashSet<string> RewriteId(string id) =>
186187
}
187188
}
188189

189-
newGraph[nodeId] = newEdges;
190+
newGraph[nodeId] = newEdges.Count > 0 ? newEdges : null;
190191
}
191192
}
192193

@@ -209,6 +210,12 @@ HashSet<string> RewriteId(string id) =>
209210
}
210211
}
211212
}
213+
214+
// Normalize empty edge sets to null for consistent serialization.
215+
if (newGraph[richId].Count == 0)
216+
{
217+
newGraph[richId] = null;
218+
}
212219
}
213220
}
214221
}

0 commit comments

Comments
 (0)