This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add span-based Guid.{Try}Parse{Exact} methods (#14062)
This commit does two things: - Changes the existing Guid.{Try}Parse{Exact} methods to work in terms of spans rather than strings. - Then adds new overloads that accept spans and that uses the same underlying span-based support. Due to the first change, performance actually improves for several of the guid formats, in particular due to substring allocations that are no longer incurred. For example, this program: ```C# using System; using System.Diagnostics; class Program { static void Main() { var sw = new Stopwatch(); const int Iters = 1000000; Console.WriteLine("Pattern\tBytes\tTime"); foreach (string pattern in new[] { "D", "B", "P", "N", "X" }) { string input = Guid.NewGuid().ToString(pattern); Guid.Parse(input); long a = GC.GetAllocatedBytesForCurrentThread(); sw.Restart(); for (int i = 0; i < Iters; i++) Guid.Parse(input); sw.Stop(); a = GC.GetAllocatedBytesForCurrentThread() - a; Console.WriteLine($"\"{pattern}\"\t{a / Iters}\t{(int)(sw.Elapsed.TotalMilliseconds / Iters * 1000000)}ns"); } } } ``` on my machine previously output: ``` Pattern Bytes Time "D" 0 218ns "B" 0 217ns "P" 0 210ns "N" 168 388ns "X" 744 765ns ``` and with this change outputs: ``` Pattern Bytes Time "D" 0 213ns "B" 0 192ns "P" 0 196ns "N" 0 372ns "X" 0 577ns ```
- Loading branch information