Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions src/LightProto.Generator/LightProtoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ IEnumerable<string> Gen()
yield return $" public bool IsMessage => true;";
yield return $" public WireFormat.WireType WireType => WireFormat.WireType.LengthDelimited;";
yield return $" public void WriteTo(ref WriterContext output, {className} message) => MemberStructWriter.WriteTo(ref output, MemberStruct.FromMessage(message));";
yield return $" public int CalculateSize({className} message) => MemberStructWriter.CalculateSize(MemberStruct.FromMessage(message));";
yield return "}";
}
},
Expand All @@ -258,7 +257,6 @@ IEnumerable<string> Gen()
yield return $" public bool IsMessage => true;";
yield return $" public WireFormat.WireType WireType => WireFormat.WireType.LengthDelimited;";
yield return $" public void WriteTo(ref WriterContext output, {className} message) => MemberStructWriter.WriteTo(ref output, MemberStruct.FromMessage(message));";
yield return $" public int CalculateSize({className} message) => MemberStructWriter.CalculateSize(MemberStruct.FromMessage(message));";
yield return "}";
}
}
Expand Down Expand Up @@ -435,42 +433,6 @@ IEnumerable<string> Gen()
derivedTypes.Select(member => $"if(message.{member.Contract.Type.Name}_MemberStruct.HasValue) {{ output.WriteTag({member.RawTag}); {member.Contract.Type}.MemberStructWriter.WriteMessageTo(ref output, message.{member.Contract.Type.Name}_MemberStruct.Value); }}"))
}}
}

public int CalculateSize(MemberStruct message) {
int size=0;
{{string.Join(Environment.NewLine + GetIntendedSpace(3),
protoMembers.SelectMany(member => {
return Gen();

IEnumerable<string> Gen()
{
var tagSize = member.RawTagSize;
var checkIfNotEmpty = GetCheckIfNotEmpty(member,"message");

if (IsCollectionType(compilation, member.Type) || IsDictionaryType(compilation, member.Type))
{
yield return $"if(message.{member.Name}!=null)";
yield return $" size += {member.Name}_ProtoWriter.CalculateSize(message.{member.Name}); ";
}
else if (TryGetInternalTypeName(member.Type, member.DataFormat,member.StringIntern, out var name))
{
yield return $"if({checkIfNotEmpty})";
yield return $" size += {tagSize} + CodedOutputStream.Compute{name}Size(message.{member.Name});";
}
else
{
yield return $"if({checkIfNotEmpty})";
yield return $" size += {tagSize} + {member.Name}_ProtoWriter.CalculateMessageSize(message.{member.Name});";
}
}
}))
}}

{{string.Join(Environment.NewLine + GetIntendedSpace(3),
derivedTypes.Select(member => $"if(message.{member.Contract.Type.Name}_MemberStruct.HasValue) {{ size+={ProtoMember.GetRawTagSize(member.RawTag)}+{member.Contract.Type}.MemberStructWriter.CalculateMessageSize(message.{member.Contract.Type.Name}_MemberStruct.Value); }}"))
}}
return size;
}
}

public sealed new class MemberStructLightProtoReader:IProtoReader<MemberStruct>
Expand Down Expand Up @@ -672,39 +634,6 @@ IEnumerable<string> Gen()
}))
}}
}

public int CalculateSize({{proxyFor?.ToDisplayString()??className}} value) {
{{className}} message = value;
int size=0;
{{string.Join(Environment.NewLine + GetIntendedSpace(3),
protoMembers.SelectMany(member => {
return Gen();

IEnumerable<string> Gen()
{
var tagSize = member.RawTagSize;
var checkIfNotEmpty = GetCheckIfNotEmpty(member,"message");

if (IsCollectionType(compilation, member.Type) || IsDictionaryType(compilation, member.Type))
{
yield return $"if(message.{member.Name}!=null)";
yield return $" size += {member.Name}_ProtoWriter.CalculateSize(message.{member.Name}); ";
}
else if (TryGetInternalTypeName(member.Type, member.DataFormat,member.StringIntern, out var name))
{
yield return $"if({checkIfNotEmpty})";
yield return $" size += {tagSize} + CodedOutputStream.Compute{name}Size(message.{member.Name});";
}
else
{
yield return $"if({checkIfNotEmpty})";
yield return $" size += {tagSize} + {member.Name}_ProtoWriter.CalculateMessageSize(message.{member.Name});";
}
}
}))
}}
return size;
}
}

public sealed class LightProtoReader:IProtoReader<{{proxyFor?.ToDisplayString()??className}}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
#if !NET6_0_OR_GREATER
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Copied from https://github.com/dotnet/corefx/blob/b0751dcd4a419ba6731dcaa7d240a8a1946c934c/src/System.Text.Json/src/System/Text/Json/Serialization/ArrayBufferWriter.cs

using System.Buffers;
using System.Diagnostics;

