fix: Optimizes TextDefinition to eliminate allocations. Removes TextDefinition ctor. (#1221)

### BREAKING CHANGE ###
The constructor for `TextDefinition` has been removed. Instead use `TextDefinition.Of()` or cast the integer/string to TextDefinition.
This commit is contained in:
Kamron Batman 2022-10-30 16:53:23 -07:00 committed by GitHub
parent 213b7025af
commit 910e06767b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 790 additions and 722 deletions

View file

@ -112,17 +112,16 @@ public struct Point2D
}
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
return destination.TryWrite(provider, $"({m_X}, {m_Y})", out charsWritten);
}
=> destination.TryWrite(provider, $"({m_X}, {m_Y})", out charsWritten);
public override string ToString() {
// Maximum number of characters that are needed to represent this:
// 4 characters for (, )
// Up to 11 characters to represent each integer
Span<char> span = stackalloc char[4+11*2];
this.TryFormat(span, out var charsWritten, null, null);
return span.Slice(0, charsWritten).ToString();
const int maxLength = 4 + 11 * 2;
Span<char> span = stackalloc char[maxLength];
TryFormat(span, out var charsWritten, null, null);
return span[..charsWritten].ToString();
}
public string ToString(string format, IFormatProvider formatProvider)
@ -131,5 +130,4 @@ public struct Point2D
// default ToString implementation.
return ToString();
}
}

View file

@ -0,0 +1,44 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TextDefinitionConverter.cs *
* *
* 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.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class TextDefinitionConverter : JsonConverter<TextDefinition>
{
public override TextDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType switch
{
JsonTokenType.String => TextDefinition.Of(reader.GetString()),
JsonTokenType.Number => TextDefinition.Of(reader.GetInt32()),
_ => throw new JsonException("TextDefinition value must be an integer or string")
};
public override void Write(Utf8JsonWriter writer, TextDefinition value, JsonSerializerOptions options)
{
if (value.Number > 0)
{
writer.WriteNumberValue(value.Number);
}
else
{
writer.WriteStringValue(value.String);
}
}
}
}

View file

@ -0,0 +1,29 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TextDefinitionConverterFactory.cs *
* *
* 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.Text.Json;
using System.Text.Json.Serialization;
namespace Server.Json
{
public class TextDefinitionConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(TextDefinition);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) =>
new TextDefinitionConverter();
}
}

View file

@ -124,9 +124,9 @@ public interface IGenericReader
{
return ReadEncodedInt() switch
{
0 => new TextDefinition(),
1 => new TextDefinition(ReadEncodedInt()),
2 => new TextDefinition(ReadString()),
0 => TextDefinition.Empty,
1 => ReadEncodedInt(),
2 => ReadString(),
_ => null
};
}

View file

@ -13,17 +13,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
namespace Server;
[Parsable]
[PropertyObject]
public class TextDefinition
public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
{
public TextDefinition(string text) : this(0, text)
public static readonly TextDefinition Empty = new();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TextDefinition Of(int number) => Of(number, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TextDefinition Of(string text) => Of(0, text);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TextDefinition Of(int number, string text) => new(number, text);
private TextDefinition()
{
}
public TextDefinition(int number = 0, string text = null)
private TextDefinition(int number, string text)
{
Number = number;
String = text;
@ -45,9 +59,9 @@ public class TextDefinition
public string GetValue() => Number > 0 ? Number.ToString() : String ?? "";
public static implicit operator TextDefinition(int v) => new(v);
public static implicit operator TextDefinition(int v) => Of(v);
public static implicit operator TextDefinition(string s) => new(s);
public static implicit operator TextDefinition(string s) => Of(s);
public static implicit operator int(TextDefinition m) => m?.Number ?? 0;
@ -60,6 +74,48 @@ public class TextDefinition
return null;
}
return Utility.ToInt32(value, out var i) ? new TextDefinition(i) : new TextDefinition(value);
return Utility.ToInt32(value, out var i) ? Of(i) : Of(value);
}
public void Deconstruct(out int number, out string s)
{
if (Number > 0)
{
number = Number;
s = null;
}
else
{
number = 0;
s = String;
}
}
public override bool Equals(object obj) => Equals(obj as TextDefinition);
public bool Equals(TextDefinition other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
if (Number > 0 || other.Number > 0)
{
return Number == other.Number;
}
return String == other.String;
}
public override int GetHashCode() => Number > 0 ? HashCode.Combine(Number) : HashCode.Combine(String);
public static bool operator ==(TextDefinition left, TextDefinition right) => Equals(left, right);
public static bool operator !=(TextDefinition left, TextDefinition right) => !Equals(left, right);
}