modernuo/Projects/Server/Text/ISelfInterpolatedStringHandler.cs

72 lines
3.5 KiB
C#
Raw Permalink Normal View History

fix: Optimizes OPL using string interpolation (#1041) ## Breaking Changes (New API) ObjectPropertyList supports the following API: ```cs list.Add(500000); list.Add(500001, stringArgument); list.Add("Some text"); list.Add($"Some text with {argument}"); list.Add(500002, $"{arg1}\t{arg2}"); ``` ## Notes 1. All API uses that require a formatter like this: ```cs list.Add(500002, "{0}\t{1}", arg1, arg2); ``` Should be changed to use string interpolation, for example: ```cs list.Add(500002, $"{arg1}\t{arg2}"); ``` 2. The following paradigm should no longer be used: ```cs list.Add(1061170, prop.ToString()); // strength requirement ~1_val~ ``` The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following: ```cs list.Add(1061170, $"{prop}"); // strength requirement ~1_val~ ``` ### Benchmarks ```cs | Method | Mean | Error | StdDev | Gen 0 | Allocated | |------------------------------- |---------:|--------:|--------:|-------:|----------:| | BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 | 88 B | | BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns | - | - | ``` ### Changes - [X] Removes crash in STArray.Return when array is null. - [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null. - [X] Fixes NPE in AosAttributes when Parent is null. - [X] Changes OPL to use string interpolation. - [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
2022-06-02 10:09:53 -07:00
/*************************************************************************
* ModernUO *
2023-08-09 09:09:26 -07:00
* Copyright 2019-2023 - ModernUO Development Team *
fix: Optimizes OPL using string interpolation (#1041) ## Breaking Changes (New API) ObjectPropertyList supports the following API: ```cs list.Add(500000); list.Add(500001, stringArgument); list.Add("Some text"); list.Add($"Some text with {argument}"); list.Add(500002, $"{arg1}\t{arg2}"); ``` ## Notes 1. All API uses that require a formatter like this: ```cs list.Add(500002, "{0}\t{1}", arg1, arg2); ``` Should be changed to use string interpolation, for example: ```cs list.Add(500002, $"{arg1}\t{arg2}"); ``` 2. The following paradigm should no longer be used: ```cs list.Add(1061170, prop.ToString()); // strength requirement ~1_val~ ``` The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following: ```cs list.Add(1061170, $"{prop}"); // strength requirement ~1_val~ ``` ### Benchmarks ```cs | Method | Mean | Error | StdDev | Gen 0 | Allocated | |------------------------------- |---------:|--------:|--------:|-------:|----------:| | BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 | 88 B | | BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns | - | - | ``` ### Changes - [X] Removes crash in STArray.Return when array is null. - [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null. - [X] Fixes NPE in AosAttributes when Parent is null. - [X] Changes OPL to use string interpolation. - [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
2022-06-02 10:09:53 -07:00
* Email: hi@modernuo.com *
* File: ISelfInterpolatedStringHandler.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.Runtime.CompilerServices;
namespace Server.Text;
public interface ISelfInterpolatedStringHandler
{
public void Add([InterpolatedStringHandlerArgument("")] ref InterpolatedStringHandler handler);
public void InitializeInterpolation(int literalLength, int formattedCount);
public void AppendLiteral(string value);
public void AppendFormatted<T>(T value);
public void AppendFormatted<T>(T value, string? format);
public void AppendFormatted<T>(T value, int alignment);
public void AppendFormatted<T>(T value, int alignment, string? format);
public void AppendFormatted(ReadOnlySpan<char> value);
public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format = null);
public void AppendFormatted(object? value, int alignment = 0, string? format = null);
public void AppendFormatted(string? value);
public void AppendFormatted(string? value, int alignment, string? format = null);
[InterpolatedStringHandler]
public ref struct InterpolatedStringHandler
{
private ISelfInterpolatedStringHandler _parent;
public InterpolatedStringHandler(int literalLength, int formattedCount, ISelfInterpolatedStringHandler parent)
{
_parent = parent;
_parent.InitializeInterpolation(literalLength, formattedCount);
}
public void AppendLiteral(string value) => _parent.AppendLiteral(value);
public void AppendFormatted<T>(T value) => _parent.AppendFormatted(value);
public void AppendFormatted<T>(T value, string? format) => _parent.AppendFormatted(value, format);
public void AppendFormatted<T>(T value, int alignment) => _parent.AppendFormatted(value, alignment);
public void AppendFormatted<T>(T value, int alignment, string? format) =>
_parent.AppendFormatted(value, alignment, format);
public void AppendFormatted(ReadOnlySpan<char> value) => _parent.AppendFormatted(value);
public void AppendFormatted(ReadOnlySpan<char> value, int alignment, string? format = null) =>
_parent.AppendFormatted(value, alignment, format);
public void AppendFormatted(object? value, int alignment = 0, string? format = null) =>
_parent.AppendFormatted(value, alignment, format);
public void AppendFormatted(string? value) => _parent.AppendFormatted(value);
public void AppendFormatted(string? value, int alignment, string? format = null) =>
_parent.AppendFormatted(value, alignment, format);
}
}