Skip to content

Commit

Permalink
C#: Optimize parsing of some primitive and wrapper types
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdunelm committed Nov 4, 2019
1 parent 4668a33 commit e305e56
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 27 deletions.
267 changes: 242 additions & 25 deletions csharp/src/Google.Protobuf/CodedInputStream.cs
Expand Up @@ -481,7 +481,33 @@ internal void SkipGroup(uint startGroupTag)
/// </summary>
public double ReadDouble()
{
return BitConverter.Int64BitsToDouble((long) ReadRawLittleEndian64());
if (bufferPos + 8 <= bufferSize)
{
if (BitConverter.IsLittleEndian)
{
var result = BitConverter.ToDouble(buffer, bufferPos);
bufferPos += 8;
return result;
}
else
{
var bytes = new byte[8];
bytes[0] = buffer[bufferPos + 7];
bytes[1] = buffer[bufferPos + 6];
bytes[2] = buffer[bufferPos + 5];
bytes[3] = buffer[bufferPos + 4];
bytes[4] = buffer[bufferPos + 3];
bytes[5] = buffer[bufferPos + 2];
bytes[6] = buffer[bufferPos + 1];
bytes[7] = buffer[bufferPos];
bufferPos += 8;
return BitConverter.ToDouble(bytes, 0);
}
}
else
{
return BitConverter.Int64BitsToDouble((long)ReadRawLittleEndian64());
}
}

/// <summary>
Expand Down Expand Up @@ -711,7 +737,126 @@ public bool MaybeConsumeTag(uint tag)
return false;
}

#endregion
internal static double? ReadDoubleWrapperLittleEndian(CodedInputStream input)
{
// tag:1 + value:8 = 9 bytes
const int expectedLength = 9;
// field=1, type=64-bit = tag of 9
const int expectedTag = 9;
// length:1 + tag:1 + value:8 = 10 bytes
if (input.bufferPos + 10 <= input.bufferSize)
{
int length = input.buffer[input.bufferPos];
if (length == 0)
{
input.bufferPos++;
return 0D;
}
if (length != expectedLength)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageLength();
}
// field=1, type=64-bit = tag of 9
if (input.buffer[input.bufferPos + 1] != expectedTag)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageTag();
}
var result = BitConverter.ToDouble(input.buffer, input.bufferPos + 2);
input.bufferPos += 10;
return result;
}
else
{
int length = input.ReadLength();
if (length == 0)
{
return 0D;
}
if (length != expectedLength)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageLength();
}
if (input.ReadTag() != expectedTag)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageTag();
}
return input.ReadDouble();
}
}

internal static double? ReadDoubleWrapperBigEndian(CodedInputStream input)
{
int length = input.ReadLength();
if (length == 0)
{
return 0D;
}
// tag:1 + value:8 = 9 bytes
if (length != 9)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageLength();
}
// field=1, type=64-bit = tag of 9
if (input.ReadTag() != 9)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageTag();
}
return input.ReadDouble();
}

internal static long? ReadInt64Wrapper(CodedInputStream input)
{
// field=1, type=varint = tag of 8
const int expectedTag = 8;
// length:1 + tag:1 + value:10(varint64-max) = 12 bytes
if (input.bufferPos + 12 <= input.bufferSize)
{
int length = input.buffer[input.bufferPos++];
if (length == 0)
{
return 0L;
}
// Length will always fit in a single byte.
if (length >= 128)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageLength();
}
int finalBufferPos = input.bufferPos + length;
if (input.buffer[input.bufferPos++] != expectedTag)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageTag();
}
var result = input.ReadInt64();
// Verify this message only contained a single field.
if (input.bufferPos != finalBufferPos)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageExtraFields();
}
return result;
}
else
{
int length = input.ReadLength();
if (length == 0)
{
return 0L;
}
int finalBufferPos = input.totalBytesRetired + input.bufferPos + length;
if (input.ReadTag() != expectedTag)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageTag();
}
var result = input.ReadInt64();
// Verify this message only contained a single field.
if (input.totalBytesRetired + input.bufferPos != finalBufferPos)
{
throw InvalidProtocolBufferException.InvalidWrapperMessageExtraFields();
}
return result;
}
}

#endregion

#region Underlying reading primitives

Expand Down Expand Up @@ -876,17 +1021,42 @@ internal static uint ReadRawVarint32(Stream input)
/// </summary>
internal ulong ReadRawVarint64()
{
int shift = 0;
ulong result = 0;
while (shift < 64)
if (bufferPos + 10 <= bufferSize)
{
byte b = ReadRawByte();
result |= (ulong) (b & 0x7F) << shift;
if ((b & 0x80) == 0)
ulong result = buffer[bufferPos++];
if (result < 128)
{
return result;
}
shift += 7;
result &= 0x7f;
int shift = 7;
do
{
byte b = buffer[bufferPos++];
result |= (ulong)(b & 0x7F) << shift;
if (b < 0x80)
{
return result;
}
shift += 7;
}
while (shift < 64);
}
else
{
int shift = 0;
ulong result = 0;
do
{
byte b = ReadRawByte();
result |= (ulong)(b & 0x7F) << shift;
if (b < 0x80)
{
return result;
}
shift += 7;
}
while (shift < 64);
}
throw InvalidProtocolBufferException.MalformedVarint();
}
Expand All @@ -896,28 +1066,75 @@ internal ulong ReadRawVarint64()
/// </summary>
internal uint ReadRawLittleEndian32()
{
uint b1 = ReadRawByte();
uint b2 = ReadRawByte();
uint b3 = ReadRawByte();
uint b4 = ReadRawByte();
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
if (bufferPos + 4 <= bufferSize)
{
if (BitConverter.IsLittleEndian)
{
var result = BitConverter.ToUInt32(buffer, bufferPos);
bufferPos += 4;
return result;
}
else
{
uint b1 = buffer[bufferPos];
uint b2 = buffer[bufferPos + 1];
uint b3 = buffer[bufferPos + 2];
uint b4 = buffer[bufferPos + 3];
bufferPos += 4;
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
}
}
else
{
uint b1 = ReadRawByte();
uint b2 = ReadRawByte();
uint b3 = ReadRawByte();
uint b4 = ReadRawByte();
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
}
}

