corefxlab icon indicating copy to clipboard operation
corefxlab copied to clipboard

TryParseBoolean: Implement a case-sensitive fast parse that reads and compares to constant

Open botaberg opened this issue 10 years ago • 5 comments

+        private unsafe static bool IsTrue(byte* utf8Bytes, int length)
+        {
+            if (length < 4)
+                return false;
+
+            byte firstChar = utf8Bytes[0];
+            if (firstChar != 't' && firstChar != 'T')

The current implementation of TryParseBoolean has a lot of branches. This can be made more performant by comparing against a constant like in Jan's suggestion: return (span.ReadInt32LittleEndian() & ~0x20202020) == 'T' + 0x100 * 'R' + 0x10000 * 'U' + 0x1000000 * 'E';

botaberg avatar Nov 08 '16 00:11 botaberg

Ati, might be worth measuring the perf impact and either doing the change or closing this issue.

KrzysztofCwalina avatar Nov 09 '17 21:11 KrzysztofCwalina

span.ReadInt32LittleEndian() might have been the feature I was looking for here:

https://github.com/dotnet/corefx/blob/ba0e14d957cd2fed3e1dec54d2c5f2a92a746c74/src/System.Memory/src/System/Buffers/Text/Utf8Parser/Utf8Parser.Date.R.cs#L33

should see if this parser works faster using ReadInt32LittleEndian.

ghost avatar Nov 09 '17 23:11 ghost

@AtsushiKan Do you plan on merging your proposed commit into corefx master? The change looks fine.

GrabYourPitchforks avatar May 30 '18 23:05 GrabYourPitchforks

Oh, right, that fell through the cracks over vacation. Sure, I can open up a PR.

ghost avatar May 31 '18 00:05 ghost

Fixed.

ghost avatar Sep 05 '18 19:09 ghost