Browse Source

Merge pull request #3267 from MarchingCube/perf-fontfamily-equality

Optimize FontFamily and members hashcode/equality.
pull/3279/head
Dariusz Komosiński 7 years ago
committed by GitHub
parent
commit
0127f82141
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      src/Avalonia.Visuals/Media/FontFamily.cs
  2. 34
      src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs

27
src/Avalonia.Visuals/Media/FontFamily.cs

@ -184,36 +184,25 @@ namespace Avalonia.Media
{
unchecked
{
var hash = (int)2186146271;
if (Key != null)
{
hash = (hash * 15768619) ^ Key.GetHashCode();
}
else
{
hash = (hash * 15768619) ^ FamilyNames.GetHashCode();
}
if (Key != null)
{
hash = (hash * 15768619) ^ Key.GetHashCode();
}
return hash;
return ((FamilyNames != null ? FamilyNames.GetHashCode() : 0) * 397) ^ (Key != null ? Key.GetHashCode() : 0);
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
if (!(obj is FontFamily other))
{
return false;
}
if (Key != null)
if (!Equals(Key, other.Key))
{
return other.FamilyNames.Equals(FamilyNames) && other.Key.Equals(Key);
return false;
}
return other.FamilyNames.Equals(FamilyNames);

34
src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs

@ -111,7 +111,24 @@ namespace Avalonia.Media.Fonts
/// </returns>
public override int GetHashCode()
{
return ToString().GetHashCode();
if (Count == 0)
{
return 0;
}
unchecked
{
int hash = 17;
for (var i = 0; i < Names.Count; i++)
{
string name = Names[i];
hash = hash * 23 + name.GetHashCode();
}
return hash;
}
}
/// <summary>
@ -128,7 +145,20 @@ namespace Avalonia.Media.Fonts
return false;
}
return other.ToString().Equals(ToString());
if (other.Count != Count)
{
return false;
}
for (int i = 0; i < Count; i++)
{
if (Names[i] != other.Names[i])
{
return false;
}
}
return true;
}
public int Count => Names.Count;

Loading…
Cancel
Save