/// <summary>
/// Reads a 64-bit little-endian integer from the stream.
/// </summary>
internal ulong ReadRawLittleEndian64()
{
ulong b1 = ReadRawByte();
ulong b2 = ReadRawByte();
ulong b3 = ReadRawByte();
ulong b4 = ReadRawByte();
ulong b5 = ReadRawByte();
ulong b6 = ReadRawByte();
ulong b7 = ReadRawByte();
ulong b8 = ReadRawByte();
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
| (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
if (bufferPos + 8 <= bufferSize)
{
if (BitConverter.IsLittleEndian)
{
var result = BitConverter.ToUInt64(buffer, bufferPos);
bufferPos += 8;
return result;
}
else
{
ulong b1 = buffer[bufferPos];
ulong b2 = buffer[bufferPos + 1];
ulong b3 = buffer[bufferPos + 2];
ulong b4 = buffer[bufferPos + 3];
ulong b5 = buffer[bufferPos + 4];
ulong b6 = buffer[bufferPos + 5];
ulong b7 = buffer[bufferPos + 6];
ulong b8 = buffer[bufferPos + 7];
bufferPos += 8;
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
| (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
}
}
else
{
ulong b1 = ReadRawByte();
ulong b2 = ReadRawByte();
ulong b3 = ReadRawByte();
ulong b4 = ReadRawByte();
ulong b5 = ReadRawByte();
ulong b6 = ReadRawByte();
ulong b7 = ReadRawByte();
ulong b8 = ReadRawByte();
return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
| (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56);
}
}

/// <summary>
Expand Down Expand Up @@ -1301,6 +1518,6 @@ private void SkipImpl(int amountToSkip)
}
}
}
#endregion
#endregion
}
}
35 changes: 34 additions & 1 deletion csharp/src/Google.Protobuf/FieldCodec.cs
Expand Up @@ -507,7 +507,7 @@ public static FieldCodec<T> ForEnum<T>(uint tag, Func<T, int> toInt32, Func<int,
{
var nestedCodec = WrapperCodecs.GetCodec<T>();
return new FieldCodec<T?>(
input => WrapperCodecs.Read<T>(input, nestedCodec),
WrapperCodecs.GetReader<T>(),
(output, value) => WrapperCodecs.Write<T>(output, value.Value, nestedCodec),
(CodedInputStream i, ref T? v) => v = WrapperCodecs.Read<T>(i, nestedCodec),
(ref T? v, T? v2) => { if (v2.HasValue) { v = v2; } return v.HasValue; },
Expand Down Expand Up @@ -539,6 +539,22 @@ private static class WrapperCodecs
{ typeof(ByteString), ForBytes(WireFormat.MakeTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.LengthDelimited)) }
};

private static readonly Dictionary<System.Type, Func<object>> Readers = new Dictionary<System.Type, Func<object>>
{
// TODO: Provide more optimized readers.
{ typeof(bool), null },
{ typeof(int), null },
{ typeof(long), () => (Func<CodedInputStream, long?>)CodedInputStream.ReadInt64Wrapper },
{ typeof(uint), null },
{ typeof(ulong), null },
{ typeof(float), null },
{ typeof(double), () => BitConverter.IsLittleEndian ?
(Func<CodedInputStream, double?>)CodedInputStream.ReadDoubleWrapperLittleEndian :
(Func<CodedInputStream, double?>)CodedInputStream.ReadDoubleWrapperBigEndian },
{ typeof(string), null },
{ typeof(ByteString), null },
};

/// <summary>
/// Returns a field codec which effectively wraps a value of type T in a message.
///
Expand All @@ -553,6 +569,23 @@ internal static FieldCodec<T> GetCodec<T>()
return (FieldCodec<T>) value;
}

internal static Func<CodedInputStream, T?> GetReader<T>() where T : struct
{
Func<object> value;
if (!Readers.TryGetValue(typeof(T), out value))
{
throw new InvalidOperationException("Invalid type argument requested for wrapper reader: " + typeof(T));
}
if (value == null)
{
// Return default unoptimized reader for the wrapper type.
var nestedCoded = GetCodec<T>();
return input => Read<T>(input, nestedCoded);
}
// Return optimized read for the wrapper type.
return (Func<CodedInputStream, T?>)value();
}

internal static T Read<T>(CodedInputStream input, FieldCodec<T> codec)
{
int length = input.ReadLength();
Expand Down
17 changes: 16 additions & 1 deletion csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs
Expand Up @@ -136,5 +136,20 @@ internal static InvalidProtocolBufferException MissingFields()
{
return new InvalidProtocolBufferException("Message was missing required fields");
}
}

internal static InvalidProtocolBufferException InvalidWrapperMessageLength()
{
return new InvalidProtocolBufferException("Wrapper type message length is incorrect.");
}

internal static InvalidProtocolBufferException InvalidWrapperMessageTag()
{
return new InvalidProtocolBufferException("Wrapper type message tag is incorrect.");
}

internal static InvalidProtocolBufferException InvalidWrapperMessageExtraFields()
{
return new InvalidProtocolBufferException("Wrapper type message contains invalid extra field(s).");
}
}
}

0 comments on commit e305e56

Please sign in to comment.