Skip to content

Commit 3cb8be0

Browse files
Copilotjongalloway
andcommitted
Optimize BackendManager deduplication performance from O(n²) to O(n)
Co-authored-by: jongalloway <[email protected]>
1 parent 0372321 commit 3cb8be0

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/NLWebNet/Services/BackendManager.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,27 @@ public IEnumerable<BackendInfo> GetBackendInfo()
235235
/// </summary>
236236
private List<NLWebResult> DeduplicateResults(List<NLWebResult> results)
237237
{
238-
var deduplicated = new List<NLWebResult>();
239-
var seenUrls = new HashSet<string>();
238+
// Use Dictionary for O(n) performance instead of O(n²)
239+
var resultsByUrl = new Dictionary<string, NLWebResult>();
240240

241-
foreach (var result in results.OrderByDescending(r => r.Score))
241+
foreach (var result in results)
242242
{
243-
// Simple deduplication by URL
244-
if (!seenUrls.Contains(result.Url))
243+
// Check if we've seen this URL before
244+
if (resultsByUrl.TryGetValue(result.Url, out var existing))
245245
{
246-
seenUrls.Add(result.Url);
247-
deduplicated.Add(result);
246+
// Keep the result with the higher score
247+
if (result.Score > existing.Score)
248+
{
249+
resultsByUrl[result.Url] = result;
250+
}
248251
}
249252
else
250253
{
251-
// If we've seen this URL, but the current result has a higher relevance score,
252-
// replace the existing one
253-
var existingIndex = deduplicated.FindIndex(r => r.Url == result.Url);
254-
if (existingIndex >= 0 && result.Score > deduplicated[existingIndex].Score)
255-
{
256-
deduplicated[existingIndex] = result;
257-
}
254+
// First time seeing this URL
255+
resultsByUrl[result.Url] = result;
258256
}
259257
}
260258

261-
return deduplicated;
259+
return resultsByUrl.Values.ToList();
262260
}
263261
}

0 commit comments

Comments
 (0)