2 ms·
Validating gigabytes of Unicode strings per second in C#?
- kristianp 2y agoThis is the .net library code they're beating. It's hundreds of lines long and makes use of ARM SIMD or SSE2 depending on support available. Lemire and colleague have certainly improved on that algorithm if it's only 30 lines long. https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.Validation.cs#L24 https://github.com/dotnet/runtime/blob/main/src/libraries/Sy...
- Atotalnoob 2y agoIs there resources to learn this kind of programming in c#? I’ve tried to learn vector, spans, and other lower level features of c#, but I can’t ever find a real world use for them.
- neonsunset 2y agoSpans are not a low-level feature, they are a general purpose container type representing a slice, like &[T] in Rust. That's it. Analyzers will nudge you to use them naturally next time you write int.Parse(text.Substring(0, 4)) to replace it with int.Parse(text.AsSpan(0, 4)) - you don't need an intermediate string, just a slice of it. But because they can wrap any memory, you can now pass anything to int.Parse that can produce a span of chars or bytes: stackalloc, pointer + length from C/C++, an array, a string, etc. all unified with a single type. Vectorization techniques themselves are not C# specific, but dotnet/runtime does have an introductory guide that demonstrates their "basic" use-case: https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/vectorization-guidelines.md https://github.com/dotnet/runtime/blob/main/docs/coding-guid...