namespace LightProto.Tests;
namespace LightProto;

// Note: this is currently an internal class that will be replaced with a shared version.
public sealed class ArrayBufferWriter<T> : IBufferWriter<T>, IDisposable
internal sealed class ByteArrayPoolBufferWriter : IBufferWriter<byte>, IDisposable
{
private T[] _rentedBuffer;
private byte[] _rentedBuffer;
private int _index;

private const int MinimumBufferSize = 256;

public ArrayBufferWriter()
public ByteArrayPoolBufferWriter()
{
_rentedBuffer = ArrayPool<T>.Shared.Rent(MinimumBufferSize);
_rentedBuffer = ArrayPool<byte>.Shared.Rent(MinimumBufferSize);
_index = 0;
}

public ArrayBufferWriter(int initialCapacity)
public ByteArrayPoolBufferWriter(int initialCapacity)
{
if (initialCapacity <= 0)
throw new ArgumentException(nameof(initialCapacity));

_rentedBuffer = ArrayPool<T>.Shared.Rent(initialCapacity);
_rentedBuffer = ArrayPool<byte>.Shared.Rent(initialCapacity);
_index = 0;
}

public ReadOnlyMemory<T> WrittenMemory
public ReadOnlyMemory<byte> WrittenMemory
{
get
{
Expand All @@ -42,6 +41,11 @@ public ReadOnlyMemory<T> WrittenMemory
}
}

public void WriteToStream(Stream stream)
{
stream.Write(_rentedBuffer, 0, _index);
}

public int WrittenCount
{
get
Expand Down Expand Up @@ -96,14 +100,14 @@ public void Dispose()
}

ClearHelper();
ArrayPool<T>.Shared.Return(_rentedBuffer);
ArrayPool<byte>.Shared.Return(_rentedBuffer);
_rentedBuffer = null!;
}

private void CheckIfDisposed()
{
if (_rentedBuffer == null)
throw new ObjectDisposedException(nameof(ArrayBufferWriter<T>));
throw new ObjectDisposedException(nameof(ByteArrayPoolBufferWriter));
}

public void Advance(int count)
Expand All @@ -119,15 +123,15 @@ public void Advance(int count)
_index += count;
}

public Memory<T> GetMemory(int sizeHint = 0)
public Memory<byte> GetMemory(int sizeHint = 0)
{
CheckIfDisposed();

CheckAndResizeBuffer(sizeHint);
return _rentedBuffer.AsMemory(_index);
}

public Span<T> GetSpan(int sizeHint = 0)
public Span<byte> GetSpan(int sizeHint = 0)
{
CheckIfDisposed();

Expand Down Expand Up @@ -155,17 +159,17 @@ private void CheckAndResizeBuffer(int sizeHint)

int newSize = checked(_rentedBuffer.Length + growBy);

T[] oldBuffer = _rentedBuffer;
byte[] oldBuffer = _rentedBuffer;

_rentedBuffer = ArrayPool<T>.Shared.Rent(newSize);
_rentedBuffer = ArrayPool<byte>.Shared.Rent(newSize);

Debug.Assert(oldBuffer.Length >= _index);
Debug.Assert(_rentedBuffer.Length >= _index);

Span<T> previousBuffer = oldBuffer.AsSpan(0, _index);
Span<byte> previousBuffer = oldBuffer.AsSpan(0, _index);
previousBuffer.CopyTo(_rentedBuffer);
previousBuffer.Clear();
ArrayPool<T>.Shared.Return(oldBuffer);
ArrayPool<byte>.Shared.Return(oldBuffer);
}

Debug.Assert(_rentedBuffer.Length - _index > 0);
Expand All @@ -177,5 +181,3 @@ private static void ThrowInvalidOperationException(int capacity)
throw new InvalidOperationException("BufferWriterAdvancedTooFar");
}
}

#endif
2 changes: 1 addition & 1 deletion src/LightProto/CodedOutputStream.ComputeSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static int ComputeSInt64Size(long value)
)]
public static int ComputeLengthSize(int length)
{
return ComputeRawVarint32Size((uint)length);
return 5;
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/LightProto/IProtoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public interface IProtoWriter<in T>
{
public WireFormat.WireType WireType { get; }
public bool IsMessage { get; }
public int CalculateSize(T value);
public void WriteTo(ref WriterContext output, T value);
}
5 changes: 5 additions & 0 deletions src/LightProto/InvalidProtocolBufferException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@ internal static InvalidProtocolBufferException SizeLimitExceeded()
+ "Use CodedInputStream.SetSizeLimit() to increase the size limit."
);
}

internal static InvalidProtocolBufferException StringWriteFailed(string reason)
{
return new InvalidProtocolBufferException($"String write failed: {reason}");
}
}
}
5 changes: 0 additions & 5 deletions src/LightProto/Parser/Bool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ sealed class BooleanProtoWriter : IProtoWriter<bool>
public WireFormat.WireType WireType => WireFormat.WireType.Varint;
public bool IsMessage => false;

public int CalculateSize(bool value)
{
return CodedOutputStream.ComputeBoolSize(value);
}

public void WriteTo(ref WriterContext output, bool value)
{
output.WriteBool(value);
Expand Down
11 changes: 1 addition & 10 deletions src/LightProto/Parser/ByteArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ sealed class ByteArrayProtoWriter : IProtoWriter<byte[]>
public WireFormat.WireType WireType => WireFormat.WireType.LengthDelimited;
public bool IsMessage => false;

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
public int CalculateSize(byte[] value)
{
return CodedOutputStream.ComputeLengthSize(value.Length) + value.Length;
}

public void WriteTo(ref WriterContext output, byte[] value)
{
output.WriteLength(value.Length);
WritingPrimitives.WriteRawBytes(ref output.buffer, ref output.state, value);
output.WriteBytes(value);
}
}
}
13 changes: 1 addition & 12 deletions src/LightProto/Parser/ByteList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,9 @@ sealed class ByteListProtoWriter : IProtoWriter<List<byte>>
public WireFormat.WireType WireType => WireFormat.WireType.LengthDelimited;
public bool IsMessage => false;

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
public int CalculateSize(List<byte> value)
{
return CodedOutputStream.ComputeLengthSize(value.Count) + value.Count;
}

public void WriteTo(ref WriterContext output, List<byte> value)
{
output.WriteLength(value.Count);
WritingPrimitives.WriteRawBytes(
ref output.buffer,
ref output.state,
output.WriteBytes(
#if NET5_0_OR_GREATER
CollectionsMarshal.AsSpan(value)
#else
Expand Down
8 changes: 0 additions & 8 deletions src/LightProto/Parser/DateOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ sealed class DateOnlyProtoWriter : IProtoWriter<DateOnly>
public WireFormat.WireType WireType => WireFormat.WireType.Varint;
public bool IsMessage => false;

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
public int CalculateSize(DateOnly value)
{
return CodedOutputStream.ComputeInt32Size(value.DayNumber);
}

public void WriteTo(ref WriterContext output, DateOnly value)
{
output.WriteInt32(value.DayNumber);
Expand Down
5 changes: 0 additions & 5 deletions src/LightProto/Parser/Decimal300.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ sealed class Decimal300ProtoWriter : IProtoWriter<Decimal>
public WireFormat.WireType WireType => WireFormat.WireType.LengthDelimited;
public bool IsMessage => false;

public int CalculateSize(Decimal value)
{
return CodedOutputStream.ComputeStringSize(value.ToString("G"));
}

public void WriteTo(ref WriterContext output, Decimal value)
{
output.WriteString(value.ToString("G"));
Expand Down
8 changes: 0 additions & 8 deletions src/LightProto/Parser/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ sealed class DoubleProtoWriter : IProtoWriter<Double>

public bool IsMessage => false;

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
public int CalculateSize(Double value)
{
return CodedOutputStream.ComputeDoubleSize(value);
}

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
Expand Down
6 changes: 0 additions & 6 deletions src/LightProto/Parser/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ sealed class EnumProtoWriter : IProtoWriter<T>
public WireFormat.WireType WireType => WireFormat.WireType.Varint;
public bool IsMessage => false;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CalculateSize(T value)
{
return CodedOutputStream.ComputeEnumSize(Unsafe.As<T, int>(ref value));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteTo(ref WriterContext output, T value)
{
Expand Down
8 changes: 0 additions & 8 deletions src/LightProto/Parser/FixedInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ sealed class Fixed32ProtoWriter : IProtoWriter<UInt32>
public WireFormat.WireType WireType => WireFormat.WireType.Fixed32;
public bool IsMessage => false;

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
public int CalculateSize(UInt32 value)
{
return CodedOutputStream.ComputeFixed32Size(value);
}

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
Expand Down
8 changes: 0 additions & 8 deletions src/LightProto/Parser/FixedInt64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ sealed class Fixed64ProtoWriter : IProtoWriter<UInt64>
public WireFormat.WireType WireType => WireFormat.WireType.Fixed64;
public bool IsMessage => false;

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
public int CalculateSize(UInt64 value)
{
return CodedOutputStream.ComputeFixed64Size(value);
}

[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining
)]
Expand Down
5 changes: 0 additions & 5 deletions src/LightProto/Parser/Guid300.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ sealed class Guid300ProtoWriter : IProtoWriter<Guid>
public WireFormat.WireType WireType => WireFormat.WireType.LengthDelimited;
public bool IsMessage => false;

public int CalculateSize(Guid value)
{
return CodedOutputStream.ComputeStringSize(value.ToString());
}

public void WriteTo(ref WriterContext output, Guid value)
{
output.WriteString(value.ToString());
Expand Down
Loading
Loading