Skip to content

Commit

Permalink
improve merge performance
Browse files Browse the repository at this point in the history
Signed-off-by: andreas hilti <andreas.hilti@bluewin.ch>
  • Loading branch information
andreas-hilti committed May 25, 2024
1 parent 7a0682d commit e8bbbff
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/CycloneDX.Utils/Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,33 @@ public List<T> Merge(List<T> list1, List<T> list2)
if (list2 is null) return list1;

var result = new List<T>(list1);
// We want to avoid the costly computation of the hashes if possible.
// Therefore, we use a nullable type.
var resultHashes = new List<int?>(list1.Count);
for (int i = 0; i < list1.Count; i++) resultHashes.Add(null);

foreach (var item in list2)
{
if (!(result.Contains(item)))
int hash = item.GetHashCode();
bool found = false;
for (int i=0; i < result.Count; i++)
{
var resultItem = result[i];
if (resultHashes[i] == null)
{
resultHashes[i] = resultItem.GetHashCode();
}
int resultHash = resultHashes[i].Value;
if (hash == resultHash && item.Equals(resultItem))
{
found = true;
break;
}
}
if (!found)
{
result.Add(item);
resultHashes.Add(hash);
}
}

Expand Down

0 comments on commit e8bbbff

Please sign in to comment.