Skip to content

Commit 6a83ad5

Browse files
committed
Add documentation for UnsafeStringComparer
1 parent d9312d0 commit 6a83ad5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This library is a small collection of specialized helper types which focus on pe
55
- [StopwatchStruct](#stopwatchstruct)
66
- [StringSet](#stringset)
77
- [StringHash](#stringhash)
8+
- [UnsafeStringComparer](#unsafestringcomparer)
89

910
## StopwatchStruct
1011

@@ -165,3 +166,37 @@ for (var i = 0; i < buffer.Length; i++)
165166

166167
Obviously that's a lot more code, but it may be worth it for high-volume parsers.
167168

169+
## UnsafeStringComparer
170+
171+
[UnsafeStringComparer](https://github.com/bretcope/PerformanceTypes/blob/master/PerformanceTypes/UnsafeStringComparer.cs) is a static helper class which helps you perform fast string equality comparisons when one operand is a string and the other is a character buffer. It is called "Unsafe..." because it uses [unsafe](https://msdn.microsoft.com/en-us/library/chfa2zb8.aspx) code for optimizations and some methods accept pointers.
172+
173+
For strings with seven or fewer characters, they are compared one character at a time. For strings with eight or more characters, UnsafeStringComparer will switch to comparing four characters at a time via 64-bit integers, which can result in an almost 4x performance improvement.
174+
175+
> SIMD would likely provide additional performance improvements, but C# does not appear to provide a way to use SIMD with unmanaged pointers. It would have to be done in a native dll, which would make this library less portable.
176+
177+
There is only one method with four public overloads:
178+
179+
```csharp
180+
bool AreEqual(string str, char[] buffer)
181+
```
182+
183+
> Compares all characters from `str` with all characters in `buffer`.
184+
185+
```csharp
186+
bool AreEqual(string str, char[] buffer, int start, int length)
187+
```
188+
189+
> Compares all characters from `str` with the characters in buffer beginning at the `start` index, and for `length` characters. If `length != str.Length`, or if `start + length > buffer.Length`, the return value will always be false.
190+
191+
```csharp
192+
bool AreEqual(string str, char* buffer, int length)
193+
```
194+
195+
> Compares all the characters from `str` with a character buffer pointed to by `buffer` for `length` characters. If `length != str.Length`, the return value will always be false. It is up to you to ensure that the buffer has at least `length` characters remaining, or this method may read from memory outside your buffer.
196+
197+
```csharp
198+
bool AreEqual(char* aPtr, char* bPtr, int length)
199+
```
200+
201+
> Compares characters from two buffers pointed at by `aPtr` and `bPtr` for `length` characters. It is up to you to ensure both buffers have at least `length` characters remaining, or this method may read from memory outside your buffers.
202+

0 commit comments

Comments
 (0)