Skip to content

Commit a2506f8

Browse files
authored
Merge pull request #5 from gmanvel/main
Perf/allocation optimizations for FastLabelPropagationCommunityDetector.AssignLabels
2 parents 744bf42 + feb6294 commit a2506f8

File tree

4 files changed

+353
-105
lines changed

4 files changed

+353
-105
lines changed

benchmarks/ManagedCode.GraphRag.Benchmarks/Cache/MemoryPipelineCacheBenchmarks.cs

Lines changed: 0 additions & 99 deletions
This file was deleted.

benchmarks/ManagedCode.GraphRag.Benchmarks/Community/FastLabelPropagationBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace ManagedCode.GraphRag.Benchmarks.Community;
99

1010
[MemoryDiagnoser]
11+
[HideColumns("Error", "StdDev", "RatioSD")]
1112
public class FastLabelPropagationBenchmarks
1213
{
1314
private EntityRecord[] _smallGraphEntities = null!;
@@ -41,7 +42,7 @@ public void Setup()
4142
(_largeGraphEntities, _largeGraphRelationships) = GenerateGraph(10_000, 5);
4243
}
4344

44-
[Benchmark]
45+
[Benchmark(Baseline = true)]
4546
public IReadOnlyDictionary<string, string> SmallGraph()
4647
{
4748
return FastLabelPropagationCommunityDetector.AssignLabels(

src/ManagedCode.GraphRag/Community/FastLabelPropagationCommunityDetector.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ public static IReadOnlyDictionary<string, string> AssignLabels(
2323

2424
var random = new Random(config.Seed);
2525
var labels = adjacency.Keys.ToDictionary(node => node, node => node, StringComparer.OrdinalIgnoreCase);
26-
var nodes = adjacency.Keys.ToList();
26+
var nodes = adjacency.Keys.ToArray();
2727
var maxIterations = Math.Max(1, config.MaxIterations);
2828

2929
for (var iteration = 0; iteration < maxIterations; iteration++)
3030
{
31-
var shuffled = nodes.OrderBy(_ => random.Next()).ToList();
31+
random.Shuffle(nodes);
3232
var changed = false;
3333

34-
foreach (var node in shuffled)
34+
var labelWeights = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase);
35+
foreach (var node in nodes)
3536
{
3637
var neighbors = adjacency[node];
3738
if (neighbors.Count == 0)
3839
{
3940
continue;
4041
}
4142

42-
var labelWeights = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase);
4343
foreach (var (neighbor, weight) in neighbors)
4444
{
4545
if (!labels.TryGetValue(neighbor, out var neighborLabel))
@@ -57,7 +57,7 @@ public static IReadOnlyDictionary<string, string> AssignLabels(
5757

5858
var maxWeight = labelWeights.Values.Max();
5959
var candidates = labelWeights
60-
.Where(pair => Math.Abs(pair.Value - maxWeight) < 1e-6)
60+
.Where(pair => Math.Abs(pair.Value - maxWeight) < double.Epsilon)
6161
.Select(pair => pair.Key)
6262
.ToList();
6363

@@ -70,6 +70,8 @@ public static IReadOnlyDictionary<string, string> AssignLabels(
7070
labels[node] = chosen;
7171
changed = true;
7272
}
73+
74+
labelWeights.Clear();
7375
}
7476

7577
if (!changed)

0 commit comments

Comments
 (0)