Skip to content

Commit

Permalink
fix(perf): Do not box Point for equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 15, 2021
1 parent 602491d commit a4d673d
Showing 1 changed file with 20 additions and 36 deletions.
56 changes: 20 additions & 36 deletions src/Uno.Foundation/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,36 @@ public Point(double x, double y)
public double X { get; set; }
public double Y { get; set; }

public override string ToString()
{
return "[{0}, {1}]".InvariantCultureFormat(X, Y);
}

internal string ToDebugString()
=> FormattableString.Invariant($"{X:F2},{Y:F2}");

internal Point WithX(double x) => new Point(x, Y);

internal Point WithY(double y) => new Point(X, y);

internal Point GetOpposite() => new Point(-X, -Y);
public override int GetHashCode()
=> X.GetHashCode() ^ Y.GetHashCode();

public override bool Equals(object obj)
=> obj is Point other && Equals(this, other);

public bool Equals(Point value) // Even if not in public doc, this is public on UWP
=> Equals(this, value);

private static bool Equals(Point left, Point right)
=> left.X == right.X && left.Y == right.Y;

public static bool operator ==(Point left, Point right)
{
return left.Equals(right);
}
=> Equals(left, right);

public static bool operator !=(Point left, Point right)
{
return !left.Equals(right);
}
=> !Equals(left, right);

public static Point operator +(Point p1, Point p2)
{
return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
=> new Point(p1.X + p2.X, p1.Y + p2.Y);

public static Point operator -(Point p1, Point p2)
{
return new Point(p1.X - p2.X, p1.Y - p2.Y);
}
=> new Point(p1.X - p2.X, p1.Y - p2.Y);

public static Point operator -(Point a)
{
return new Point(-a.X, -a.Y);
}
=> new Point(-a.X, -a.Y);

public static implicit operator Point(string point)
{
Expand All @@ -83,19 +75,11 @@ public static implicit operator Point(string point)
}
}

public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode();

public override bool Equals(object obj)
{
if (obj is Point)
{
var point = (Point)obj;

return X == point.X && Y == point.Y;
}
public override string ToString()
=> "[{0}, {1}]".InvariantCultureFormat(X, Y);

return false;
}
internal string ToDebugString()
=> FormattableString.Invariant($"{X:F2},{Y:F2}");

private string DebugDisplay => $"{X:f1},{Y:f1}";
}
Expand Down

0 comments on commit a4d673d

Please sign in to comment.