You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ This library is a small collection of specialized helper types which focus on pe
5
5
-[StopwatchStruct](#stopwatchstruct)
6
6
-[StringSet](#stringset)
7
7
-[StringHash](#stringhash)
8
+
-[UnsafeStringComparer](#unsafestringcomparer)
8
9
9
10
## StopwatchStruct
10
11
@@ -165,3 +166,37 @@ for (var i = 0; i < buffer.Length; i++)
165
166
166
167
Obviouslythat's a lot more code, but it may be worth it for high-volume parsers.
167
168
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
+
Forstringswithsevenorfewercharacters, theyarecomparedonecharacteratatime. Forstringswitheightormorecharacters, UnsafeStringComparerwillswitch 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(stringstr, char[] buffer)
181
+
```
182
+
183
+
> Compares all characters from `str` with all characters in `buffer`.
> 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(stringstr, char*buffer, intlength)
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, intlength)
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.
0 commit comments