Skip to content

Commit

Permalink
alternative performance improvement
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 Jun 11, 2024
1 parent 078e59a commit a387dee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/CycloneDX.Core/Models/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ public override bool Equals(object obj)

public bool Equals(Component obj)
{
// quick check without Serializer for improved performance
if (obj == null || Name != obj.Name)
{
return false;
}

return Json.Serializer.Serialize(this) == Json.Serializer.Serialize(obj);
}

Expand Down
26 changes: 1 addition & 25 deletions src/CycloneDX.Utils/Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,12 @@ 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)
{
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)
if (!(result.Contains(item)))
{
result.Add(item);
resultHashes.Add(hash);
}
}

Expand Down

0 comments on commit a387dee

Please sign in to comment.