Skip to content

Commit 12a3bfe

Browse files
committed
Very minor improvement to ValueSpan.GetInt().
It's about ~3% when deserializing an array of 24 integers.
1 parent bcb17d4 commit 12a3bfe

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

PhpSerializerNET/Deserialization/ValueSpan.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ internal double GetDouble(ReadOnlySpan<byte> input) {
3838
internal int GetInt(ReadOnlySpan<byte> input) {
3939
// All the PHP integers we deal with here can only be the number characters and an optional "-".
4040
// See also the Validator code.
41-
// 'long.Parse()' has to take into account that we can skip here, making this manual approach faster.
41+
// 'long.Parse()' has to make considerations that we can skip here, making this manual approach faster.
4242
var span = input.Slice(this.Start, this.Length);
43-
int i = 0;
44-
bool isNegative = false;
45-
if (span[0] == '-') {
46-
i++;
47-
isNegative = true;
43+
if (span[0] == (byte)'-') {
44+
int result = span[1] - 48;
45+
for (int i = 2; i < span.Length; i++) {
46+
result = result * 10 + (span[i] - 48);
47+
}
48+
return result*-1;
49+
} else {
50+
int result = span[0] - 48;
51+
for (int i = 1; i < span.Length; i++) {
52+
result = result * 10 + (span[i] - 48);
53+
}
54+
return result;
4855
}
49-
int result = 0;
50-
for (; i < span.Length; i++) {
51-
result = result * 10 + (span[i] - 48);
52-
}
53-
return isNegative ? result * -1 : result;
5456
}
5557

5658
internal string GetString(ReadOnlySpan<byte> input, Encoding inputEncoding) {

0 commit comments

Comments
 (0)