Clean up Uri.UnescapeDataString by stephentoub · Pull Request #42225 · dotnet/corefx (original) (raw)
This was really meant as a minor cleanup, e.g. to remove unsafe code that needn't be. It does however have a perf benefit for longer inputs when nothing needs to be unescaped (or when the thing that needs to be unescaped is later in the input), as then the use of IndexOf provides vectorization "for free".
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Diagnosers; using BenchmarkDotNet.Running; using System; using System.Linq;
[MemoryDiagnoser] public class Program { static void Main(string[] args) => BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args);
[Params(4, 40, 400)] public int Repeats { get; set; }
[Params("ASCII", "Mixed", "Escaped", "Emoji")] public string InputMode { get; set; }
private string _value;
[GlobalSetup]
public void Setup() =>
_value = InputMode switch
{
"ASCII" => string.Concat(Enumerable.Repeat("abcd", Repeats)),
"Mixed" => string.Concat(Enumerable.Repeat("abcd%2B", Repeats)),
"Escaped" => string.Concat(Enumerable.Repeat("%2B", Repeats)),
"Emoji" => string.Concat(Enumerable.Repeat("%F0%9F%98%80", Repeats)),
_ => throw new Exception()
};
[Benchmark]
public string Unescape() => Uri.UnescapeDataString(_value);
}