modernuo/Projects/Server/Geometry/Point2D.cs

205 lines
7 KiB
C#
Raw Permalink Normal View History

2020-08-25 18:53:35 -07:00
/*************************************************************************
* ModernUO *
2023-08-09 09:09:26 -07:00
* Copyright 2019-2023 - ModernUO Development Team *
2020-08-25 18:53:35 -07:00
* Email: hi@modernuo.com *
* File: Point2D.cs *
2020-08-25 18:53:35 -07:00
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
2020-08-25 18:53:35 -07:00
namespace Server;
public struct Point2D
: IPoint2D, IComparable<Point2D>, IComparable<IPoint2D>, IEquatable<object>, IEquatable<Point2D>,
IEquatable<IPoint2D>, ISpanFormattable, ISpanParsable<Point2D>
2020-08-25 18:53:35 -07:00
{
internal int m_X;
internal int m_Y;
2020-08-25 18:53:35 -07:00
public static readonly Point2D Zero = new(0, 0);
2020-08-25 18:53:35 -07:00
[CommandProperty(AccessLevel.Counselor)]
public int X
{
get => m_X;
set => m_X = value;
}
2020-08-25 18:53:35 -07:00
[CommandProperty(AccessLevel.Counselor)]
public int Y
{
get => m_Y;
set => m_Y = value;
}
2020-08-25 18:53:35 -07:00
2023-02-13 23:28:23 -08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Point2D(IPoint2D p) : this(p.X, p.Y)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Point2D(Point3D p) : this(p.X, p.Y)
{
}
2020-08-25 18:53:35 -07:00
2023-02-13 23:28:23 -08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Point2D(Point2D p) : this(p.X, p.Y)
{
}
2020-08-25 18:53:35 -07:00
2023-02-13 23:28:23 -08:00
public Point2D(int x, int y)
{
m_X = x;
m_Y = y;
}
public bool Equals(Point2D other) => m_X == other.m_X && m_Y == other.m_Y;
2020-08-25 18:53:35 -07:00
public bool Equals(IPoint2D other) => m_X == other?.X && m_Y == other.Y;
2020-08-25 18:53:35 -07:00
public override bool Equals(object obj) => obj is Point2D other && Equals(other);
2020-08-25 18:53:35 -07:00
public override int GetHashCode() => HashCode.Combine(m_X, m_Y);
2020-08-25 18:53:35 -07:00
public static bool operator ==(Point2D l, Point2D r) => l.m_X == r.m_X && l.m_Y == r.m_Y;
2020-08-25 18:53:35 -07:00
public static bool operator !=(Point2D l, Point2D r) => l.m_X != r.m_X || l.m_Y != r.m_Y;
2020-08-25 18:53:35 -07:00
public static bool operator ==(Point2D l, IPoint2D r) => !ReferenceEquals(r, null) && l.m_X == r.X && l.m_Y == r.Y;
2020-08-25 18:53:35 -07:00
public static bool operator !=(Point2D l, IPoint2D r) => !ReferenceEquals(r, null) && (l.m_X != r.X || l.m_Y != r.Y);
2020-08-25 18:53:35 -07:00
public static bool operator >(Point2D l, Point2D r) => l.m_X > r.m_X && l.m_Y > r.m_Y;
2020-08-25 18:53:35 -07:00
public static bool operator >(Point2D l, IPoint2D r) => !ReferenceEquals(r, null) && l.m_X > r.X && l.m_Y > r.Y;
2020-08-25 18:53:35 -07:00
public static bool operator <(Point2D l, Point2D r) => l.m_X < r.m_X && l.m_Y < r.m_Y;
2020-08-25 18:53:35 -07:00
public static bool operator <(Point2D l, IPoint2D r) => !ReferenceEquals(r, null) && l.m_X < r.X && l.m_Y < r.Y;
2020-08-25 18:53:35 -07:00
public static bool operator >=(Point2D l, Point2D r) => l.m_X >= r.m_X && l.m_Y >= r.m_Y;
2020-08-25 18:53:35 -07:00
public static bool operator >=(Point2D l, IPoint2D r) => !ReferenceEquals(r, null) && l.m_X >= r.X && l.m_Y >= r.Y;
2020-08-25 18:53:35 -07:00
public static bool operator <=(Point2D l, Point2D r) => l.m_X <= r.m_X && l.m_Y <= r.m_Y;
2020-08-25 18:53:35 -07:00
public static bool operator <=(Point2D l, IPoint2D r) => !ReferenceEquals(r, null) && l.m_X <= r.X && l.m_Y <= r.Y;
2020-08-25 18:53:35 -07:00
public int CompareTo(Point2D other)
{
var xComparison = m_X.CompareTo(other.m_X);
return xComparison != 0 ? xComparison : m_Y.CompareTo(other.m_Y);
}
2020-08-25 18:53:35 -07:00
public int CompareTo(IPoint2D other)
{
var xComparison = m_X.CompareTo(other.X);
return xComparison != 0 ? xComparison : m_Y.CompareTo(other.Y);
2020-08-25 18:53:35 -07:00
}
fix: Optimizes Point2D by implementing ISpanFormattable (#1203) Point2D implements a basic TryFormat function. Only a single format is supported: "(X, Y)" where X and Y are base 10 integers. Uses recent improvements in string interpolation to write the characters to the destination without first allocating memory for boxed arguments or intermediate strings. This is a partial solution to issue #1067. A similar solution well be implemented in other classes in Geometry if it looks promising. Benchmarks --- https://github.com/harleyholt/ModernUOBenchmarks/blob/d6f8dabf3c813937e65874ea82004701be0153c4/Program.cs Before change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 44.27 ns | 0.068 ns | 0.063 ns | 0.0063 | 40 B | | InterpolatedString | 110.64 ns | 0.316 ns | 0.295 ns | 0.0126 | 80 B | | InterpolatedStringMultiplePoints | 211.36 ns | 0.349 ns | 0.326 ns | 0.0293 | 184 B | ``` After change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 55.13 ns | 0.826 ns | 0.772 ns | 0.0063 | 40 B | | InterpolatedString | 55.73 ns | 0.954 ns | 0.892 ns | 0.0063 | 40 B | | InterpolatedStringMultiplePoints | 105.42 ns | 0.151 ns | 0.134 ns | 0.0101 | 64 B | ```
2022-10-25 12:08:35 -07:00
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
=> destination.TryWrite(provider, $"({m_X}, {m_Y})", out charsWritten);
fix: Optimizes Point2D by implementing ISpanFormattable (#1203) Point2D implements a basic TryFormat function. Only a single format is supported: "(X, Y)" where X and Y are base 10 integers. Uses recent improvements in string interpolation to write the characters to the destination without first allocating memory for boxed arguments or intermediate strings. This is a partial solution to issue #1067. A similar solution well be implemented in other classes in Geometry if it looks promising. Benchmarks --- https://github.com/harleyholt/ModernUOBenchmarks/blob/d6f8dabf3c813937e65874ea82004701be0153c4/Program.cs Before change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 44.27 ns | 0.068 ns | 0.063 ns | 0.0063 | 40 B | | InterpolatedString | 110.64 ns | 0.316 ns | 0.295 ns | 0.0126 | 80 B | | InterpolatedStringMultiplePoints | 211.36 ns | 0.349 ns | 0.326 ns | 0.0293 | 184 B | ``` After change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 55.13 ns | 0.826 ns | 0.772 ns | 0.0063 | 40 B | | InterpolatedString | 55.73 ns | 0.954 ns | 0.892 ns | 0.0063 | 40 B | | InterpolatedStringMultiplePoints | 105.42 ns | 0.151 ns | 0.134 ns | 0.0101 | 64 B | ```
2022-10-25 12:08:35 -07:00
public override string ToString()
{
fix: Optimizes Point2D by implementing ISpanFormattable (#1203) Point2D implements a basic TryFormat function. Only a single format is supported: "(X, Y)" where X and Y are base 10 integers. Uses recent improvements in string interpolation to write the characters to the destination without first allocating memory for boxed arguments or intermediate strings. This is a partial solution to issue #1067. A similar solution well be implemented in other classes in Geometry if it looks promising. Benchmarks --- https://github.com/harleyholt/ModernUOBenchmarks/blob/d6f8dabf3c813937e65874ea82004701be0153c4/Program.cs Before change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 44.27 ns | 0.068 ns | 0.063 ns | 0.0063 | 40 B | | InterpolatedString | 110.64 ns | 0.316 ns | 0.295 ns | 0.0126 | 80 B | | InterpolatedStringMultiplePoints | 211.36 ns | 0.349 ns | 0.326 ns | 0.0293 | 184 B | ``` After change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 55.13 ns | 0.826 ns | 0.772 ns | 0.0063 | 40 B | | InterpolatedString | 55.73 ns | 0.954 ns | 0.892 ns | 0.0063 | 40 B | | InterpolatedStringMultiplePoints | 105.42 ns | 0.151 ns | 0.134 ns | 0.0101 | 64 B | ```
2022-10-25 12:08:35 -07:00
// Maximum number of characters that are needed to represent this:
// 4 characters for (, )
// Up to 11 characters to represent each integer
const int maxLength = 4 + 11 * 2;
Span<char> span = stackalloc char[maxLength];
TryFormat(span, out var charsWritten, null, null);
return span[..charsWritten].ToString();
fix: Optimizes Point2D by implementing ISpanFormattable (#1203) Point2D implements a basic TryFormat function. Only a single format is supported: "(X, Y)" where X and Y are base 10 integers. Uses recent improvements in string interpolation to write the characters to the destination without first allocating memory for boxed arguments or intermediate strings. This is a partial solution to issue #1067. A similar solution well be implemented in other classes in Geometry if it looks promising. Benchmarks --- https://github.com/harleyholt/ModernUOBenchmarks/blob/d6f8dabf3c813937e65874ea82004701be0153c4/Program.cs Before change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 44.27 ns | 0.068 ns | 0.063 ns | 0.0063 | 40 B | | InterpolatedString | 110.64 ns | 0.316 ns | 0.295 ns | 0.0126 | 80 B | | InterpolatedStringMultiplePoints | 211.36 ns | 0.349 ns | 0.326 ns | 0.0293 | 184 B | ``` After change: ``` BenchmarkDotNet=v0.13.2, OS=ubuntu 22.04 Intel Core i7-9700K CPU 3.60GHz (Coffee Lake), 1 CPU, 8 logical and 8 physical cores .NET SDK=6.0.402 [Host] : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 DefaultJob : .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------------- |----------:|---------:|---------:|-------:|----------:| | CallToString | 55.13 ns | 0.826 ns | 0.772 ns | 0.0063 | 40 B | | InterpolatedString | 55.73 ns | 0.954 ns | 0.892 ns | 0.0063 | 40 B | | InterpolatedStringMultiplePoints | 105.42 ns | 0.151 ns | 0.134 ns | 0.0101 | 64 B | ```
2022-10-25 12:08:35 -07:00
}
public string ToString(string format, IFormatProvider formatProvider)
{
// format and formatProvider are not doing anything right now, so use the
// default ToString implementation.
return ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point2D Parse(string s) => Parse(s, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point2D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryParse(string s, IFormatProvider provider, out Point2D result) =>
TryParse(s.AsSpan(), provider, out result);
public static Point2D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
{
s = s.Trim();
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var comma = s.IndexOfOrdinal(',');
if (comma == -1)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var first = s.Slice(1, comma - 1).Trim();
if (!Utility.ToInt32(first, out var x))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var second = s.Slice(comma + 1, s.Length - comma - 2).Trim();
if (!Utility.ToInt32(second, out var y))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
return new Point2D(x, y);
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Point2D result)
{
s = s.Trim();
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
{
result = default;
return false;
}
var comma = s.IndexOfOrdinal(',');
if (comma == -1)
{
result = default;
return false;
}
var first = s.Slice(1, comma - 1).Trim();
if (!Utility.ToInt32(first, out var x))
{
result = default;
return false;
}
var second = s.Slice(comma + 1, s.Length - comma - 2).Trim();
if (!Utility.ToInt32(second, out var y))
{
result = default;
return false;
}
result = new Point2D(x, y);
return true;
}
2020-08-25 18:53:35 -07:00
}