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.
This commit is contained in:
parent
fe31470f05
commit
ecbee17690
227 changed files with 1426 additions and 1008 deletions
71
Projects/Server/Text/ISelfInterpolatedStringHandler.cs
Normal file
71
Projects/Server/Text/ISelfInterpolatedStringHandler.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -285,4 +285,22 @@ public static class StringHelpers
|
|||
4 => MemoryMarshal.Cast<byte, uint>(buffer).IndexOf((uint)0) * 4,
|
||||
_ => buffer.IndexOf((byte)0)
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ReplaceAny(this Span<char> chars, ReadOnlySpan<char> invalidChars, ReadOnlySpan<char> replacementChars)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var indexOf = chars.IndexOfAny(invalidChars);
|
||||
if (indexOf == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var chr = chars[indexOf];
|
||||
|
||||
chars[indexOf] = replacementChars[invalidChars.IndexOf(chr)];
|
||||
chars = chars[(indexOf + 1)..];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue