Skip to content

Commit

Permalink
PlainTextSerializer optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
sanych-sun committed Aug 17, 2023
1 parent e7955dd commit 367d83c
Show file tree
Hide file tree
Showing 61 changed files with 1,468 additions and 2,295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class BooleanConverter: SimpleConverterBase<bool>
public class BooleanConverter: PrimitiveTypeConverterBase<bool>
{
protected override bool TryFormatContent(bool content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(bool content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out bool value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out bool value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class ByteConverter: SimpleConverterBase<byte>
public class ByteConverter: PrimitiveTypeConverterBase<byte>
{
protected override bool TryFormatContent(byte content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(byte content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out byte value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out byte value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class DateTimeOffsetConverter : SimpleConverterBase<DateTimeOffset>
public class DateTimeOffsetConverter : PrimitiveTypeConverterBase<DateTimeOffset>
{
protected override bool TryFormatContent(DateTimeOffset content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(DateTimeOffset content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten, new StandardFormat('O'));

protected override bool TryParseContent(ReadOnlySpan<byte> data, out DateTimeOffset value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out DateTimeOffset value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed, 'O');
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class DecimalConverter : SimpleConverterBase<decimal>
public class DecimalConverter : PrimitiveTypeConverterBase<decimal>
{
protected override bool TryFormatContent(decimal content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(decimal content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out decimal value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out decimal value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class DoubleConverter : SimpleConverterBase<double>
public class DoubleConverter : PrimitiveTypeConverterBase<double>
{
protected override bool TryFormatContent(double content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(double content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out double value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out double value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class GuidConverter: SimpleConverterBase<Guid>
public class GuidConverter: PrimitiveTypeConverterBase<Guid>
{
protected override bool TryFormatContent(Guid content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(Guid content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten, new StandardFormat('B'));

protected override bool TryParseContent(ReadOnlySpan<byte> data, out Guid value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out Guid value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed, 'B');
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class Int16Converter: SimpleConverterBase<short>
public class Int16Converter: PrimitiveTypeConverterBase<short>
{
protected override bool TryFormatContent(short content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(short content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out short value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out short value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class Int32Converter : SimpleConverterBase<int>
public class Int32Converter : PrimitiveTypeConverterBase<int>
{
protected override bool TryFormatContent(int content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(int content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out int value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out int value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class Int64Converter : SimpleConverterBase<long>
public class Int64Converter : PrimitiveTypeConverterBase<long>
{
protected override bool TryFormatContent(long content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(long content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out long value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out long value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Buffers;

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

internal class NullableConverterWrapper<T> : IConverter<T?>
where T : struct

{
private readonly IConverter<T> wrappedConverter;

public NullableConverterWrapper(IConverter<T> wrappedConverter)
{
this.wrappedConverter = wrappedConverter;
}

public void Format(T? content, IBufferWriter<byte> writer)
{
if (!content.HasValue)
{
return;
}

this.wrappedConverter.Format(content.Value, writer);
}

public T? Parse(ReadOnlySequence<byte> bytes)
{
if (bytes.IsEmpty)
{
return null;
}

return this.wrappedConverter.Parse(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Buffers;

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public abstract class PrimitiveTypeConverterBase<T> : IConverter<T>
where T : struct
{
public void Format(T content, IBufferWriter<byte> writer)
{
var target = writer.GetSpan();
if (this.TryFormat(content, target, out int bytesWritten))
{
writer.Advance(bytesWritten);
return;
}

// should never be here.
throw new OutOfMemoryException();
}

public T Parse(ReadOnlySequence<byte> bytes)
{
T ParseSource(ReadOnlySpan<byte> data)
{
if(this.TryParse(data, out T result, out var consumed))
{
if (consumed != data.Length)
{
throw new FormatException("Found some extra bytes after the content parsed.");
}

return result;
}

throw new FormatException($"Cannot read the payload as {typeof(T)}.");
}

if (bytes.IsEmpty)
{
throw new FormatException($"Cannot read the payload as {typeof(T)}.");
}

if (bytes.IsSingleSegment)
{
return ParseSource(bytes.FirstSpan);
}

// It's unlikely to get here, but it's safer to have fallback
Span<byte> buffer = stackalloc byte[(int)bytes.Length];
bytes.CopyTo(buffer);

return ParseSource(buffer);
}


protected abstract bool TryFormat(T content, Span<byte> target, out int bytesWritten);

protected abstract bool TryParse(ReadOnlySpan<byte> data, out T value, out int bytesConsumed);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class SByteConverter: SimpleConverterBase<sbyte>
public class SByteConverter: PrimitiveTypeConverterBase<sbyte>
{
protected override bool TryFormatContent(sbyte content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(sbyte content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out sbyte value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out sbyte value, out int bytesConsumed)
=> Utf8Parser.TryParse(data, out value, out bytesConsumed);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace RabbitMQ.Next.Serialization.PlainText.Converters;

public class SingleConverter : SimpleConverterBase<float>
public class SingleConverter : PrimitiveTypeConverterBase<float>
{
protected override bool TryFormatContent(float content, Span<byte> target, out int bytesWritten)
protected override bool TryFormat(float content, Span<byte> target, out int bytesWritten)
=> Utf8Formatter.TryFormat(content, target, out bytesWritten);

protected override bool TryParseContent(ReadOnlySpan<byte> data, out float value, out int bytesConsumed)
protected override bool TryParse(ReadOnlySpan<byte> data, out float value, out int bytesConsumed)
=>Utf8Parser.TryParse(data, out value, out bytesConsumed);
}
Loading

0 comments on commit 367d83c

Please sign in to comment.