diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2018-12-26 17:28:43 +0300 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2018-12-26 17:28:43 +0300 |
commit | 1d82fbe81a21487654200a1825882f96b7e7f63f (patch) | |
tree | e7be1388fb410e6153d6620fd1e898981de3f7e0 | |
parent | 2c15bce027681b03ca46c5bc62d10382dc3a8b08 (diff) |
34 files changed, 2056 insertions, 4731 deletions
diff --git a/sandbox/DynamicCodeDumper/DynamicCodeDumper.csproj b/sandbox/DynamicCodeDumper/DynamicCodeDumper.csproj index e36ad5ee..9395f9eb 100644 --- a/sandbox/DynamicCodeDumper/DynamicCodeDumper.csproj +++ b/sandbox/DynamicCodeDumper/DynamicCodeDumper.csproj @@ -77,4 +77,7 @@ <Link>Class1.cs</Link> </Compile> </ItemGroup> + <ItemGroup> + <PackageReference Include="System.Memory" Version="4.5.1" /> + </ItemGroup> </Project> diff --git a/src/MessagePack/FloatBits.cs b/src/MessagePack/FloatBits.cs index 46dee4dd..c1c11640 100644 --- a/src/MessagePack/FloatBits.cs +++ b/src/MessagePack/FloatBits.cs @@ -30,23 +30,23 @@ namespace MessagePack this.Value = value; } - public Float32Bits(byte[] bigEndianBytes, int offset) + public Float32Bits(ReadOnlySpan<byte> bigEndianBytes) { this = default(Float32Bits); if (BitConverter.IsLittleEndian) { - this.Byte0 = bigEndianBytes[offset + 3]; - this.Byte1 = bigEndianBytes[offset + 2]; - this.Byte2 = bigEndianBytes[offset + 1]; - this.Byte3 = bigEndianBytes[offset]; + this.Byte0 = bigEndianBytes[3]; + this.Byte1 = bigEndianBytes[2]; + this.Byte2 = bigEndianBytes[1]; + this.Byte3 = bigEndianBytes[0]; } else { - this.Byte0 = bigEndianBytes[offset]; - this.Byte1 = bigEndianBytes[offset + 1]; - this.Byte2 = bigEndianBytes[offset + 2]; - this.Byte3 = bigEndianBytes[offset + 3]; + this.Byte0 = bigEndianBytes[0]; + this.Byte1 = bigEndianBytes[1]; + this.Byte2 = bigEndianBytes[2]; + this.Byte3 = bigEndianBytes[3]; } } } @@ -87,31 +87,31 @@ namespace MessagePack this.Value = value; } - public Float64Bits(byte[] bigEndianBytes, int offset) + public Float64Bits(ReadOnlySpan<byte> bigEndianBytes) { this = default(Float64Bits); if (BitConverter.IsLittleEndian) { - this.Byte0 = bigEndianBytes[offset + 7]; - this.Byte1 = bigEndianBytes[offset + 6]; - this.Byte2 = bigEndianBytes[offset + 5]; - this.Byte3 = bigEndianBytes[offset + 4]; - this.Byte4 = bigEndianBytes[offset + 3]; - this.Byte5 = bigEndianBytes[offset + 2]; - this.Byte6 = bigEndianBytes[offset + 1]; - this.Byte7 = bigEndianBytes[offset]; + this.Byte0 = bigEndianBytes[7]; + this.Byte1 = bigEndianBytes[6]; + this.Byte2 = bigEndianBytes[5]; + this.Byte3 = bigEndianBytes[4]; + this.Byte4 = bigEndianBytes[3]; + this.Byte5 = bigEndianBytes[2]; + this.Byte6 = bigEndianBytes[1]; + this.Byte7 = bigEndianBytes[0]; } else { - this.Byte0 = bigEndianBytes[offset]; - this.Byte1 = bigEndianBytes[offset + 1]; - this.Byte2 = bigEndianBytes[offset + 2]; - this.Byte3 = bigEndianBytes[offset + 3]; - this.Byte4 = bigEndianBytes[offset + 4]; - this.Byte5 = bigEndianBytes[offset + 5]; - this.Byte6 = bigEndianBytes[offset + 6]; - this.Byte7 = bigEndianBytes[offset + 7]; + this.Byte0 = bigEndianBytes[0]; + this.Byte1 = bigEndianBytes[1]; + this.Byte2 = bigEndianBytes[2]; + this.Byte3 = bigEndianBytes[3]; + this.Byte4 = bigEndianBytes[4]; + this.Byte5 = bigEndianBytes[5]; + this.Byte6 = bigEndianBytes[6]; + this.Byte7 = bigEndianBytes[7]; } } } diff --git a/src/MessagePack/Formatters/CollectionFormatter.cs b/src/MessagePack/Formatters/CollectionFormatter.cs index de17ec59..3b370045 100644 --- a/src/MessagePack/Formatters/CollectionFormatter.cs +++ b/src/MessagePack/Formatters/CollectionFormatter.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Buffers; #if NETSTANDARD || NETFRAMEWORK using System.Collections.Concurrent; @@ -12,49 +13,42 @@ namespace MessagePack.Formatters { public sealed class ArrayFormatter<T> : IMessagePackFormatter<T[]> { - public int Serialize(ref byte[] bytes, int offset, T[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify<T>(); - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += formatter.Serialize(ref bytes, offset, value[i], formatterResolver); + formatter.Serialize(writer, value[i], formatterResolver); } - - return offset - startOffset; } } - public T[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify<T>(); - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new T[len]; for (int i = 0; i < array.Length; i++) { - array[i] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + array[i] = formatter.Deserialize(ref byteSequence, formatterResolver); } - readSize = offset - startOffset; return array; } } @@ -64,34 +58,34 @@ namespace MessagePack.Formatters { public static readonly ByteArraySegmentFormatter Instance = new ByteArraySegmentFormatter(); - ByteArraySegmentFormatter() + private ByteArraySegmentFormatter() { } - public int Serialize(ref byte[] bytes, int offset, ArraySegment<byte> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ArraySegment<byte> value, IFormatterResolver formatterResolver) { if (value.Array == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteBytes(ref bytes, offset, value.Array, value.Offset, value.Count); + MessagePackBinary.WriteBytes(writer, value); } } - public ArraySegment<byte> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ArraySegment<byte> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return default(ArraySegment<byte>); } else { // use ReadBytesSegment? But currently straem api uses memory pool so can't save arraysegment... - var binary = MessagePackBinary.ReadBytes(bytes, offset, out readSize); + byte[] binary = MessagePackBinary.ReadBytes(ref byteSequence); return new ArraySegment<byte>(binary, 0, binary.Length); } } @@ -99,7 +93,7 @@ namespace MessagePack.Formatters public sealed class ArraySegmentFormatter<T> : IMessagePackFormatter<ArraySegment<T>> { - public int Serialize(ref byte[] bytes, int offset, ArraySegment<T> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ArraySegment<T> value, IFormatterResolver formatterResolver) { if (value.Array == null) { @@ -123,7 +117,7 @@ namespace MessagePack.Formatters } } - public ArraySegment<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ArraySegment<T> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -141,7 +135,7 @@ namespace MessagePack.Formatters // List<T> is popular format, should avoid abstraction. public sealed class ListFormatter<T> : IMessagePackFormatter<List<T>> { - public int Serialize(ref byte[] bytes, int offset, List<T> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, List<T> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -164,7 +158,7 @@ namespace MessagePack.Formatters } } - public List<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public List<T> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -194,7 +188,7 @@ namespace MessagePack.Formatters where TCollection : IEnumerable<TElement> where TEnumerator : IEnumerator<TElement> { - public int Serialize(ref byte[] bytes, int offset, TCollection value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, TCollection value, IFormatterResolver formatterResolver) { if (value == null) { @@ -283,8 +277,14 @@ namespace MessagePack.Formatters var headerLength = MessagePackBinary.GetArrayHeaderLength(count); if (headerLength != 3) { - if (headerLength == 1) offset -= 2; // 1 - else offset += 2; // 5 + if (headerLength == 1) + { + offset -= 2; // 1 + } + else + { + offset += 2; // 5 + } MessagePackBinary.EnsureCapacity(ref bytes, offset, headerLength); Buffer.BlockCopy(bytes, writeStarOffset + 3, bytes, writeStarOffset + headerLength, moveCount); @@ -297,7 +297,7 @@ namespace MessagePack.Formatters } } - public TCollection Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public TCollection Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -571,7 +571,7 @@ namespace MessagePack.Formatters // [Key, [Array]] public sealed class InterfaceGroupingFormatter<TKey, TElement> : IMessagePackFormatter<IGrouping<TKey, TElement>> { - public int Serialize(ref byte[] bytes, int offset, IGrouping<TKey, TElement> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, IGrouping<TKey, TElement> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -587,7 +587,7 @@ namespace MessagePack.Formatters } } - public IGrouping<TKey, TElement> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public IGrouping<TKey, TElement> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -600,7 +600,10 @@ namespace MessagePack.Formatters var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); offset += readSize; - if (count != 2) throw new InvalidOperationException("Invalid Grouping format."); + if (count != 2) + { + throw new InvalidOperationException("Invalid Grouping format."); + } var key = formatterResolver.GetFormatterWithVerify<TKey>().Deserialize(bytes, offset, formatterResolver, out readSize); offset += readSize; @@ -632,10 +635,10 @@ namespace MessagePack.Formatters } } - class Grouping<TKey, TElement> : IGrouping<TKey, TElement> + internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement> { - readonly TKey key; - readonly IEnumerable<TElement> elements; + private readonly TKey key; + private readonly IEnumerable<TElement> elements; public Grouping(TKey key, IEnumerable<TElement> elements) { @@ -662,9 +665,9 @@ namespace MessagePack.Formatters } } - class Lookup<TKey, TElement> : ILookup<TKey, TElement> + internal class Lookup<TKey, TElement> : ILookup<TKey, TElement> { - readonly Dictionary<TKey, IGrouping<TKey, TElement>> groupings; + private readonly Dictionary<TKey, IGrouping<TKey, TElement>> groupings; public Lookup(Dictionary<TKey, IGrouping<TKey, TElement>> groupings) { @@ -708,7 +711,7 @@ namespace MessagePack.Formatters public sealed class NonGenericListFormatter<T> : IMessagePackFormatter<T> where T : class, IList, new() { - public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver) { if (value == null) { @@ -728,7 +731,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -758,12 +761,12 @@ namespace MessagePack.Formatters { public static readonly IMessagePackFormatter<IList> Instance = new NonGenericInterfaceListFormatter(); - NonGenericInterfaceListFormatter() + private NonGenericInterfaceListFormatter() { } - public int Serialize(ref byte[] bytes, int offset, IList value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, IList value, IFormatterResolver formatterResolver) { if (value == null) { @@ -783,7 +786,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public IList Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public IList Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -812,7 +815,7 @@ namespace MessagePack.Formatters public sealed class NonGenericDictionaryFormatter<T> : IMessagePackFormatter<T> where T : class, IDictionary, new() { - public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver) { if (value == null) { @@ -833,7 +836,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -866,12 +869,12 @@ namespace MessagePack.Formatters { public static readonly IMessagePackFormatter<IDictionary> Instance = new NonGenericInterfaceDictionaryFormatter(); - NonGenericInterfaceDictionaryFormatter() + private NonGenericInterfaceDictionaryFormatter() { } - public int Serialize(ref byte[] bytes, int offset, IDictionary value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, IDictionary value, IFormatterResolver formatterResolver) { if (value == null) { @@ -892,7 +895,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public IDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public IDictionary Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { diff --git a/src/MessagePack/Formatters/DictionaryFormatter.cs b/src/MessagePack/Formatters/DictionaryFormatter.cs index ea7a3a6e..bcdd65dd 100644 --- a/src/MessagePack/Formatters/DictionaryFormatter.cs +++ b/src/MessagePack/Formatters/DictionaryFormatter.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Buffers; #if NETSTANDARD || NETFRAMEWORK using System.Collections.Concurrent; @@ -16,15 +17,14 @@ namespace MessagePack.Formatters where TDictionary : IEnumerable<KeyValuePair<TKey, TValue>> where TEnumerator : IEnumerator<KeyValuePair<TKey, TValue>> { - public int Serialize(ref byte[] bytes, int offset, TDictionary value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, TDictionary value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>(); var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>(); @@ -49,7 +49,7 @@ namespace MessagePack.Formatters } } - offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count); + MessagePackBinary.WriteMapHeader(writer, count); var e = GetSourceEnumerator(value); try @@ -57,47 +57,40 @@ namespace MessagePack.Formatters while (e.MoveNext()) { var item = e.Current; - offset += keyFormatter.Serialize(ref bytes, offset, item.Key, formatterResolver); - offset += valueFormatter.Serialize(ref bytes, offset, item.Value, formatterResolver); + keyFormatter.Serialize(writer, item.Key, formatterResolver); + valueFormatter.Serialize(writer, item.Value, formatterResolver); } } finally { e.Dispose(); } - - return offset - startOffset; } } - public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public TDictionary Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return default(TDictionary); } else { - var startOffset = offset; var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>(); var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>(); - var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadMapHeader(ref byteSequence); var dict = Create(len); for (int i = 0; i < len; i++) { - var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + var key = keyFormatter.Deserialize(ref byteSequence, formatterResolver); - var value = valueFormatter.Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + var value = valueFormatter.Deserialize(ref byteSequence, formatterResolver); Add(dict, i, key, value); } - readSize = offset - startOffset; return Complete(dict); } @@ -279,7 +272,7 @@ namespace MessagePack.Formatters public abstract class DictionaryFormatterBase<TKey, TValue, TIntermediate, TDictionary> : IMessagePackFormatter<TDictionary> where TDictionary : IDictionary<TKey, TValue> { - public int Serialize(ref byte[] bytes, int offset, TDictionary value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, TDictionary value, IFormatterResolver formatterResolver) { if (value == null) { @@ -314,7 +307,7 @@ namespace MessagePack.Formatters } } - public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public TDictionary Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { diff --git a/src/MessagePack/Formatters/DynamicObjectTypeFallbackFormatter.cs b/src/MessagePack/Formatters/DynamicObjectTypeFallbackFormatter.cs index 3380229f..c2ea0831 100644 --- a/src/MessagePack/Formatters/DynamicObjectTypeFallbackFormatter.cs +++ b/src/MessagePack/Formatters/DynamicObjectTypeFallbackFormatter.cs @@ -2,6 +2,7 @@ using MessagePack.Resolvers; using System; +using System.Buffers; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -11,7 +12,7 @@ namespace MessagePack.Formatters { public sealed class DynamicObjectTypeFallbackFormatter : IMessagePackFormatter<object> { - delegate int SerializeMethod(object dynamicFormatter, ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver); + delegate int SerializeMethod(object dynamicFormatter, IBufferWriter<byte> writer, object value, IFormatterResolver formatterResolver); readonly MessagePack.Internal.ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>> serializers = new Internal.ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>>(); @@ -22,11 +23,11 @@ namespace MessagePack.Formatters this.innerResolvers = innerResolvers; } - public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, object value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } var type = value.GetType(); @@ -35,7 +36,8 @@ namespace MessagePack.Formatters if (type == typeof(object)) { // serialize to empty map - return MessagePackBinary.WriteMapHeader(ref bytes, offset, 0); + MessagePackBinary.WriteMapHeader(writer, 0); + return; } KeyValuePair<object, SerializeMethod> formatterAndDelegate; @@ -60,22 +62,20 @@ namespace MessagePack.Formatters { var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(t); var param0 = Expression.Parameter(typeof(object), "formatter"); - var param1 = Expression.Parameter(typeof(byte[]).MakeByRefType(), "bytes"); - var param2 = Expression.Parameter(typeof(int), "offset"); - var param3 = Expression.Parameter(typeof(object), "value"); - var param4 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver"); + var param1 = Expression.Parameter(typeof(IBufferWriter<byte>), "byteSequence"); + var param2 = Expression.Parameter(typeof(object), "value"); + var param3 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver"); - var serializeMethodInfo = formatterType.GetRuntimeMethod("Serialize", new[] { typeof(byte[]).MakeByRefType(), typeof(int), t, typeof(IFormatterResolver) }); + var serializeMethodInfo = formatterType.GetRuntimeMethod("Serialize", new[] { typeof(IBufferWriter<byte>), t, typeof(IFormatterResolver) }); var body = Expression.Call( Expression.Convert(param0, formatterType), serializeMethodInfo, param1, - param2, - ti.IsValueType ? Expression.Unbox(param3, t) : Expression.Convert(param3, t), - param4); + ti.IsValueType ? Expression.Unbox(param2, t) : Expression.Convert(param2, t), + param3); - var lambda = Expression.Lambda<SerializeMethod>(body, param0, param1, param2, param3, param4).Compile(); + var lambda = Expression.Lambda<SerializeMethod>(body, param0, param1, param2, param3).Compile(); formatterAndDelegate = new KeyValuePair<object, SerializeMethod>(formatter, lambda); } @@ -85,12 +85,12 @@ namespace MessagePack.Formatters } } - return formatterAndDelegate.Value(formatterAndDelegate.Key, ref bytes, offset, value, formatterResolver); + formatterAndDelegate.Value(formatterAndDelegate.Key, writer, value, formatterResolver); } - public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public object Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return PrimitiveObjectFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize); + return PrimitiveObjectFormatter.Instance.Deserialize(ref byteSequence, formatterResolver); } } } diff --git a/src/MessagePack/Formatters/EnumAsStringFormatter.cs b/src/MessagePack/Formatters/EnumAsStringFormatter.cs index f40f092f..465589f4 100644 --- a/src/MessagePack/Formatters/EnumAsStringFormatter.cs +++ b/src/MessagePack/Formatters/EnumAsStringFormatter.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; namespace MessagePack.Formatters @@ -24,7 +25,7 @@ namespace MessagePack.Formatters } } - public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver) { string name; if (!valueNameMapping.TryGetValue(value, out name)) @@ -32,12 +33,12 @@ namespace MessagePack.Formatters name = value.ToString(); // fallback for flags etc, But Enum.ToString is too slow. } - return MessagePackBinary.WriteString(ref bytes, offset, name); + MessagePackBinary.WriteString(writer, name); } - public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - var name = MessagePackBinary.ReadString(bytes, offset, out readSize); + var name = MessagePackBinary.ReadString(ref byteSequence); T value; if (!nameValueMapping.TryGetValue(name, out value)) diff --git a/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.cs b/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.cs index d3d1e870..4eef6578 100644 --- a/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.cs +++ b/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; namespace MessagePack.Formatters { @@ -10,14 +11,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int16 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int16 value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value); + return MessagePackBinary.WriteInt16ForceInt16Block(writer, value); } - public Int16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int16 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadInt16(bytes, offset, out readSize); + return MessagePackBinary.ReadInt16(ref byteSequence); } } @@ -29,28 +30,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int16? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int16? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value.Value); + MessagePackBinary.WriteInt16ForceInt16Block(writer, value.Value); } } - public Int16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int16? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadInt16(bytes, offset, out readSize); + return MessagePackBinary.ReadInt16(ref byteSequence); } } } @@ -64,45 +65,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Int16[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int16[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value[i]); + MessagePackBinary.WriteInt16ForceInt16Block(writer, value[i]); } - - return offset - startOffset; } } - public Int16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int16[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new Int16[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadInt16(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadInt16(ref byteSequence); } - readSize = offset - startOffset; return array; } } @@ -116,14 +109,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int32 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int32 value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value); + return MessagePackBinary.WriteInt32ForceInt32Block(writer, value); } - public Int32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int32 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadInt32(bytes, offset, out readSize); + return MessagePackBinary.ReadInt32(ref byteSequence); } } @@ -135,28 +128,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int32? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int32? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value.Value); + MessagePackBinary.WriteInt32ForceInt32Block(writer, value.Value); } } - public Int32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int32? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadInt32(bytes, offset, out readSize); + return MessagePackBinary.ReadInt32(ref byteSequence); } } } @@ -170,45 +163,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Int32[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int32[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value[i]); + MessagePackBinary.WriteInt32ForceInt32Block(writer, value[i]); } - - return offset - startOffset; } } - public Int32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int32[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new Int32[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadInt32(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadInt32(ref byteSequence); } - readSize = offset - startOffset; return array; } } @@ -222,14 +207,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int64 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int64 value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value); + return MessagePackBinary.WriteInt64ForceInt64Block(writer, value); } - public Int64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int64 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadInt64(bytes, offset, out readSize); + return MessagePackBinary.ReadInt64(ref byteSequence); } } @@ -241,28 +226,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int64? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int64? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value.Value); + MessagePackBinary.WriteInt64ForceInt64Block(writer, value.Value); } } - public Int64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int64? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadInt64(bytes, offset, out readSize); + return MessagePackBinary.ReadInt64(ref byteSequence); } } } @@ -276,45 +261,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Int64[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int64[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value[i]); + MessagePackBinary.WriteInt64ForceInt64Block(writer, value[i]); } - - return offset - startOffset; } } - public Int64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int64[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new Int64[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadInt64(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadInt64(ref byteSequence); } - readSize = offset - startOffset; return array; } } @@ -328,14 +305,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt16 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt16 value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value); + return MessagePackBinary.WriteUInt16ForceUInt16Block(writer, value); } - public UInt16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt16 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadUInt16(bytes, offset, out readSize); + return MessagePackBinary.ReadUInt16(ref byteSequence); } } @@ -347,28 +324,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt16? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt16? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value.Value); + MessagePackBinary.WriteUInt16ForceUInt16Block(writer, value.Value); } } - public UInt16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt16? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadUInt16(bytes, offset, out readSize); + return MessagePackBinary.ReadUInt16(ref byteSequence); } } } @@ -382,45 +359,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, UInt16[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt16[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value[i]); + MessagePackBinary.WriteUInt16ForceUInt16Block(writer, value[i]); } - - return offset - startOffset; } } - public UInt16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt16[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new UInt16[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadUInt16(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadUInt16(ref byteSequence); } - readSize = offset - startOffset; return array; } } @@ -434,14 +403,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt32 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt32 value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value); + return MessagePackBinary.WriteUInt32ForceUInt32Block(writer, value); } - public UInt32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt32 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadUInt32(bytes, offset, out readSize); + return MessagePackBinary.ReadUInt32(ref byteSequence); } } @@ -453,28 +422,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt32? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt32? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value.Value); + MessagePackBinary.WriteUInt32ForceUInt32Block(writer, value.Value); } } - public UInt32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt32? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadUInt32(bytes, offset, out readSize); + return MessagePackBinary.ReadUInt32(ref byteSequence); } } } @@ -488,45 +457,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, UInt32[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt32[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value[i]); + MessagePackBinary.WriteUInt32ForceUInt32Block(writer, value[i]); } - - return offset - startOffset; } } - public UInt32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt32[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new UInt32[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadUInt32(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadUInt32(ref byteSequence); } - readSize = offset - startOffset; return array; } } @@ -540,14 +501,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt64 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt64 value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value); + return MessagePackBinary.WriteUInt64ForceUInt64Block(writer, value); } - public UInt64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt64 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadUInt64(bytes, offset, out readSize); + return MessagePackBinary.ReadUInt64(ref byteSequence); } } @@ -559,28 +520,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt64? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt64? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value.Value); + MessagePackBinary.WriteUInt64ForceUInt64Block(writer, value.Value); } } - public UInt64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt64? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadUInt64(bytes, offset, out readSize); + return MessagePackBinary.ReadUInt64(ref byteSequence); } } } @@ -594,45 +555,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, UInt64[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt64[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value[i]); + MessagePackBinary.WriteUInt64ForceUInt64Block(writer, value[i]); } - - return offset - startOffset; } } - public UInt64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt64[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new UInt64[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadUInt64(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadUInt64(ref byteSequence); } - readSize = offset - startOffset; return array; } } @@ -646,14 +599,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Byte value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Byte value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, value); + return MessagePackBinary.WriteByteForceByteBlock(writer, value); } - public Byte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Byte Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadByte(bytes, offset, out readSize); + return MessagePackBinary.ReadByte(ref byteSequence); } } @@ -665,28 +618,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Byte? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Byte? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, value.Value); + MessagePackBinary.WriteByteForceByteBlock(writer, value.Value); } } - public Byte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Byte? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadByte(bytes, offset, out readSize); + return MessagePackBinary.ReadByte(ref byteSequence); } } } @@ -700,14 +653,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, SByte value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, SByte value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value); + return MessagePackBinary.WriteSByteForceSByteBlock(writer, value); } - public SByte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public SByte Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.ReadSByte(bytes, offset, out readSize); + return MessagePackBinary.ReadSByte(ref byteSequence); } } @@ -719,28 +672,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, SByte? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, SByte? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value.Value); + MessagePackBinary.WriteSByteForceSByteBlock(writer, value.Value); } } - public SByte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public SByte? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.ReadSByte(bytes, offset, out readSize); + return MessagePackBinary.ReadSByte(ref byteSequence); } } } @@ -754,45 +707,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, SByte[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, SByte[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value[i]); + MessagePackBinary.WriteSByteForceSByteBlock(writer, value[i]); } - - return offset - startOffset; } } - public SByte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public SByte[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new SByte[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.ReadSByte(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.ReadSByte(ref byteSequence); } - readSize = offset - startOffset; return array; } } diff --git a/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.tt b/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.tt index a8e54702..7cda9de7 100644 --- a/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.tt +++ b/src/MessagePack/Formatters/ForceSizePrimitiveFormatter.tt @@ -18,6 +18,7 @@ }; #> using System; +using System.Buffers; namespace MessagePack.Formatters { @@ -30,14 +31,14 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, <#= t.Name #> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, <#= t.Name #> value, IFormatterResolver formatterResolver) { - return MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(ref bytes, offset, value); + return MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(writer, value); } - public <#= t.Name #> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public <#= t.Name #> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - return MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize); + return MessagePackBinary.Read<#= t.Name #>(ref byteSequence); } } @@ -49,28 +50,28 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, <#= t.Name #>? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, <#= t.Name #>? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(ref bytes, offset, value.Value); + MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(writer, value.Value); } } - public <#= t.Name #>? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public <#= t.Name #>? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize); + return MessagePackBinary.Read<#= t.Name #>(ref byteSequence); } } } @@ -85,45 +86,37 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, <#= t.Name #>[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, <#= t.Name #>[] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); for (int i = 0; i < value.Length; i++) { - offset += MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(ref bytes, offset, value[i]); + MessagePackBinary.Write<#= t.Name #>Force<#= t.Name #>Block(writer, value[i]); } - - return offset - startOffset; } } - public <#= t.Name #>[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public <#= t.Name #>[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; - - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); var array = new <#= t.Name #>[len]; for (int i = 0; i < array.Length; i++) { - array[i] = MessagePackBinary.Read<#= t.Name #>(bytes, offset, out readSize); - offset += readSize; + array[i] = MessagePackBinary.Read<#= t.Name #>(ref byteSequence); } - readSize = offset - startOffset; return array; } } diff --git a/src/MessagePack/Formatters/IMessagePackFormatter.cs b/src/MessagePack/Formatters/IMessagePackFormatter.cs index cb1e37b0..fff8ae3e 100644 --- a/src/MessagePack/Formatters/IMessagePackFormatter.cs +++ b/src/MessagePack/Formatters/IMessagePackFormatter.cs @@ -1,9 +1,12 @@ -namespace MessagePack.Formatters +using System; +using System.Buffers; + +namespace MessagePack.Formatters { public interface IMessagePackFormatter<T> { - int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver); + void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver); - T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize); + T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver resolver); } } diff --git a/src/MessagePack/Formatters/IgnoreFormatter.cs b/src/MessagePack/Formatters/IgnoreFormatter.cs index cbf38bac..036ec86d 100644 --- a/src/MessagePack/Formatters/IgnoreFormatter.cs +++ b/src/MessagePack/Formatters/IgnoreFormatter.cs @@ -1,15 +1,17 @@ -namespace MessagePack.Formatters +using System.Buffers; + +namespace MessagePack.Formatters { public sealed class IgnoreFormatter<T> : IMessagePackFormatter<T> { - public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } - public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - readSize = MessagePackBinary.ReadNextBlock(bytes, offset); + MessagePackBinary.ReadNextBlock(ref byteSequence); return default(T); } } diff --git a/src/MessagePack/Formatters/MultiDimentionalArrayFormatter.cs b/src/MessagePack/Formatters/MultiDimentionalArrayFormatter.cs index 8e8ec0f1..42663f70 100644 --- a/src/MessagePack/Formatters/MultiDimentionalArrayFormatter.cs +++ b/src/MessagePack/Formatters/MultiDimentionalArrayFormatter.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Text; @@ -10,59 +11,49 @@ namespace MessagePack.Formatters { const int ArrayLength = 3; - public int Serialize(ref byte[] bytes, int offset, T[,] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T[,] value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { var i = value.GetLength(0); var j = value.GetLength(1); - var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify<T>(); - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, ArrayLength); - offset += MessagePackBinary.WriteInt32(ref bytes, offset, i); - offset += MessagePackBinary.WriteInt32(ref bytes, offset, j); + MessagePackBinary.WriteArrayHeader(writer, ArrayLength); + MessagePackBinary.WriteInt32(writer, i); + MessagePackBinary.WriteInt32(writer, j); - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length); + MessagePackBinary.WriteArrayHeader(writer, value.Length); foreach (var item in value) { - offset += formatter.Serialize(ref bytes, offset, item, formatterResolver); + formatter.Serialize(writer, item, formatterResolver); } - - return offset - startOffset; } } - public T[,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T[,] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify<T>(); - var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var len = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,] format"); - var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize); - offset += readSize; - - var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize); - offset += readSize; - - var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; - + var iLength = MessagePackBinary.ReadInt32(ref byteSequence); + var jLength = MessagePackBinary.ReadInt32(ref byteSequence); + var maxLen = MessagePackBinary.ReadArrayHeader(ref byteSequence); + var array = new T[iLength, jLength]; var i = 0; @@ -93,7 +84,7 @@ namespace MessagePack.Formatters { const int ArrayLength = 4; - public int Serialize(ref byte[] bytes, int offset, T[,,] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T[,,] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -123,7 +114,7 @@ namespace MessagePack.Formatters } } - public T[,,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T[,,] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -188,7 +179,7 @@ namespace MessagePack.Formatters { const int ArrayLength = 5; - public int Serialize(ref byte[] bytes, int offset, T[,,,] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T[,,,] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -220,7 +211,7 @@ namespace MessagePack.Formatters } } - public T[,,,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T[,,,] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { diff --git a/src/MessagePack/Formatters/NullableFormatter.cs b/src/MessagePack/Formatters/NullableFormatter.cs index 8d008856..d65d8a21 100644 --- a/src/MessagePack/Formatters/NullableFormatter.cs +++ b/src/MessagePack/Formatters/NullableFormatter.cs @@ -1,34 +1,32 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Buffers; namespace MessagePack.Formatters { public sealed class NullableFormatter<T> : IMessagePackFormatter<T?> where T : struct { - public int Serialize(ref byte[] bytes, int offset, T? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Value, formatterResolver); + formatterResolver.GetFormatterWithVerify<T>().Serialize(writer, value.Value, formatterResolver); } } - public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize); + return formatterResolver.GetFormatterWithVerify<T>().Deserialize(ref byteSequence, formatterResolver); } } } @@ -43,28 +41,28 @@ namespace MessagePack.Formatters this.underlyingFormatter = underlyingFormatter; } - public int Serialize(ref byte[] bytes, int offset, T? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T? value, IFormatterResolver formatterResolver) { if (value == null) { - return MessagePackBinary.WriteNil(ref bytes, offset); + MessagePackBinary.WriteNil(writer); } else { - return underlyingFormatter.Serialize(ref bytes, offset, value.Value, formatterResolver); + underlyingFormatter.Serialize(writer, value.Value, formatterResolver); } } - public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } else { - return underlyingFormatter.Deserialize(bytes, offset, formatterResolver, out readSize); + return underlyingFormatter.Deserialize(ref byteSequence, formatterResolver); } } } diff --git a/src/MessagePack/Formatters/OldSpecFormatter.cs b/src/MessagePack/Formatters/OldSpecFormatter.cs index 4531de6e..98273380 100644 --- a/src/MessagePack/Formatters/OldSpecFormatter.cs +++ b/src/MessagePack/Formatters/OldSpecFormatter.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; namespace MessagePack.Formatters { @@ -9,20 +10,20 @@ namespace MessagePack.Formatters { public static readonly NativeDateTimeFormatter Instance = new NativeDateTimeFormatter(); - public int Serialize(ref byte[] bytes, int offset, DateTime value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, DateTime value, IFormatterResolver formatterResolver) { var dateData = value.ToBinary(); - return MessagePackBinary.WriteInt64(ref bytes, offset, dateData); + MessagePackBinary.WriteInt64(writer, dateData); } - public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public DateTime Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension) + if (MessagePackBinary.GetMessagePackType(byteSequence) == MessagePackType.Extension) { - return DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize); + return DateTimeFormatter.Instance.Deserialize(ref byteSequence, formatterResolver); } - var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize); + var dateData = MessagePackBinary.ReadInt64(ref byteSequence); return DateTime.FromBinary(dateData); } } @@ -31,7 +32,7 @@ namespace MessagePack.Formatters { public static readonly NativeDateTimeArrayFormatter Instance = new NativeDateTimeArrayFormatter(); - public int Serialize(ref byte[] bytes, int offset, DateTime[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, DateTime[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -50,7 +51,7 @@ namespace MessagePack.Formatters } } - public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public DateTime[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -89,7 +90,7 @@ namespace MessagePack.Formatters public static readonly OldSpecStringFormatter Instance = new OldSpecStringFormatter(); // Old spec does not exists str 8 format. - public int Serialize(ref byte[] bytes, int offset, string value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, string value, IFormatterResolver formatterResolver) { if (value == null) return MessagePackBinary.WriteNil(ref bytes, offset); @@ -151,7 +152,7 @@ namespace MessagePack.Formatters } } - public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public string Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadString(bytes, offset, out readSize); } @@ -164,7 +165,7 @@ namespace MessagePack.Formatters { public static readonly OldSpecBinaryFormatter Instance = new OldSpecBinaryFormatter(); - public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, byte[] value, IFormatterResolver formatterResolver) { if (value == null) return MessagePackBinary.WriteNil(ref bytes, offset); @@ -202,7 +203,7 @@ namespace MessagePack.Formatters } } - public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public byte[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var type = MessagePackBinary.GetMessagePackType(bytes, offset); if (type == MessagePackType.Nil) diff --git a/src/MessagePack/Formatters/PrimitiveFormatter.cs b/src/MessagePack/Formatters/PrimitiveFormatter.cs index cd72e1fd..d8e2fd90 100644 --- a/src/MessagePack/Formatters/PrimitiveFormatter.cs +++ b/src/MessagePack/Formatters/PrimitiveFormatter.cs @@ -10,12 +10,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int16 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int16 value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteInt16(ref bytes, offset, value); } - public Int16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int16 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadInt16(bytes, offset, out readSize); } @@ -29,7 +29,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int16? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int16? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -41,7 +41,7 @@ namespace MessagePack.Formatters } } - public Int16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int16? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -64,7 +64,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Int16[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int16[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -83,7 +83,7 @@ namespace MessagePack.Formatters } } - public Int16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int16[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -116,12 +116,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int32 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int32 value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteInt32(ref bytes, offset, value); } - public Int32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int32 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadInt32(bytes, offset, out readSize); } @@ -135,7 +135,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int32? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int32? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -147,7 +147,7 @@ namespace MessagePack.Formatters } } - public Int32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int32? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -170,7 +170,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Int32[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int32[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -189,7 +189,7 @@ namespace MessagePack.Formatters } } - public Int32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int32[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -222,12 +222,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int64 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int64 value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteInt64(ref bytes, offset, value); } - public Int64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int64 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadInt64(bytes, offset, out readSize); } @@ -241,7 +241,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Int64? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int64? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -253,7 +253,7 @@ namespace MessagePack.Formatters } } - public Int64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int64? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -276,7 +276,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Int64[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Int64[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -295,7 +295,7 @@ namespace MessagePack.Formatters } } - public Int64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Int64[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -328,12 +328,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt16 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt16 value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteUInt16(ref bytes, offset, value); } - public UInt16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt16 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadUInt16(bytes, offset, out readSize); } @@ -347,7 +347,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt16? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt16? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -359,7 +359,7 @@ namespace MessagePack.Formatters } } - public UInt16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt16? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -382,7 +382,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, UInt16[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt16[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -401,7 +401,7 @@ namespace MessagePack.Formatters } } - public UInt16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt16[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -434,12 +434,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt32 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt32 value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteUInt32(ref bytes, offset, value); } - public UInt32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt32 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadUInt32(bytes, offset, out readSize); } @@ -453,7 +453,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt32? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt32? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -465,7 +465,7 @@ namespace MessagePack.Formatters } } - public UInt32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt32? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -488,7 +488,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, UInt32[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt32[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -507,7 +507,7 @@ namespace MessagePack.Formatters } } - public UInt32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt32[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -540,12 +540,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt64 value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt64 value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteUInt64(ref bytes, offset, value); } - public UInt64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt64 Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadUInt64(bytes, offset, out readSize); } @@ -559,7 +559,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, UInt64? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt64? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -571,7 +571,7 @@ namespace MessagePack.Formatters } } - public UInt64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt64? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -594,7 +594,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, UInt64[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, UInt64[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -613,7 +613,7 @@ namespace MessagePack.Formatters } } - public UInt64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public UInt64[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -646,12 +646,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Single value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Single value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteSingle(ref bytes, offset, value); } - public Single Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Single Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadSingle(bytes, offset, out readSize); } @@ -665,7 +665,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Single? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Single? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -677,7 +677,7 @@ namespace MessagePack.Formatters } } - public Single? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Single? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -700,7 +700,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Single[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Single[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -719,7 +719,7 @@ namespace MessagePack.Formatters } } - public Single[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Single[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -752,12 +752,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Double value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Double value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteDouble(ref bytes, offset, value); } - public Double Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Double Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadDouble(bytes, offset, out readSize); } @@ -771,7 +771,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Double? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Double? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -783,7 +783,7 @@ namespace MessagePack.Formatters } } - public Double? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Double? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -806,7 +806,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Double[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Double[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -825,7 +825,7 @@ namespace MessagePack.Formatters } } - public Double[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Double[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -858,12 +858,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Boolean value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Boolean value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteBoolean(ref bytes, offset, value); } - public Boolean Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Boolean Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadBoolean(bytes, offset, out readSize); } @@ -877,7 +877,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Boolean? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Boolean? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -889,7 +889,7 @@ namespace MessagePack.Formatters } } - public Boolean? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Boolean? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -912,7 +912,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Boolean[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Boolean[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -931,7 +931,7 @@ namespace MessagePack.Formatters } } - public Boolean[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Boolean[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -964,12 +964,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Byte value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Byte value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteByte(ref bytes, offset, value); } - public Byte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Byte Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadByte(bytes, offset, out readSize); } @@ -983,7 +983,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Byte? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Byte? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -995,7 +995,7 @@ namespace MessagePack.Formatters } } - public Byte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Byte? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -1018,12 +1018,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, SByte value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, SByte value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteSByte(ref bytes, offset, value); } - public SByte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public SByte Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadSByte(bytes, offset, out readSize); } @@ -1037,7 +1037,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, SByte? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, SByte? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -1049,7 +1049,7 @@ namespace MessagePack.Formatters } } - public SByte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public SByte? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -1072,7 +1072,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, SByte[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, SByte[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -1091,7 +1091,7 @@ namespace MessagePack.Formatters } } - public SByte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public SByte[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -1124,12 +1124,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Char value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Char value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteChar(ref bytes, offset, value); } - public Char Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Char Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadChar(bytes, offset, out readSize); } @@ -1143,7 +1143,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, Char? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Char? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -1155,7 +1155,7 @@ namespace MessagePack.Formatters } } - public Char? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Char? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -1178,7 +1178,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Char[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Char[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -1197,7 +1197,7 @@ namespace MessagePack.Formatters } } - public Char[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Char[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -1230,12 +1230,12 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, DateTime value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, DateTime value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteDateTime(ref bytes, offset, value); } - public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public DateTime Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadDateTime(bytes, offset, out readSize); } @@ -1249,7 +1249,7 @@ namespace MessagePack.Formatters { } - public int Serialize(ref byte[] bytes, int offset, DateTime? value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, DateTime? value, IFormatterResolver formatterResolver) { if (value == null) { @@ -1261,7 +1261,7 @@ namespace MessagePack.Formatters } } - public DateTime? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public DateTime? Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -1284,7 +1284,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, DateTime[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, DateTime[] value, IFormatterResolver formatterResolver) { if (value == null) { @@ -1303,7 +1303,7 @@ namespace MessagePack.Formatters } } - public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public DateTime[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { diff --git a/src/MessagePack/Formatters/PrimitiveObjectFormatter.cs b/src/MessagePack/Formatters/PrimitiveObjectFormatter.cs index dfeb1509..5a1ce5a3 100644 --- a/src/MessagePack/Formatters/PrimitiveObjectFormatter.cs +++ b/src/MessagePack/Formatters/PrimitiveObjectFormatter.cs @@ -48,7 +48,7 @@ namespace MessagePack.Formatters #endif - public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, object value, IFormatterResolver formatterResolver) { if (value == null) { @@ -156,7 +156,7 @@ namespace MessagePack.Formatters throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name); } - public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public object Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var type = MessagePackBinary.GetMessagePackType(bytes, offset); switch (type) diff --git a/src/MessagePack/Formatters/StandardClassLibraryFormatter.cs b/src/MessagePack/Formatters/StandardClassLibraryFormatter.cs index 3a0cbb96..0e4e7187 100644 --- a/src/MessagePack/Formatters/StandardClassLibraryFormatter.cs +++ b/src/MessagePack/Formatters/StandardClassLibraryFormatter.cs @@ -23,12 +23,12 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, byte[] value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteBytes(ref bytes, offset, value); } - public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public byte[] Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return MessagePackBinary.ReadBytes(bytes, offset, out readSize); } @@ -43,7 +43,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, String value, IFormatterResolver typeResolver) + public void Serialize(IBufferWriter<byte> writer, String value, IFormatterResolver typeResolver) { return MessagePackBinary.WriteString(ref bytes, offset, value); } @@ -63,7 +63,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, String[] value, IFormatterResolver typeResolver) + public void Serialize(IBufferWriter<byte> writer, String[] value, IFormatterResolver typeResolver) { if (value == null) { @@ -116,12 +116,12 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, decimal value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, decimal value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteString(ref bytes, offset, value.ToString(CultureInfo.InvariantCulture)); } - public decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public decimal Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return decimal.Parse(MessagePackBinary.ReadString(bytes, offset, out readSize), CultureInfo.InvariantCulture); } @@ -136,12 +136,12 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, TimeSpan value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, TimeSpan value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteInt64(ref bytes, offset, value.Ticks); } - public TimeSpan Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public TimeSpan Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return new TimeSpan(MessagePackBinary.ReadInt64(bytes, offset, out readSize)); } @@ -156,7 +156,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, DateTimeOffset value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, DateTimeOffset value, IFormatterResolver formatterResolver) { var startOffset = offset; offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2); @@ -165,7 +165,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public DateTimeOffset Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public DateTimeOffset Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var startOffset = offset; var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); @@ -195,7 +195,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Guid value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Guid value, IFormatterResolver formatterResolver) { MessagePackBinary.EnsureCapacity(ref bytes, offset, 38); @@ -205,7 +205,7 @@ namespace MessagePack.Formatters return 38; } - public Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Guid Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var segment = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize); return new GuidBits(segment).Value; @@ -222,7 +222,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Uri value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Uri value, IFormatterResolver formatterResolver) { if (value == null) { @@ -234,7 +234,7 @@ namespace MessagePack.Formatters } } - public Uri Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Uri Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -257,7 +257,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Version value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Version value, IFormatterResolver formatterResolver) { if (value == null) { @@ -269,7 +269,7 @@ namespace MessagePack.Formatters } } - public Version Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Version Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -285,7 +285,7 @@ namespace MessagePack.Formatters public sealed class KeyValuePairFormatter<TKey, TValue> : IMessagePackFormatter<KeyValuePair<TKey, TValue>> { - public int Serialize(ref byte[] bytes, int offset, KeyValuePair<TKey, TValue> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, KeyValuePair<TKey, TValue> value, IFormatterResolver formatterResolver) { var startOffset = offset; offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2); @@ -294,7 +294,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public KeyValuePair<TKey, TValue> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public KeyValuePair<TKey, TValue> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var startOffset = offset; var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); @@ -322,7 +322,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, StringBuilder value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, StringBuilder value, IFormatterResolver formatterResolver) { if (value == null) { @@ -334,7 +334,7 @@ namespace MessagePack.Formatters } } - public StringBuilder Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public StringBuilder Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -357,7 +357,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, BitArray value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, BitArray value, IFormatterResolver formatterResolver) { if (value == null) { @@ -377,7 +377,7 @@ namespace MessagePack.Formatters } } - public BitArray Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public BitArray Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -415,12 +415,12 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, System.Numerics.BigInteger value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, System.Numerics.BigInteger value, IFormatterResolver formatterResolver) { return MessagePackBinary.WriteBytes(ref bytes, offset, value.ToByteArray()); } - public System.Numerics.BigInteger Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public System.Numerics.BigInteger Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { return new System.Numerics.BigInteger(MessagePackBinary.ReadBytes(bytes, offset, out readSize)); } @@ -435,7 +435,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, System.Numerics.Complex value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, System.Numerics.Complex value, IFormatterResolver formatterResolver) { var startOffset = offset; offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2); @@ -444,7 +444,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public System.Numerics.Complex Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public System.Numerics.Complex Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var startOffset = offset; var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); @@ -465,7 +465,7 @@ namespace MessagePack.Formatters public sealed class LazyFormatter<T> : IMessagePackFormatter<Lazy<T>> { - public int Serialize(ref byte[] bytes, int offset, Lazy<T> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Lazy<T> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -477,7 +477,7 @@ namespace MessagePack.Formatters } } - public Lazy<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Lazy<T> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -503,7 +503,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Task value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Task value, IFormatterResolver formatterResolver) { if (value == null) { @@ -516,7 +516,7 @@ namespace MessagePack.Formatters } } - public Task Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Task Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (!MessagePackBinary.IsNil(bytes, offset)) { @@ -532,7 +532,7 @@ namespace MessagePack.Formatters public sealed class TaskValueFormatter<T> : IMessagePackFormatter<Task<T>> { - public int Serialize(ref byte[] bytes, int offset, Task<T> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Task<T> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -545,7 +545,7 @@ namespace MessagePack.Formatters } } - public Task<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Task<T> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -562,12 +562,12 @@ namespace MessagePack.Formatters public sealed class ValueTaskFormatter<T> : IMessagePackFormatter<ValueTask<T>> { - public int Serialize(ref byte[] bytes, int offset, ValueTask<T> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTask<T> value, IFormatterResolver formatterResolver) { return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Result, formatterResolver); } - public ValueTask<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTask<T> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize); return new ValueTask<T>(v); diff --git a/src/MessagePack/Formatters/TupleFormatter.cs b/src/MessagePack/Formatters/TupleFormatter.cs index 7348f54d..6e0bedb7 100644 --- a/src/MessagePack/Formatters/TupleFormatter.cs +++ b/src/MessagePack/Formatters/TupleFormatter.cs @@ -7,7 +7,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1> : IMessagePackFormatter<Tuple<T1>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -24,7 +24,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -50,7 +50,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2> : IMessagePackFormatter<Tuple<T1, T2>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -68,7 +68,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -96,7 +96,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2, T3> : IMessagePackFormatter<Tuple<T1, T2, T3>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2, T3> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -115,7 +115,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2, T3> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2, T3> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -145,7 +145,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2, T3, T4> : IMessagePackFormatter<Tuple<T1, T2, T3, T4>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2, T3, T4> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -165,7 +165,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2, T3, T4> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2, T3, T4> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -197,7 +197,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2, T3, T4, T5> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2, T3, T4, T5> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -218,7 +218,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2, T3, T4, T5> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2, T3, T4, T5> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -252,7 +252,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2, T3, T4, T5, T6> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5, T6>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5, T6> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2, T3, T4, T5, T6> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -274,7 +274,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2, T3, T4, T5, T6> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2, T3, T4, T5, T6> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -310,7 +310,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2, T3, T4, T5, T6, T7> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5, T6, T7>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5, T6, T7> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2, T3, T4, T5, T6, T7> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -333,7 +333,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { @@ -371,7 +371,7 @@ namespace MessagePack.Formatters public sealed class TupleFormatter<T1, T2, T3, T4, T5, T6, T7, TRest> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>> { - public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> value, IFormatterResolver formatterResolver) { if (value == null) { @@ -395,7 +395,7 @@ namespace MessagePack.Formatters } } - public Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { diff --git a/src/MessagePack/Formatters/TypelessFormatter.cs b/src/MessagePack/Formatters/TypelessFormatter.cs index b86b224f..a3816e2c 100644 --- a/src/MessagePack/Formatters/TypelessFormatter.cs +++ b/src/MessagePack/Formatters/TypelessFormatter.cs @@ -123,7 +123,7 @@ namespace MessagePack.Formatters } } - public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, object value, IFormatterResolver formatterResolver) { if (value == null) { @@ -207,7 +207,7 @@ namespace MessagePack.Formatters return offset - startOffset; } - public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public object Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (MessagePackBinary.IsNil(bytes, offset)) { diff --git a/src/MessagePack/Formatters/UnsafeBinaryFormatters.cs b/src/MessagePack/Formatters/UnsafeBinaryFormatters.cs index d28a99cb..2e5a4753 100644 --- a/src/MessagePack/Formatters/UnsafeBinaryFormatters.cs +++ b/src/MessagePack/Formatters/UnsafeBinaryFormatters.cs @@ -36,7 +36,7 @@ namespace MessagePack.Formatters return 18; } - public unsafe Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public unsafe Guid Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (!BitConverter.IsLittleEndian) throw new Exception("BinaryGuidFormatter only allows on little endian env."); @@ -95,7 +95,7 @@ namespace MessagePack.Formatters return 18; } - public unsafe Decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public unsafe Decimal Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (!BitConverter.IsLittleEndian) throw new Exception("BinaryDecimalFormatter only allows on little endian env."); diff --git a/src/MessagePack/Formatters/ValueTupleFormatter.cs b/src/MessagePack/Formatters/ValueTupleFormatter.cs index 87039785..c1905d07 100644 --- a/src/MessagePack/Formatters/ValueTupleFormatter.cs +++ b/src/MessagePack/Formatters/ValueTupleFormatter.cs @@ -1,38 +1,32 @@ #if NETSTANDARD || NETFRAMEWORK using System; +using System.Buffers; namespace MessagePack.Formatters { public sealed class ValueTupleFormatter<T1> : IMessagePackFormatter<ValueTuple<T1>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 1); + MessagePackBinary.WriteArrayHeader(writer, 1); - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - - return offset - startOffset; + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); } - public ValueTuple<T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 1) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1>(item1); } } @@ -41,36 +35,28 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2> : IMessagePackFormatter<ValueTuple<T1, T2>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2); - - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); + MessagePackBinary.WriteArrayHeader(writer, 2); - return offset - startOffset; + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); } - public ValueTuple<T1, T2> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 2) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2>(item1, item2); } } @@ -79,39 +65,30 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2, T3> : IMessagePackFormatter<ValueTuple<T1, T2, T3>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2, T3> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 3); + MessagePackBinary.WriteArrayHeader(writer, 3); - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver); - - return offset - startOffset; + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); + formatterResolver.GetFormatterWithVerify<T3>().Serialize(writer, value.Item3, formatterResolver); } - public ValueTuple<T1, T2, T3> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2, T3> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 3) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); + var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2, T3>(item1, item2, item3); } } @@ -120,42 +97,32 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2, T3, T4> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2, T3, T4> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 4); + MessagePackBinary.WriteArrayHeader(writer, 4); - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver); - - return offset - startOffset; + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); + formatterResolver.GetFormatterWithVerify<T3>().Serialize(writer, value.Item3, formatterResolver); + formatterResolver.GetFormatterWithVerify<T4>().Serialize(writer, value.Item4, formatterResolver); } - public ValueTuple<T1, T2, T3, T4> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2, T3, T4> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 4) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); + var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref byteSequence, formatterResolver); + var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2, T3, T4>(item1, item2, item3, item4); } } @@ -164,45 +131,34 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2, T3, T4, T5> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 5); - - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver); + MessagePackBinary.WriteArrayHeader(writer, 5); - return offset - startOffset; + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); + formatterResolver.GetFormatterWithVerify<T3>().Serialize(writer, value.Item3, formatterResolver); + formatterResolver.GetFormatterWithVerify<T4>().Serialize(writer, value.Item4, formatterResolver); + formatterResolver.GetFormatterWithVerify<T5>().Serialize(writer, value.Item5, formatterResolver); } - public ValueTuple<T1, T2, T3, T4, T5> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2, T3, T4, T5> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 5) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); + var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref byteSequence, formatterResolver); + var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(ref byteSequence, formatterResolver); + var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5); } } @@ -211,48 +167,36 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5, T6> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5, T6>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5, T6> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2, T3, T4, T5, T6> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 6); - - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver); - - return offset - startOffset; + MessagePackBinary.WriteArrayHeader(writer, 6); + + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); + formatterResolver.GetFormatterWithVerify<T3>().Serialize(writer, value.Item3, formatterResolver); + formatterResolver.GetFormatterWithVerify<T4>().Serialize(writer, value.Item4, formatterResolver); + formatterResolver.GetFormatterWithVerify<T5>().Serialize(writer, value.Item5, formatterResolver); + formatterResolver.GetFormatterWithVerify<T6>().Serialize(writer, value.Item6, formatterResolver); } - public ValueTuple<T1, T2, T3, T4, T5, T6> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2, T3, T4, T5, T6> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 6) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); + var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref byteSequence, formatterResolver); + var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(ref byteSequence, formatterResolver); + var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(ref byteSequence, formatterResolver); + var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6); } } @@ -261,51 +205,38 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5, T6, T7> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5, T6, T7> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2, T3, T4, T5, T6, T7> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 7); - - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T7>().Serialize(ref bytes, offset, value.Item7, formatterResolver); - - return offset - startOffset; + MessagePackBinary.WriteArrayHeader(writer, 7); + + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); + formatterResolver.GetFormatterWithVerify<T3>().Serialize(writer, value.Item3, formatterResolver); + formatterResolver.GetFormatterWithVerify<T4>().Serialize(writer, value.Item4, formatterResolver); + formatterResolver.GetFormatterWithVerify<T5>().Serialize(writer, value.Item5, formatterResolver); + formatterResolver.GetFormatterWithVerify<T6>().Serialize(writer, value.Item6, formatterResolver); + formatterResolver.GetFormatterWithVerify<T7>().Serialize(writer, value.Item7, formatterResolver); } - public ValueTuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 7) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); + var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref byteSequence, formatterResolver); + var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(ref byteSequence, formatterResolver); + var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(ref byteSequence, formatterResolver); + var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(ref byteSequence, formatterResolver); + var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7); } } @@ -314,54 +245,40 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5, T6, T7, TRest> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>> where TRest : struct { - public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 8); - - offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<T7>().Serialize(ref bytes, offset, value.Item7, formatterResolver); - offset += formatterResolver.GetFormatterWithVerify<TRest>().Serialize(ref bytes, offset, value.Rest, formatterResolver); - - return offset - startOffset; + MessagePackBinary.WriteArrayHeader(writer, 8); + + formatterResolver.GetFormatterWithVerify<T1>().Serialize(writer, value.Item1, formatterResolver); + formatterResolver.GetFormatterWithVerify<T2>().Serialize(writer, value.Item2, formatterResolver); + formatterResolver.GetFormatterWithVerify<T3>().Serialize(writer, value.Item3, formatterResolver); + formatterResolver.GetFormatterWithVerify<T4>().Serialize(writer, value.Item4, formatterResolver); + formatterResolver.GetFormatterWithVerify<T5>().Serialize(writer, value.Item5, formatterResolver); + formatterResolver.GetFormatterWithVerify<T6>().Serialize(writer, value.Item6, formatterResolver); + formatterResolver.GetFormatterWithVerify<T7>().Serialize(writer, value.Item7, formatterResolver); + formatterResolver.GetFormatterWithVerify<TRest>().Serialize(writer, value.Rest, formatterResolver); } - public ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != 8) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; - - var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; - var item8 = formatterResolver.GetFormatterWithVerify<TRest>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + + var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(ref byteSequence, formatterResolver); + var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(ref byteSequence, formatterResolver); + var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(ref byteSequence, formatterResolver); + var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(ref byteSequence, formatterResolver); + var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(ref byteSequence, formatterResolver); + var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(ref byteSequence, formatterResolver); + var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(ref byteSequence, formatterResolver); + var item8 = formatterResolver.GetFormatterWithVerify<TRest>().Deserialize(ref byteSequence, formatterResolver); - readSize = offset - startOffset; return new ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(item1, item2, item3, item4, item5, item6, item7, item8); } } diff --git a/src/MessagePack/Formatters/ValueTupleFormatter.tt b/src/MessagePack/Formatters/ValueTupleFormatter.tt index 8920d067..20905362 100644 --- a/src/MessagePack/Formatters/ValueTupleFormatter.tt +++ b/src/MessagePack/Formatters/ValueTupleFormatter.tt @@ -6,6 +6,7 @@ <#@ output extension=".cs" #> #if NETSTANDARD || NETFRAMEWORK using System; +using System.Buffers; namespace MessagePack.Formatters { @@ -18,37 +19,30 @@ namespace MessagePack.Formatters public sealed class ValueTupleFormatter<<#= ts #>> : IMessagePackFormatter<<#= t #>><#= (t.Contains("TRest") ? " where TRest : struct" : "") #> { - public int Serialize(ref byte[] bytes, int offset, <#= t #> value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, <#= t #> value, IFormatterResolver formatterResolver) { - var startOffset = offset; - offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, <#= i #>); + MessagePackBinary.WriteArrayHeader(writer, <#= i #>); <# for(var j = 1; j <= i; j++) { #> - offset += formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Serialize(ref bytes, offset, value.<#= toItem(j) #>, formatterResolver); + formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Serialize(writer, value.<#= toItem(j) #>, formatterResolver); <# } #> - - return offset - startOffset; } - public <#= t #> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public <#= t #> Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { - if (MessagePackBinary.IsNil(bytes, offset)) + if (MessagePackBinary.IsNil(byteSequence)) { throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); } else { - var startOffset = offset; - var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var count = MessagePackBinary.ReadArrayHeader(ref byteSequence); if (count != <#= i #>) throw new InvalidOperationException("Invalid ValueTuple count"); - offset += readSize; <# for(var j = 1; j <= i; j++) { #> - var item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(bytes, offset, formatterResolver, out readSize); - offset += readSize; + var item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(ref byteSequence, formatterResolver); <# } #> - readSize = offset - startOffset; return new ValueTuple<<#= ts #>>(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => "item" + x)) #>); } } diff --git a/src/MessagePack/Internal/UnsafeMemory.Low.cs b/src/MessagePack/Internal/UnsafeMemory.Low.cs deleted file mode 100644 index 1684038c..00000000 --- a/src/MessagePack/Internal/UnsafeMemory.Low.cs +++ /dev/null @@ -1,167 +0,0 @@ -#if NETSTANDARD || NETFRAMEWORK - -using System; -using System.Runtime.CompilerServices; - -namespace MessagePack.Internal -{ - // for string key property name write optimization. - - public static class UnsafeMemory - { - public static readonly bool Is32Bit = (IntPtr.Size == 4); - } - - public static partial class UnsafeMemory32 - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw1(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(byte*)pDst = *(byte*)pSrc; - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw2(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(short*)pDst = *(short*)pSrc; - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw3(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(byte*)pDst = *(byte*)pSrc; - *(short*)(pDst + 1) = *(short*)(pSrc + 1); - } - - return src.Length; - } - } - - public static partial class UnsafeMemory64 - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw1(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(byte*)pDst = *(byte*)pSrc; - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw2(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(short*)pDst = *(short*)pSrc; - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw3(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(byte*)pDst = *(byte*)pSrc; - *(short*)(pDst + 1) = *(short*)(pSrc + 1); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw4(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw5(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 1) = *(int*)(pSrc + 1); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw6(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 2) = *(int*)(pSrc + 2); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw7(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 3) = *(int*)(pSrc + 3); - } - - return src.Length; - } - } -} - -#endif
\ No newline at end of file diff --git a/src/MessagePack/Internal/UnsafeMemory.cs b/src/MessagePack/Internal/UnsafeMemory.cs deleted file mode 100644 index 4eac4665..00000000 --- a/src/MessagePack/Internal/UnsafeMemory.cs +++ /dev/null @@ -1,894 +0,0 @@ -#if NETSTANDARD || NETFRAMEWORK - -using System.Runtime.CompilerServices; - -namespace MessagePack.Internal -{ - public static partial class UnsafeMemory32 - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw4(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw5(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 1) = *(int*)(pSrc + 1); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw6(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 2) = *(int*)(pSrc + 2); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw7(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 3) = *(int*)(pSrc + 3); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw8(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw9(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 5) = *(int*)(pSrc + 5); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw10(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 6) = *(int*)(pSrc + 6); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw11(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 7) = *(int*)(pSrc + 7); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw12(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw13(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 9) = *(int*)(pSrc + 9); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw14(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 10) = *(int*)(pSrc + 10); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw15(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 11) = *(int*)(pSrc + 11); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw16(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw17(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 13) = *(int*)(pSrc + 13); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw18(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 14) = *(int*)(pSrc + 14); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw19(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 15) = *(int*)(pSrc + 15); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw20(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw21(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 17) = *(int*)(pSrc + 17); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw22(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 18) = *(int*)(pSrc + 18); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw23(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 19) = *(int*)(pSrc + 19); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw24(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw25(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 21) = *(int*)(pSrc + 21); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw26(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 22) = *(int*)(pSrc + 22); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw27(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 23) = *(int*)(pSrc + 23); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw28(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 24) = *(int*)(pSrc + 24); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw29(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 24) = *(int*)(pSrc + 24); - *(int*)(pDst + 25) = *(int*)(pSrc + 25); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw30(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 24) = *(int*)(pSrc + 24); - *(int*)(pDst + 26) = *(int*)(pSrc + 26); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw31(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(int*)(pDst + 0) = *(int*)(pSrc + 0); - *(int*)(pDst + 4) = *(int*)(pSrc + 4); - *(int*)(pDst + 8) = *(int*)(pSrc + 8); - *(int*)(pDst + 12) = *(int*)(pSrc + 12); - *(int*)(pDst + 16) = *(int*)(pSrc + 16); - *(int*)(pDst + 20) = *(int*)(pSrc + 20); - *(int*)(pDst + 24) = *(int*)(pSrc + 24); - *(int*)(pDst + 27) = *(int*)(pSrc + 27); - } - - return src.Length; - } - - } - - public static partial class UnsafeMemory64 - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw8(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw9(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 1) = *(long*)(pSrc + 1); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw10(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 2) = *(long*)(pSrc + 2); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw11(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 3) = *(long*)(pSrc + 3); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw12(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 4) = *(long*)(pSrc + 4); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw13(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 5) = *(long*)(pSrc + 5); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw14(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 6) = *(long*)(pSrc + 6); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw15(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 7) = *(long*)(pSrc + 7); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw16(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw17(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 9) = *(long*)(pSrc + 9); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw18(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 10) = *(long*)(pSrc + 10); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw19(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 11) = *(long*)(pSrc + 11); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw20(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 12) = *(long*)(pSrc + 12); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw21(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 13) = *(long*)(pSrc + 13); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw22(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 14) = *(long*)(pSrc + 14); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw23(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 15) = *(long*)(pSrc + 15); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw24(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw25(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 17) = *(long*)(pSrc + 17); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw26(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 18) = *(long*)(pSrc + 18); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw27(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 19) = *(long*)(pSrc + 19); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw28(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 20) = *(long*)(pSrc + 20); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw29(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 21) = *(long*)(pSrc + 21); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw30(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 22) = *(long*)(pSrc + 22); - } - - return src.Length; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw31(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { - *(long*)(pDst + 0) = *(long*)(pSrc + 0); - *(long*)(pDst + 8) = *(long*)(pSrc + 8); - *(long*)(pDst + 16) = *(long*)(pSrc + 16); - *(long*)(pDst + 23) = *(long*)(pSrc + 23); - } - - return src.Length; - } - - } -} - -#endif
\ No newline at end of file diff --git a/src/MessagePack/Internal/UnsafeMemory.tt b/src/MessagePack/Internal/UnsafeMemory.tt deleted file mode 100644 index dbf7797f..00000000 --- a/src/MessagePack/Internal/UnsafeMemory.tt +++ /dev/null @@ -1,67 +0,0 @@ -<#@ template debug="false" hostspecific="false" language="C#" #> -<#@ assembly name="System.Core" #> -<#@ import namespace="System.Linq" #> -<#@ import namespace="System.Text" #> -<#@ import namespace="System.Collections.Generic" #> -<#@ output extension=".cs" #> -<# - var Max = 31; -#> -#if NETSTANDARD - -using System.Runtime.CompilerServices; - -namespace MessagePack.Internal -{ - public static partial class UnsafeMemory32 - { -<# for(var i = 4; i <= Max; i++) { #> - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw<#= i #>(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { -<# for(var j = 0; j < (i / 4); j++) { #> - *(int*)(pDst + <#= (4 * j) #>) = *(int*)(pSrc + <#= (4 * j) #>); -<# } #> -<# if(i % 4 != 0) { #> - *(int*)(pDst + <#= i - 4 #>) = *(int*)(pSrc + <#= (i - 4) #>); -<# } #> - } - - return src.Length; - } - -<# } #> - } - - public static partial class UnsafeMemory64 - { -<# for(var i = 8; i <= Max; i++) { #> - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe int WriteRaw<#= i #>(ref byte[] dst, int dstOffset, byte[] src) - { - MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); - - fixed (byte* pSrc = &src[0]) - fixed (byte* pDst = &dst[dstOffset]) - { -<# for(var j = 0; j < (i / 8); j++) { #> - *(long*)(pDst + <#= (8 * j) #>) = *(long*)(pSrc + <#= (8 * j) #>); -<# } #> -<# if(i % 8 != 0) { #> - *(long*)(pDst + <#= i - 8 #>) = *(long*)(pSrc + <#= (i - 8) #>); -<# } #> - } - - return src.Length; - } - -<# } #> - } -} - -#endif
\ No newline at end of file diff --git a/src/MessagePack/LZ4/LZ4MessagePackSerializer.cs b/src/MessagePack/LZ4/LZ4MessagePackSerializer.cs index 5fcd28b7..2fd4cfd8 100644 --- a/src/MessagePack/LZ4/LZ4MessagePackSerializer.cs +++ b/src/MessagePack/LZ4/LZ4MessagePackSerializer.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.IO; using MessagePack.Internal; using MessagePack.LZ4; @@ -157,7 +158,7 @@ namespace MessagePack } } - public override T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver, out int readSize) => DeserializeCore<T>(bytes, resolver, out readSize); + public override T Deserialize<T>(ReadOnlySequence<byte> bytes, IFormatterResolver resolver, out int readSize) => DeserializeCore<T>(bytes, resolver, out readSize); public static byte[] Decode(Stream stream, bool readStrict = false) { diff --git a/src/MessagePack/MessagePack.csproj b/src/MessagePack/MessagePack.csproj index 604b742c..038be594 100644 --- a/src/MessagePack/MessagePack.csproj +++ b/src/MessagePack/MessagePack.csproj @@ -27,6 +27,7 @@ <PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.1" /> <PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" /> + <PackageReference Include="System.Memory" Version="4.5.1" /> </ItemGroup> <ItemGroup> @@ -79,4 +80,8 @@ <AutoGen>True</AutoGen> </Compile> </ItemGroup> + + <ItemGroup> + <Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" /> + </ItemGroup> </Project> diff --git a/src/MessagePack/MessagePackBinary.cs b/src/MessagePack/MessagePackBinary.cs index 2352f450..24f34d88 100644 --- a/src/MessagePack/MessagePackBinary.cs +++ b/src/MessagePack/MessagePackBinary.cs @@ -1,7 +1,9 @@ -using MessagePack.Decoders; -using MessagePack.Internal; -using System; +using System; +using System.Buffers; using System.IO; +using System.Runtime.InteropServices; +using MessagePack.Decoders; +using MessagePack.Internal; namespace MessagePack { @@ -11,30 +13,30 @@ namespace MessagePack /// </summary> public static partial class MessagePackBinary { - const int MaxSize = 256; // [0] ~ [255] - const int ArrayMaxSize = 0x7FFFFFC7; // https://msdn.microsoft.com/en-us/library/system.array - - static readonly IMapHeaderDecoder[] mapHeaderDecoders = new IMapHeaderDecoder[MaxSize]; - static readonly IArrayHeaderDecoder[] arrayHeaderDecoders = new IArrayHeaderDecoder[MaxSize]; - static readonly IBooleanDecoder[] booleanDecoders = new IBooleanDecoder[MaxSize]; - static readonly IByteDecoder[] byteDecoders = new IByteDecoder[MaxSize]; - static readonly IBytesDecoder[] bytesDecoders = new IBytesDecoder[MaxSize]; - static readonly IBytesSegmentDecoder[] bytesSegmentDecoders = new IBytesSegmentDecoder[MaxSize]; - static readonly ISByteDecoder[] sbyteDecoders = new ISByteDecoder[MaxSize]; - static readonly ISingleDecoder[] singleDecoders = new ISingleDecoder[MaxSize]; - static readonly IDoubleDecoder[] doubleDecoders = new IDoubleDecoder[MaxSize]; - static readonly IInt16Decoder[] int16Decoders = new IInt16Decoder[MaxSize]; - static readonly IInt32Decoder[] int32Decoders = new IInt32Decoder[MaxSize]; - static readonly IInt64Decoder[] int64Decoders = new IInt64Decoder[MaxSize]; - static readonly IUInt16Decoder[] uint16Decoders = new IUInt16Decoder[MaxSize]; - static readonly IUInt32Decoder[] uint32Decoders = new IUInt32Decoder[MaxSize]; - static readonly IUInt64Decoder[] uint64Decoders = new IUInt64Decoder[MaxSize]; - static readonly IStringDecoder[] stringDecoders = new IStringDecoder[MaxSize]; - static readonly IStringSegmentDecoder[] stringSegmentDecoders = new IStringSegmentDecoder[MaxSize]; - static readonly IExtDecoder[] extDecoders = new IExtDecoder[MaxSize]; - static readonly IExtHeaderDecoder[] extHeaderDecoders = new IExtHeaderDecoder[MaxSize]; - static readonly IDateTimeDecoder[] dateTimeDecoders = new IDateTimeDecoder[MaxSize]; - static readonly IReadNextDecoder[] readNextDecoders = new IReadNextDecoder[MaxSize]; + private const int MaxSize = 256; // [0] ~ [255] + private const int ArrayMaxSize = 0x7FFFFFC7; // https://msdn.microsoft.com/en-us/library/system.array + + private static readonly IMapHeaderDecoder[] mapHeaderDecoders = new IMapHeaderDecoder[MaxSize]; + private static readonly IArrayHeaderDecoder[] arrayHeaderDecoders = new IArrayHeaderDecoder[MaxSize]; + private static readonly IBooleanDecoder[] booleanDecoders = new IBooleanDecoder[MaxSize]; + private static readonly IByteDecoder[] byteDecoders = new IByteDecoder[MaxSize]; + private static readonly IBytesDecoder[] bytesDecoders = new IBytesDecoder[MaxSize]; + private static readonly IBytesSegmentDecoder[] bytesSegmentDecoders = new IBytesSegmentDecoder[MaxSize]; + private static readonly ISByteDecoder[] sbyteDecoders = new ISByteDecoder[MaxSize]; + private static readonly ISingleDecoder[] singleDecoders = new ISingleDecoder[MaxSize]; + private static readonly IDoubleDecoder[] doubleDecoders = new IDoubleDecoder[MaxSize]; + private static readonly IInt16Decoder[] int16Decoders = new IInt16Decoder[MaxSize]; + private static readonly IInt32Decoder[] int32Decoders = new IInt32Decoder[MaxSize]; + private static readonly IInt64Decoder[] int64Decoders = new IInt64Decoder[MaxSize]; + private static readonly IUInt16Decoder[] uint16Decoders = new IUInt16Decoder[MaxSize]; + private static readonly IUInt32Decoder[] uint32Decoders = new IUInt32Decoder[MaxSize]; + private static readonly IUInt64Decoder[] uint64Decoders = new IUInt64Decoder[MaxSize]; + private static readonly IStringDecoder[] stringDecoders = new IStringDecoder[MaxSize]; + private static readonly IStringSegmentDecoder[] stringSegmentDecoders = new IStringSegmentDecoder[MaxSize]; + private static readonly IExtDecoder[] extDecoders = new IExtDecoder[MaxSize]; + private static readonly IExtHeaderDecoder[] extHeaderDecoders = new IExtHeaderDecoder[MaxSize]; + private static readonly IDateTimeDecoder[] dateTimeDecoders = new IDateTimeDecoder[MaxSize]; + private static readonly IReadNextDecoder[] readNextDecoders = new IReadNextDecoder[MaxSize]; static MessagePackBinary() { @@ -251,115 +253,25 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static void EnsureCapacity(ref byte[] bytes, int offset, int appendLength) + public static MessagePackType GetMessagePackType(ReadOnlySequence<byte> byteSequence) { - var newLength = offset + appendLength; - - // If null(most case fisrt time) fill byte. - if (bytes == null) - { - bytes = new byte[newLength]; - return; - } - - // like MemoryStream.EnsureCapacity - var current = bytes.Length; - if (newLength > current) - { - int num = newLength; - if (num < 256) - { - num = 256; - FastResize(ref bytes, num); - return; - } - - if (current == ArrayMaxSize) - { - throw new InvalidOperationException("byte[] size reached maximum size of array(0x7FFFFFC7), can not write to single byte[]. Details: https://msdn.microsoft.com/en-us/library/system.array"); - } - - var newSize = unchecked((current * 2)); - if (newSize < 0) // overflow - { - num = ArrayMaxSize; - } - else - { - if (num < newSize) - { - num = newSize; - } - } - - FastResize(ref bytes, num); - } + return MessagePackCode.ToMessagePackType(byteSequence.First.Span[0]); } - // Buffer.BlockCopy version of Array.Resize #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static void FastResize(ref byte[] array, int newSize) + public static void ReadNext(ref ReadOnlySequence<byte> byteSequence) { - if (newSize < 0) throw new ArgumentOutOfRangeException("newSize"); - - byte[] array2 = array; - if (array2 == null) - { - array = new byte[newSize]; - return; - } - - if (array2.Length != newSize) - { - byte[] array3 = new byte[newSize]; - Buffer.BlockCopy(array2, 0, array3, 0, (array2.Length > newSize) ? newSize : array2.Length); - array = array3; - } - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static byte[] FastCloneWithResize(byte[] array, int newSize) - { - if (newSize < 0) throw new ArgumentOutOfRangeException("newSize"); - - byte[] array2 = array; - if (array2 == null) - { - array = new byte[newSize]; - return array; - } - - byte[] array3 = new byte[newSize]; - Buffer.BlockCopy(array2, 0, array3, 0, (array2.Length > newSize) ? newSize : array2.Length); - return array3; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static MessagePackType GetMessagePackType(byte[] bytes, int offset) - { - return MessagePackCode.ToMessagePackType(bytes[offset]); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int ReadNext(byte[] bytes, int offset) - { - return readNextDecoders[bytes[offset]].Read(bytes, offset); + readNextDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int ReadNextBlock(byte[] bytes, int offset) + public static void ReadNextBlock(ref ReadOnlySequence<byte> byteSequence) { - switch (GetMessagePackType(bytes, offset)) + switch (GetMessagePackType(byteSequence)) { case MessagePackType.Unknown: case MessagePackType.Integer: @@ -370,31 +282,28 @@ namespace MessagePack case MessagePackType.Binary: case MessagePackType.Extension: default: - return ReadNext(bytes, offset); + ReadNext(ref byteSequence); + break; case MessagePackType.Array: { - var startOffset = offset; - int readSize; - var header = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); - offset += readSize; + var header = MessagePackBinary.ReadArrayHeader(ref byteSequence); for (int i = 0; i < header; i++) { - offset += ReadNextBlock(bytes, offset); + ReadNextBlock(ref byteSequence); } - return offset - startOffset; + + break; } case MessagePackType.Map: { - var startOffset = offset; - int readSize; - var header = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize); - offset += readSize; + var header = MessagePackBinary.ReadMapHeader(ref byteSequence); for (int i = 0; i < header; i++) { - offset += ReadNextBlock(bytes, offset); // read key block - offset += ReadNextBlock(bytes, offset); // read value block + ReadNextBlock(ref byteSequence); // read key block + ReadNextBlock(ref byteSequence); // read value block } - return offset - startOffset; + + break; } } } @@ -402,251 +311,42 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteNil(ref byte[] bytes, int offset) + public static void WriteNil(IBufferWriter<byte> writer) { - EnsureCapacity(ref bytes, offset, 1); - - bytes[offset] = MessagePackCode.Nil; - return 1; + var span = writer.GetSpan(1); + span[0] = MessagePackCode.Nil; + writer.Advance(1); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static Nil ReadNil(byte[] bytes, int offset, out int readSize) + public static Nil ReadNil(ref ReadOnlySequence<byte> byteSequence) { - if (bytes[offset] == MessagePackCode.Nil) + if (byteSequence.First.Span[0] == MessagePackCode.Nil) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return Nil.Default; } else { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static bool IsNil(byte[] bytes, int offset) + public static bool IsNil(ReadOnlySequence<byte> byteSequence) { - return bytes[offset] == MessagePackCode.Nil; + return byteSequence.First.Span[0] == MessagePackCode.Nil; } - public static int WriteRaw(ref byte[] bytes, int offset, byte[] rawMessagePackBlock) + public static void WriteRaw(IBufferWriter<byte> writer, ReadOnlySpan<byte> rawMessagePackBlock) { - EnsureCapacity(ref bytes, offset, rawMessagePackBlock.Length); - -#if NETSTANDARD || NETFRAMEWORK - if (UnsafeMemory.Is32Bit) - { - switch (rawMessagePackBlock.Length) - { - case 1: - UnsafeMemory32.WriteRaw1(ref bytes, offset, rawMessagePackBlock); - break; - case 2: - UnsafeMemory32.WriteRaw2(ref bytes, offset, rawMessagePackBlock); - break; - case 3: - UnsafeMemory32.WriteRaw3(ref bytes, offset, rawMessagePackBlock); - break; - case 4: - UnsafeMemory32.WriteRaw4(ref bytes, offset, rawMessagePackBlock); - break; - case 5: - UnsafeMemory32.WriteRaw5(ref bytes, offset, rawMessagePackBlock); - break; - case 6: - UnsafeMemory32.WriteRaw6(ref bytes, offset, rawMessagePackBlock); - break; - case 7: - UnsafeMemory32.WriteRaw7(ref bytes, offset, rawMessagePackBlock); - break; - case 8: - UnsafeMemory32.WriteRaw8(ref bytes, offset, rawMessagePackBlock); - break; - case 9: - UnsafeMemory32.WriteRaw9(ref bytes, offset, rawMessagePackBlock); - break; - case 10: - UnsafeMemory32.WriteRaw10(ref bytes, offset, rawMessagePackBlock); - break; - case 11: - UnsafeMemory32.WriteRaw11(ref bytes, offset, rawMessagePackBlock); - break; - case 12: - UnsafeMemory32.WriteRaw12(ref bytes, offset, rawMessagePackBlock); - break; - case 13: - UnsafeMemory32.WriteRaw13(ref bytes, offset, rawMessagePackBlock); - break; - case 14: - UnsafeMemory32.WriteRaw14(ref bytes, offset, rawMessagePackBlock); - break; - case 15: - UnsafeMemory32.WriteRaw15(ref bytes, offset, rawMessagePackBlock); - break; - case 16: - UnsafeMemory32.WriteRaw16(ref bytes, offset, rawMessagePackBlock); - break; - case 17: - UnsafeMemory32.WriteRaw17(ref bytes, offset, rawMessagePackBlock); - break; - case 18: - UnsafeMemory32.WriteRaw18(ref bytes, offset, rawMessagePackBlock); - break; - case 19: - UnsafeMemory32.WriteRaw19(ref bytes, offset, rawMessagePackBlock); - break; - case 20: - UnsafeMemory32.WriteRaw20(ref bytes, offset, rawMessagePackBlock); - break; - case 21: - UnsafeMemory32.WriteRaw21(ref bytes, offset, rawMessagePackBlock); - break; - case 22: - UnsafeMemory32.WriteRaw22(ref bytes, offset, rawMessagePackBlock); - break; - case 23: - UnsafeMemory32.WriteRaw23(ref bytes, offset, rawMessagePackBlock); - break; - case 24: - UnsafeMemory32.WriteRaw24(ref bytes, offset, rawMessagePackBlock); - break; - case 25: - UnsafeMemory32.WriteRaw25(ref bytes, offset, rawMessagePackBlock); - break; - case 26: - UnsafeMemory32.WriteRaw26(ref bytes, offset, rawMessagePackBlock); - break; - case 27: - UnsafeMemory32.WriteRaw27(ref bytes, offset, rawMessagePackBlock); - break; - case 28: - UnsafeMemory32.WriteRaw28(ref bytes, offset, rawMessagePackBlock); - break; - case 29: - UnsafeMemory32.WriteRaw29(ref bytes, offset, rawMessagePackBlock); - break; - case 30: - UnsafeMemory32.WriteRaw30(ref bytes, offset, rawMessagePackBlock); - break; - case 31: - UnsafeMemory32.WriteRaw31(ref bytes, offset, rawMessagePackBlock); - break; - default: - Buffer.BlockCopy(rawMessagePackBlock, 0, bytes, offset, rawMessagePackBlock.Length); - break; - } - } - else - { - switch (rawMessagePackBlock.Length) - { - case 1: - UnsafeMemory64.WriteRaw1(ref bytes, offset, rawMessagePackBlock); - break; - case 2: - UnsafeMemory64.WriteRaw2(ref bytes, offset, rawMessagePackBlock); - break; - case 3: - UnsafeMemory64.WriteRaw3(ref bytes, offset, rawMessagePackBlock); - break; - case 4: - UnsafeMemory64.WriteRaw4(ref bytes, offset, rawMessagePackBlock); - break; - case 5: - UnsafeMemory64.WriteRaw5(ref bytes, offset, rawMessagePackBlock); - break; - case 6: - UnsafeMemory64.WriteRaw6(ref bytes, offset, rawMessagePackBlock); - break; - case 7: - UnsafeMemory64.WriteRaw7(ref bytes, offset, rawMessagePackBlock); - break; - case 8: - UnsafeMemory64.WriteRaw8(ref bytes, offset, rawMessagePackBlock); - break; - case 9: - UnsafeMemory64.WriteRaw9(ref bytes, offset, rawMessagePackBlock); - break; - case 10: - UnsafeMemory64.WriteRaw10(ref bytes, offset, rawMessagePackBlock); - break; - case 11: - UnsafeMemory64.WriteRaw11(ref bytes, offset, rawMessagePackBlock); - break; - case 12: - UnsafeMemory64.WriteRaw12(ref bytes, offset, rawMessagePackBlock); - break; - case 13: - UnsafeMemory64.WriteRaw13(ref bytes, offset, rawMessagePackBlock); - break; - case 14: - UnsafeMemory64.WriteRaw14(ref bytes, offset, rawMessagePackBlock); - break; - case 15: - UnsafeMemory64.WriteRaw15(ref bytes, offset, rawMessagePackBlock); - break; - case 16: - UnsafeMemory64.WriteRaw16(ref bytes, offset, rawMessagePackBlock); - break; - case 17: - UnsafeMemory64.WriteRaw17(ref bytes, offset, rawMessagePackBlock); - break; - case 18: - UnsafeMemory64.WriteRaw18(ref bytes, offset, rawMessagePackBlock); - break; - case 19: - UnsafeMemory64.WriteRaw19(ref bytes, offset, rawMessagePackBlock); - break; - case 20: - UnsafeMemory64.WriteRaw20(ref bytes, offset, rawMessagePackBlock); - break; - case 21: - UnsafeMemory64.WriteRaw21(ref bytes, offset, rawMessagePackBlock); - break; - case 22: - UnsafeMemory64.WriteRaw22(ref bytes, offset, rawMessagePackBlock); - break; - case 23: - UnsafeMemory64.WriteRaw23(ref bytes, offset, rawMessagePackBlock); - break; - case 24: - UnsafeMemory64.WriteRaw24(ref bytes, offset, rawMessagePackBlock); - break; - case 25: - UnsafeMemory64.WriteRaw25(ref bytes, offset, rawMessagePackBlock); - break; - case 26: - UnsafeMemory64.WriteRaw26(ref bytes, offset, rawMessagePackBlock); - break; - case 27: - UnsafeMemory64.WriteRaw27(ref bytes, offset, rawMessagePackBlock); - break; - case 28: - UnsafeMemory64.WriteRaw28(ref bytes, offset, rawMessagePackBlock); - break; - case 29: - UnsafeMemory64.WriteRaw29(ref bytes, offset, rawMessagePackBlock); - break; - case 30: - UnsafeMemory64.WriteRaw30(ref bytes, offset, rawMessagePackBlock); - break; - case 31: - UnsafeMemory64.WriteRaw31(ref bytes, offset, rawMessagePackBlock); - break; - default: - Buffer.BlockCopy(rawMessagePackBlock, 0, bytes, offset, rawMessagePackBlock.Length); - break; - } - } -#else - Buffer.BlockCopy(rawMessagePackBlock, 0, bytes, offset, rawMessagePackBlock.Length); -#endif - return rawMessagePackBlock.Length; + var span = writer.GetSpan(rawMessagePackBlock.Length); + rawMessagePackBlock.CopyTo(span); + writer.Advance(span.Length); } /// <summary> @@ -656,10 +356,10 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteFixedMapHeaderUnsafe(ref byte[] bytes, int offset, int count) + public static int WriteFixedMapHeaderUnsafe(IBufferWriter<byte> writer, int count) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = (byte)(MessagePackCode.MinFixMap | count); + var span = writer.GetSpan(1); + span[0] = (byte)(MessagePackCode.MinFixMap | count); return 1; } @@ -669,11 +369,11 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteMapHeader(ref byte[] bytes, int offset, int count) + public static void WriteMapHeader(IBufferWriter<byte> writer, int count) { checked { - return WriteMapHeader(ref bytes, offset, (uint)count); + WriteMapHeader(writer, (uint)count); } } @@ -683,37 +383,37 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteMapHeader(ref byte[] bytes, int offset, uint count) + public static void WriteMapHeader(IBufferWriter<byte> writer, uint count) { if (count <= MessagePackRange.MaxFixMapCount) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = (byte)(MessagePackCode.MinFixMap | count); - return 1; + var span = writer.GetSpan(1); + span[0] = (byte)(MessagePackCode.MinFixMap | count); + writer.Advance(1); } else if (count <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, 3); + var span = writer.GetSpan(3); unchecked { - bytes[offset] = MessagePackCode.Map16; - bytes[offset + 1] = (byte)(count >> 8); - bytes[offset + 2] = (byte)(count); + span[0] = MessagePackCode.Map16; + span[1] = (byte)(count >> 8); + span[2] = (byte)(count); } - return 3; + writer.Advance(3); } else { - EnsureCapacity(ref bytes, offset, 5); + var span = writer.GetSpan(5); unchecked { - bytes[offset] = MessagePackCode.Map32; - bytes[offset + 1] = (byte)(count >> 24); - bytes[offset + 2] = (byte)(count >> 16); - bytes[offset + 3] = (byte)(count >> 8); - bytes[offset + 4] = (byte)(count); + span[0] = MessagePackCode.Map32; + span[1] = (byte)(count >> 24); + span[2] = (byte)(count >> 16); + span[3] = (byte)(count >> 8); + span[4] = (byte)(count); } - return 5; + writer.Advance(5); } } @@ -723,18 +423,18 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteMapHeaderForceMap32Block(ref byte[] bytes, int offset, uint count) + public static void WriteMapHeaderForceMap32Block(IBufferWriter<byte> writer, uint count) { - EnsureCapacity(ref bytes, offset, 5); + var span = writer.GetSpan(5); unchecked { - bytes[offset] = MessagePackCode.Map32; - bytes[offset + 1] = (byte)(count >> 24); - bytes[offset + 2] = (byte)(count >> 16); - bytes[offset + 3] = (byte)(count >> 8); - bytes[offset + 4] = (byte)(count); + span[0] = MessagePackCode.Map32; + span[1] = (byte)(count >> 24); + span[2] = (byte)(count >> 16); + span[3] = (byte)(count >> 8); + span[4] = (byte)(count); } - return 5; + writer.Advance(5); } /// <summary> @@ -743,11 +443,11 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int ReadMapHeader(byte[] bytes, int offset, out int readSize) + public static int ReadMapHeader(ref ReadOnlySequence<byte> byteSequence) { checked { - return (int)mapHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return (int)mapHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } } @@ -757,9 +457,9 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static uint ReadMapHeaderRaw(byte[] bytes, int offset, out int readSize) + public static uint ReadMapHeaderRaw(ref ReadOnlySequence<byte> byteSequence) { - return mapHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return mapHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK @@ -788,11 +488,11 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteFixedArrayHeaderUnsafe(ref byte[] bytes, int offset, int count) + public static void WriteFixedArrayHeaderUnsafe(IBufferWriter<byte> writer, int count) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = (byte)(MessagePackCode.MinFixArray | count); - return 1; + var span = writer.GetSpan(1); + span[0] = (byte)(MessagePackCode.MinFixArray | count); + writer.Advance(1); } /// <summary> @@ -801,11 +501,11 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteArrayHeader(ref byte[] bytes, int offset, int count) + public static void WriteArrayHeader(IBufferWriter<byte> writer, int count) { checked { - return WriteArrayHeader(ref bytes, offset, (uint)count); + WriteArrayHeader(writer, (uint)count); } } @@ -815,37 +515,37 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteArrayHeader(ref byte[] bytes, int offset, uint count) + public static void WriteArrayHeader(IBufferWriter<byte> writer, uint count) { if (count <= MessagePackRange.MaxFixArrayCount) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = (byte)(MessagePackCode.MinFixArray | count); - return 1; + var span = writer.GetSpan(1); + span[0] = (byte)(MessagePackCode.MinFixArray | count); + writer.Advance(1); } else if (count <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, 3); + var span = writer.GetSpan(3); unchecked { - bytes[offset] = MessagePackCode.Array16; - bytes[offset + 1] = (byte)(count >> 8); - bytes[offset + 2] = (byte)(count); + span[0] = MessagePackCode.Array16; + span[1] = (byte)(count >> 8); + span[2] = (byte)(count); } - return 3; + writer.Advance(3); } else { - EnsureCapacity(ref bytes, offset, 5); + var span = writer.GetSpan(5); unchecked { - bytes[offset] = MessagePackCode.Array32; - bytes[offset + 1] = (byte)(count >> 24); - bytes[offset + 2] = (byte)(count >> 16); - bytes[offset + 3] = (byte)(count >> 8); - bytes[offset + 4] = (byte)(count); + span[0] = MessagePackCode.Array32; + span[1] = (byte)(count >> 24); + span[2] = (byte)(count >> 16); + span[3] = (byte)(count >> 8); + span[4] = (byte)(count); } - return 5; + writer.Advance(5); } } @@ -855,16 +555,16 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteArrayHeaderForceArray32Block(ref byte[] bytes, int offset, uint count) + public static int WriteArrayHeaderForceArray32Block(IBufferWriter<byte> writer, uint count) { - EnsureCapacity(ref bytes, offset, 5); + var span = writer.GetSpan(5); unchecked { - bytes[offset] = MessagePackCode.Array32; - bytes[offset + 1] = (byte)(count >> 24); - bytes[offset + 2] = (byte)(count >> 16); - bytes[offset + 3] = (byte)(count >> 8); - bytes[offset + 4] = (byte)(count); + span[0] = MessagePackCode.Array32; + span[1] = (byte)(count >> 24); + span[2] = (byte)(count >> 16); + span[3] = (byte)(count >> 8); + span[4] = (byte)(count); } return 5; } @@ -875,11 +575,11 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int ReadArrayHeader(byte[] bytes, int offset, out int readSize) + public static int ReadArrayHeader(ref ReadOnlySequence<byte> byteSequence) { checked { - return (int)arrayHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return (int)arrayHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } } @@ -889,47 +589,48 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static uint ReadArrayHeaderRaw(byte[] bytes, int offset, out int readSize) + public static uint ReadArrayHeaderRaw(ref ReadOnlySequence<byte> byteSequence) { - return arrayHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return arrayHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteBoolean(ref byte[] bytes, int offset, bool value) + public static int WriteBoolean(IBufferWriter<byte> writer, bool value) { - EnsureCapacity(ref bytes, offset, 1); + var span = writer.GetSpan(1); - bytes[offset] = (value ? MessagePackCode.True : MessagePackCode.False); + span[0] = (value ? MessagePackCode.True : MessagePackCode.False); return 1; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static bool ReadBoolean(byte[] bytes, int offset, out int readSize) + public static bool ReadBoolean(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return booleanDecoders[bytes[offset]].Read(); + bool result = booleanDecoders[byteSequence.First.Span[0]].Read(); + byteSequence = byteSequence.Slice(1); + return result; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteByte(ref byte[] bytes, int offset, byte value) + public static int WriteByte(IBufferWriter<byte> writer, byte value) { if (value <= MessagePackCode.MaxFixInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = value; + var span = writer.GetSpan(1); + span[0] = value; return 1; } else { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = value; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = value; return 2; } } @@ -937,124 +638,109 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteByteForceByteBlock(ref byte[] bytes, int offset, byte value) + public static int WriteByteForceByteBlock(IBufferWriter<byte> writer, byte value) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = value; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = value; return 2; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static byte ReadByte(byte[] bytes, int offset, out int readSize) - { - return byteDecoders[bytes[offset]].Read(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteBytes(ref byte[] bytes, int offset, byte[] value) + public static byte ReadByte(ref ReadOnlySequence<byte> byteSequence) { - if (value == null) - { - return WriteNil(ref bytes, offset); - } - else - { - return WriteBytes(ref bytes, offset, value, 0, value.Length); - } + return byteDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteBytes(ref byte[] dest, int dstOffset, byte[] src, int srcOffset, int count) + public static void WriteBytes(IBufferWriter<byte> writer, ReadOnlySpan<byte> src) { if (src == null) { - return WriteNil(ref dest, dstOffset); + WriteNil(writer); } - if (count <= byte.MaxValue) + if (src.Length <= byte.MaxValue) { - var size = count + 2; - EnsureCapacity(ref dest, dstOffset, size); + var size = src.Length + 2; + var span = writer.GetSpan(size); - dest[dstOffset] = MessagePackCode.Bin8; - dest[dstOffset + 1] = (byte)count; + span[0] = MessagePackCode.Bin8; + span[1] = (byte)src.Length; - Buffer.BlockCopy(src, srcOffset, dest, dstOffset + 2, count); - return size; + src.CopyTo(span.Slice(2)); + writer.Advance(size); } - else if (count <= UInt16.MaxValue) + else if (src.Length <= UInt16.MaxValue) { - var size = count + 3; - EnsureCapacity(ref dest, dstOffset, size); + var size = src.Length + 3; + var span = writer.GetSpan(size); unchecked { - dest[dstOffset] = MessagePackCode.Bin16; - dest[dstOffset + 1] = (byte)(count >> 8); - dest[dstOffset + 2] = (byte)(count); + span[0] = MessagePackCode.Bin16; + span[1] = (byte)(src.Length >> 8); + span[2] = (byte)(src.Length); } - Buffer.BlockCopy(src, srcOffset, dest, dstOffset + 3, count); - return size; + src.CopyTo(span.Slice(3)); + writer.Advance(size); } else { - var size = count + 5; - EnsureCapacity(ref dest, dstOffset, size); + var size = src.Length + 5; + var span = writer.GetSpan(size); unchecked { - dest[dstOffset] = MessagePackCode.Bin32; - dest[dstOffset + 1] = (byte)(count >> 24); - dest[dstOffset + 2] = (byte)(count >> 16); - dest[dstOffset + 3] = (byte)(count >> 8); - dest[dstOffset + 4] = (byte)(count); + span[0] = MessagePackCode.Bin32; + span[1] = (byte)(src.Length >> 24); + span[2] = (byte)(src.Length >> 16); + span[3] = (byte)(src.Length >> 8); + span[4] = (byte)(src.Length); } - Buffer.BlockCopy(src, srcOffset, dest, dstOffset + 5, count); - return size; + src.CopyTo(span.Slice(5)); + writer.Advance(size); } } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static byte[] ReadBytes(byte[] bytes, int offset, out int readSize) + public static byte[] ReadBytes(ref ReadOnlySequence<byte> byteSequence) { - return bytesDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return bytesDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static ArraySegment<byte> ReadBytesSegment(byte[] bytes, int offset, out int readSize) + public static ArraySegment<byte> ReadBytesSegment(ref ReadOnlySequence<byte> byteSequence) { - return bytesSegmentDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return bytesSegmentDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteSByte(ref byte[] bytes, int offset, sbyte value) + public static int WriteSByte(IBufferWriter<byte> writer, sbyte value) { if (value < MessagePackRange.MinFixNegativeInt) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.Int8; - bytes[offset + 1] = unchecked((byte)(value)); + var span = writer.GetSpan(2); + span[0] = MessagePackCode.Int8; + span[1] = unchecked((byte)(value)); return 2; } else { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); return 1; } } @@ -1062,45 +748,45 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteSByteForceSByteBlock(ref byte[] bytes, int offset, sbyte value) + public static int WriteSByteForceSByteBlock(IBufferWriter<byte> writer, sbyte value) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.Int8; - bytes[offset + 1] = unchecked((byte)(value)); + var span = writer.GetSpan(2); + span[0] = MessagePackCode.Int8; + span[1] = unchecked((byte)(value)); return 2; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static sbyte ReadSByte(byte[] bytes, int offset, out int readSize) + public static sbyte ReadSByte(ref ReadOnlySequence<byte> byteSequence) { - return sbyteDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return sbyteDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteSingle(ref byte[] bytes, int offset, float value) + public static int WriteSingle(IBufferWriter<byte> writer, float value) { - EnsureCapacity(ref bytes, offset, 5); + var span = writer.GetSpan(5); - bytes[offset] = MessagePackCode.Float32; + span[0] = MessagePackCode.Float32; var num = new Float32Bits(value); if (BitConverter.IsLittleEndian) { - bytes[offset + 1] = num.Byte3; - bytes[offset + 2] = num.Byte2; - bytes[offset + 3] = num.Byte1; - bytes[offset + 4] = num.Byte0; + span[1] = num.Byte3; + span[2] = num.Byte2; + span[3] = num.Byte1; + span[4] = num.Byte0; } else { - bytes[offset + 1] = num.Byte0; - bytes[offset + 2] = num.Byte1; - bytes[offset + 3] = num.Byte2; - bytes[offset + 4] = num.Byte3; + span[1] = num.Byte0; + span[2] = num.Byte1; + span[3] = num.Byte2; + span[4] = num.Byte3; } return 5; @@ -1109,42 +795,42 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static float ReadSingle(byte[] bytes, int offset, out int readSize) + public static float ReadSingle(ref ReadOnlySequence<byte> byteSequence) { - return singleDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return singleDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteDouble(ref byte[] bytes, int offset, double value) + public static int WriteDouble(IBufferWriter<byte> writer, double value) { - EnsureCapacity(ref bytes, offset, 9); + var span = writer.GetSpan(9); - bytes[offset] = MessagePackCode.Float64; + span[0] = MessagePackCode.Float64; var num = new Float64Bits(value); if (BitConverter.IsLittleEndian) { - bytes[offset + 1] = num.Byte7; - bytes[offset + 2] = num.Byte6; - bytes[offset + 3] = num.Byte5; - bytes[offset + 4] = num.Byte4; - bytes[offset + 5] = num.Byte3; - bytes[offset + 6] = num.Byte2; - bytes[offset + 7] = num.Byte1; - bytes[offset + 8] = num.Byte0; + span[1] = num.Byte7; + span[2] = num.Byte6; + span[3] = num.Byte5; + span[4] = num.Byte4; + span[5] = num.Byte3; + span[6] = num.Byte2; + span[7] = num.Byte1; + span[8] = num.Byte0; } else { - bytes[offset + 1] = num.Byte0; - bytes[offset + 2] = num.Byte1; - bytes[offset + 3] = num.Byte2; - bytes[offset + 4] = num.Byte3; - bytes[offset + 5] = num.Byte4; - bytes[offset + 6] = num.Byte5; - bytes[offset + 7] = num.Byte6; - bytes[offset + 8] = num.Byte7; + span[1] = num.Byte0; + span[2] = num.Byte1; + span[3] = num.Byte2; + span[4] = num.Byte3; + span[5] = num.Byte4; + span[6] = num.Byte5; + span[7] = num.Byte6; + span[8] = num.Byte7; } return 9; @@ -1153,38 +839,38 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static double ReadDouble(byte[] bytes, int offset, out int readSize) + public static double ReadDouble(ref ReadOnlySequence<byte> byteSequence) { - return doubleDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return doubleDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteInt16(ref byte[] bytes, int offset, short value) + public static int WriteInt16(IBufferWriter<byte> writer, short value) { if (value >= 0) { // positive int(use uint) if (value <= MessagePackRange.MaxFixPositiveInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); return 1; } else if (value <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = unchecked((byte)value); + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = unchecked((byte)value); return 2; } else { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); return 3; } } @@ -1193,23 +879,23 @@ namespace MessagePack // negative int(use int) if (MessagePackRange.MinFixNegativeInt <= value) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); return 1; } else if (sbyte.MinValue <= value) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.Int8; - bytes[offset + 1] = unchecked((byte)value); + var span = writer.GetSpan(2); + span[0] = MessagePackCode.Int8; + span[1] = unchecked((byte)value); return 2; } else { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.Int16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); + var span = writer.GetSpan(3); + span[0] = MessagePackCode.Int16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); return 3; } } @@ -1218,21 +904,21 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteInt16ForceInt16Block(ref byte[] bytes, int offset, short value) + public static int WriteInt16ForceInt16Block(IBufferWriter<byte> writer, short value) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.Int16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); + var span = writer.GetSpan(3); + span[0] = MessagePackCode.Int16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); return 3; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static short ReadInt16(byte[] bytes, int offset, out int readSize) + public static short ReadInt16(ref ReadOnlySequence<byte> byteSequence) { - return int16Decoders[bytes[offset]].Read(bytes, offset, out readSize); + return int16Decoders[byteSequence.First.Span[0]].Read(ref byteSequence); } /// <summary> @@ -1241,51 +927,51 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WritePositiveFixedIntUnsafe(ref byte[] bytes, int offset, int value) + public static int WritePositiveFixedIntUnsafe(IBufferWriter<byte> writer, int value) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = (byte)value; + var span = writer.GetSpan(1); + span[0] = (byte)value; return 1; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteInt32(ref byte[] bytes, int offset, int value) + public static void WriteInt32(IBufferWriter<byte> writer, int value) { if (value >= 0) { // positive int(use uint) if (value <= MessagePackRange.MaxFixPositiveInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); - return 1; + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); + writer.Advance(1); } else if (value <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = unchecked((byte)value); - return 2; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = unchecked((byte)value); + writer.Advance(2); } else if (value <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } else { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.UInt32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); - return 5; + var span = writer.GetSpan(5); + span[0] = MessagePackCode.UInt32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); + writer.Advance(5); } } else @@ -1293,34 +979,34 @@ namespace MessagePack // negative int(use int) if (MessagePackRange.MinFixNegativeInt <= value) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); - return 1; + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); + writer.Advance(1); } else if (sbyte.MinValue <= value) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.Int8; - bytes[offset + 1] = unchecked((byte)value); - return 2; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.Int8; + span[1] = unchecked((byte)value); + writer.Advance(2); } else if (short.MinValue <= value) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.Int16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.Int16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } else { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.Int32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); - return 5; + var span = writer.GetSpan(5); + span[0] = MessagePackCode.Int32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); + writer.Advance(5); } } } @@ -1331,77 +1017,77 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteInt32ForceInt32Block(ref byte[] bytes, int offset, int value) - { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.Int32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); + public static int WriteInt32ForceInt32Block(IBufferWriter<byte> writer, int value) + { + var span = writer.GetSpan(5); + span[0] = MessagePackCode.Int32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); return 5; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int ReadInt32(byte[] bytes, int offset, out int readSize) + public static int ReadInt32(ref ReadOnlySequence<byte> byteSequence) { - return int32Decoders[bytes[offset]].Read(bytes, offset, out readSize); + return int32Decoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteInt64(ref byte[] bytes, int offset, long value) + public static void WriteInt64(IBufferWriter<byte> writer, long value) { if (value >= 0) { // positive int(use uint) if (value <= MessagePackRange.MaxFixPositiveInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); - return 1; + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); + writer.Advance(1); } else if (value <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = unchecked((byte)value); - return 2; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = unchecked((byte)value); + writer.Advance(2); } else if (value <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } else if (value <= uint.MaxValue) { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.UInt32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); - return 5; + var span = writer.GetSpan(5); + span[0] = MessagePackCode.UInt32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); + writer.Advance(5); } else { - EnsureCapacity(ref bytes, offset, 9); - bytes[offset] = MessagePackCode.UInt64; - bytes[offset + 1] = unchecked((byte)(value >> 56)); - bytes[offset + 2] = unchecked((byte)(value >> 48)); - bytes[offset + 3] = unchecked((byte)(value >> 40)); - bytes[offset + 4] = unchecked((byte)(value >> 32)); - bytes[offset + 5] = unchecked((byte)(value >> 24)); - bytes[offset + 6] = unchecked((byte)(value >> 16)); - bytes[offset + 7] = unchecked((byte)(value >> 8)); - bytes[offset + 8] = unchecked((byte)value); - return 9; + var span = writer.GetSpan(9); + span[0] = MessagePackCode.UInt64; + span[1] = unchecked((byte)(value >> 56)); + span[2] = unchecked((byte)(value >> 48)); + span[3] = unchecked((byte)(value >> 40)); + span[4] = unchecked((byte)(value >> 32)); + span[5] = unchecked((byte)(value >> 24)); + span[6] = unchecked((byte)(value >> 16)); + span[7] = unchecked((byte)(value >> 8)); + span[8] = unchecked((byte)value); + writer.Advance(9); } } else @@ -1409,48 +1095,48 @@ namespace MessagePack // negative int(use int) if (MessagePackRange.MinFixNegativeInt <= value) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); - return 1; + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); + writer.Advance(1); } else if (sbyte.MinValue <= value) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.Int8; - bytes[offset + 1] = unchecked((byte)value); - return 2; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.Int8; + span[1] = unchecked((byte)value); + writer.Advance(2); } else if (short.MinValue <= value) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.Int16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.Int16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } else if (int.MinValue <= value) { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.Int32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); - return 5; + var span = writer.GetSpan(5); + span[0] = MessagePackCode.Int32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); + writer.Advance(5); } else { - EnsureCapacity(ref bytes, offset, 9); - bytes[offset] = MessagePackCode.Int64; - bytes[offset + 1] = unchecked((byte)(value >> 56)); - bytes[offset + 2] = unchecked((byte)(value >> 48)); - bytes[offset + 3] = unchecked((byte)(value >> 40)); - bytes[offset + 4] = unchecked((byte)(value >> 32)); - bytes[offset + 5] = unchecked((byte)(value >> 24)); - bytes[offset + 6] = unchecked((byte)(value >> 16)); - bytes[offset + 7] = unchecked((byte)(value >> 8)); - bytes[offset + 8] = unchecked((byte)value); - return 9; + var span = writer.GetSpan(9); + span[0] = MessagePackCode.Int64; + span[1] = unchecked((byte)(value >> 56)); + span[2] = unchecked((byte)(value >> 48)); + span[3] = unchecked((byte)(value >> 40)); + span[4] = unchecked((byte)(value >> 32)); + span[5] = unchecked((byte)(value >> 24)); + span[6] = unchecked((byte)(value >> 16)); + span[7] = unchecked((byte)(value >> 8)); + span[8] = unchecked((byte)value); + writer.Advance(9); } } } @@ -1458,111 +1144,111 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteInt64ForceInt64Block(ref byte[] bytes, int offset, long value) - { - EnsureCapacity(ref bytes, offset, 9); - bytes[offset] = MessagePackCode.Int64; - bytes[offset + 1] = unchecked((byte)(value >> 56)); - bytes[offset + 2] = unchecked((byte)(value >> 48)); - bytes[offset + 3] = unchecked((byte)(value >> 40)); - bytes[offset + 4] = unchecked((byte)(value >> 32)); - bytes[offset + 5] = unchecked((byte)(value >> 24)); - bytes[offset + 6] = unchecked((byte)(value >> 16)); - bytes[offset + 7] = unchecked((byte)(value >> 8)); - bytes[offset + 8] = unchecked((byte)value); - return 9; + public static void WriteInt64ForceInt64Block(IBufferWriter<byte> writer, long value) + { + var span = writer.GetSpan(9); + span[0] = MessagePackCode.Int64; + span[1] = unchecked((byte)(value >> 56)); + span[2] = unchecked((byte)(value >> 48)); + span[3] = unchecked((byte)(value >> 40)); + span[4] = unchecked((byte)(value >> 32)); + span[5] = unchecked((byte)(value >> 24)); + span[6] = unchecked((byte)(value >> 16)); + span[7] = unchecked((byte)(value >> 8)); + span[8] = unchecked((byte)value); + writer.Advance(9); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static long ReadInt64(byte[] bytes, int offset, out int readSize) + public static long ReadInt64(ref ReadOnlySequence<byte> byteSequence) { - return int64Decoders[bytes[offset]].Read(bytes, offset, out readSize); + return int64Decoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteUInt16(ref byte[] bytes, int offset, ushort value) + public static void WriteUInt16(IBufferWriter<byte> writer, ushort value) { if (value <= MessagePackRange.MaxFixPositiveInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); - return 1; + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); + writer.Advance(1); } else if (value <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = unchecked((byte)value); - return 2; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = unchecked((byte)value); + writer.Advance(2); } else { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteUInt16ForceUInt16Block(ref byte[] bytes, int offset, ushort value) + public static void WriteUInt16ForceUInt16Block(IBufferWriter<byte> writer, ushort value) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static ushort ReadUInt16(byte[] bytes, int offset, out int readSize) + public static ushort ReadUInt16(ref ReadOnlySequence<byte> byteSequence) { - return uint16Decoders[bytes[offset]].Read(bytes, offset, out readSize); + return uint16Decoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteUInt32(ref byte[] bytes, int offset, uint value) + public static int WriteUInt32(IBufferWriter<byte> writer, uint value) { if (value <= MessagePackRange.MaxFixPositiveInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); return 1; } else if (value <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = unchecked((byte)value); + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = unchecked((byte)value); return 2; } else if (value <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); return 3; } else { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.UInt32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); + var span = writer.GetSpan(5); + span[0] = MessagePackCode.UInt32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); return 5; } } @@ -1570,132 +1256,130 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteUInt32ForceUInt32Block(ref byte[] bytes, int offset, uint value) - { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.UInt32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); + public static int WriteUInt32ForceUInt32Block(IBufferWriter<byte> writer, uint value) + { + var span = writer.GetSpan(5); + span[0] = MessagePackCode.UInt32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); return 5; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static uint ReadUInt32(byte[] bytes, int offset, out int readSize) + public static uint ReadUInt32(ref ReadOnlySequence<byte> byteSequence) { - return uint32Decoders[bytes[offset]].Read(bytes, offset, out readSize); + return uint32Decoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteUInt64(ref byte[] bytes, int offset, ulong value) + public static void WriteUInt64(IBufferWriter<byte> writer, ulong value) { if (value <= MessagePackRange.MaxFixPositiveInt) { - EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = unchecked((byte)value); - return 1; + var span = writer.GetSpan(1); + span[0] = unchecked((byte)value); + writer.Advance(1); } else if (value <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, 2); - bytes[offset] = MessagePackCode.UInt8; - bytes[offset + 1] = unchecked((byte)value); - return 2; + var span = writer.GetSpan(2); + span[0] = MessagePackCode.UInt8; + span[1] = unchecked((byte)value); + writer.Advance(2); } else if (value <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.UInt16; - bytes[offset + 1] = unchecked((byte)(value >> 8)); - bytes[offset + 2] = unchecked((byte)value); - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.UInt16; + span[1] = unchecked((byte)(value >> 8)); + span[2] = unchecked((byte)value); + writer.Advance(3); } else if (value <= uint.MaxValue) { - EnsureCapacity(ref bytes, offset, 5); - bytes[offset] = MessagePackCode.UInt32; - bytes[offset + 1] = unchecked((byte)(value >> 24)); - bytes[offset + 2] = unchecked((byte)(value >> 16)); - bytes[offset + 3] = unchecked((byte)(value >> 8)); - bytes[offset + 4] = unchecked((byte)value); - return 5; + var span = writer.GetSpan(5); + span[0] = MessagePackCode.UInt32; + span[1] = unchecked((byte)(value >> 24)); + span[2] = unchecked((byte)(value >> 16)); + span[3] = unchecked((byte)(value >> 8)); + span[4] = unchecked((byte)value); + writer.Advance(5); } else { - EnsureCapacity(ref bytes, offset, 9); - bytes[offset] = MessagePackCode.UInt64; - bytes[offset + 1] = unchecked((byte)(value >> 56)); - bytes[offset + 2] = unchecked((byte)(value >> 48)); - bytes[offset + 3] = unchecked((byte)(value >> 40)); - bytes[offset + 4] = unchecked((byte)(value >> 32)); - bytes[offset + 5] = unchecked((byte)(value >> 24)); - bytes[offset + 6] = unchecked((byte)(value >> 16)); - bytes[offset + 7] = unchecked((byte)(value >> 8)); - bytes[offset + 8] = unchecked((byte)value); - return 9; + var span = writer.GetSpan(9); + span[0] = MessagePackCode.UInt64; + span[1] = unchecked((byte)(value >> 56)); + span[2] = unchecked((byte)(value >> 48)); + span[3] = unchecked((byte)(value >> 40)); + span[4] = unchecked((byte)(value >> 32)); + span[5] = unchecked((byte)(value >> 24)); + span[6] = unchecked((byte)(value >> 16)); + span[7] = unchecked((byte)(value >> 8)); + span[8] = unchecked((byte)value); + writer.Advance(9); } } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteUInt64ForceUInt64Block(ref byte[] bytes, int offset, ulong value) - { - EnsureCapacity(ref bytes, offset, 9); - bytes[offset] = MessagePackCode.UInt64; - bytes[offset + 1] = unchecked((byte)(value >> 56)); - bytes[offset + 2] = unchecked((byte)(value >> 48)); - bytes[offset + 3] = unchecked((byte)(value >> 40)); - bytes[offset + 4] = unchecked((byte)(value >> 32)); - bytes[offset + 5] = unchecked((byte)(value >> 24)); - bytes[offset + 6] = unchecked((byte)(value >> 16)); - bytes[offset + 7] = unchecked((byte)(value >> 8)); - bytes[offset + 8] = unchecked((byte)value); - return 9; + public static void WriteUInt64ForceUInt64Block(IBufferWriter<byte> writer, ulong value) + { + var span = writer.GetSpan(9); + span[0] = MessagePackCode.UInt64; + span[1] = unchecked((byte)(value >> 56)); + span[2] = unchecked((byte)(value >> 48)); + span[3] = unchecked((byte)(value >> 40)); + span[4] = unchecked((byte)(value >> 32)); + span[5] = unchecked((byte)(value >> 24)); + span[6] = unchecked((byte)(value >> 16)); + span[7] = unchecked((byte)(value >> 8)); + span[8] = unchecked((byte)value); + writer.Advance(9); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static ulong ReadUInt64(byte[] bytes, int offset, out int readSize) + public static ulong ReadUInt64(ref ReadOnlySequence<byte> byteSequence) { - return uint64Decoders[bytes[offset]].Read(bytes, offset, out readSize); + return uint64Decoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteChar(ref byte[] bytes, int offset, char value) + public static void WriteChar(IBufferWriter<byte> writer, char value) { - return WriteUInt16(ref bytes, offset, (ushort)value); + WriteUInt16(writer, value); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static char ReadChar(byte[] bytes, int offset, out int readSize) + public static char ReadChar(ref ReadOnlySequence<byte> byteSequence) { - return (char)ReadUInt16(bytes, offset, out readSize); + return (char)ReadUInt16(ref byteSequence); } /// <summary> - /// Unsafe. If value is guranteed length is 0 ~ 31, can use this method. + /// Unsafe. If value is guaranteed length is 0 ~ 31, can use this method. /// </summary> #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteFixedStringUnsafe(ref byte[] bytes, int offset, string value, int byteCount) + public static unsafe void WriteFixedStringUnsafe(IBufferWriter<byte> writer, string value, int byteCount) { - EnsureCapacity(ref bytes, offset, byteCount + 1); - bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount); - StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 1); - - return byteCount + 1; + var span = writer.GetSpan(byteCount + 1); + span[0] = (byte)(MessagePackCode.MinFixStr | byteCount); + writer.Advance(StringEncoding.UTF8.GetBytes(value, span.Slice(1)) + 1); } /// <summary> @@ -1704,41 +1388,41 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteStringUnsafe(ref byte[] bytes, int offset, string value, int byteCount) + public static int WriteStringUnsafe(IBufferWriter<byte> writer, string value, int byteCount) { if (byteCount <= MessagePackRange.MaxFixStringLength) { - EnsureCapacity(ref bytes, offset, byteCount + 1); - bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount); - StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 1); + var span = writer.GetSpan(byteCount + 1); + span[0] = (byte)(MessagePackCode.MinFixStr | byteCount); + StringEncoding.UTF8.GetBytes(value, span.Slice(1)); return byteCount + 1; } else if (byteCount <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, byteCount + 2); - bytes[offset] = MessagePackCode.Str8; - bytes[offset + 1] = unchecked((byte)byteCount); - StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 2); + var span = writer.GetSpan(byteCount + 2); + span[0] = MessagePackCode.Str8; + span[1] = unchecked((byte)byteCount); + StringEncoding.UTF8.GetBytes(value, span.Slice(2)); return byteCount + 2; } else if (byteCount <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, byteCount + 3); - bytes[offset] = MessagePackCode.Str16; - bytes[offset + 1] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 2] = unchecked((byte)byteCount); - StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 3); + var span = writer.GetSpan(byteCount + 3); + span[0] = MessagePackCode.Str16; + span[1] = unchecked((byte)(byteCount >> 8)); + span[2] = unchecked((byte)byteCount); + StringEncoding.UTF8.GetBytes(value, span.Slice(3)); return byteCount + 3; } else { - EnsureCapacity(ref bytes, offset, byteCount + 5); - bytes[offset] = MessagePackCode.Str32; - bytes[offset + 1] = unchecked((byte)(byteCount >> 24)); - bytes[offset + 2] = unchecked((byte)(byteCount >> 16)); - bytes[offset + 3] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 4] = unchecked((byte)byteCount); - StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 5); + var span = writer.GetSpan(byteCount + 5); + span[0] = MessagePackCode.Str32; + span[1] = unchecked((byte)(byteCount >> 24)); + span[2] = unchecked((byte)(byteCount >> 16)); + span[3] = unchecked((byte)(byteCount >> 8)); + span[4] = unchecked((byte)byteCount); + StringEncoding.UTF8.GetBytes(value, span.Slice(5)); return byteCount + 5; } } @@ -1746,43 +1430,43 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteStringBytes(ref byte[] bytes, int offset, byte[] utf8stringBytes) + public static void WriteStringBytes(IBufferWriter<byte> writer, ReadOnlySpan<byte> utf8stringBytes) { var byteCount = utf8stringBytes.Length; if (byteCount <= MessagePackRange.MaxFixStringLength) { - EnsureCapacity(ref bytes, offset, byteCount + 1); - bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount); - Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 1, byteCount); - return byteCount + 1; + var span = writer.GetSpan(byteCount + 1); + span[0] = (byte)(MessagePackCode.MinFixStr | byteCount); + utf8stringBytes.CopyTo(span.Slice(1)); + writer.Advance(1); } else if (byteCount <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, byteCount + 2); - bytes[offset] = MessagePackCode.Str8; - bytes[offset + 1] = unchecked((byte)byteCount); - Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 2, byteCount); - return byteCount + 2; + var span = writer.GetSpan(byteCount + 2); + span[0] = MessagePackCode.Str8; + span[1] = unchecked((byte)byteCount); + utf8stringBytes.CopyTo(span.Slice(2)); + writer.Advance(2); } else if (byteCount <= ushort.MaxValue) { - EnsureCapacity(ref bytes, offset, byteCount + 3); - bytes[offset] = MessagePackCode.Str16; - bytes[offset + 1] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 2] = unchecked((byte)byteCount); - Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 3, byteCount); - return byteCount + 3; + var span = writer.GetSpan(byteCount + 3); + span[0] = MessagePackCode.Str16; + span[1] = unchecked((byte)(byteCount >> 8)); + span[2] = unchecked((byte)byteCount); + utf8stringBytes.CopyTo(span.Slice(3)); + writer.Advance(3); } else { - EnsureCapacity(ref bytes, offset, byteCount + 5); - bytes[offset] = MessagePackCode.Str32; - bytes[offset + 1] = unchecked((byte)(byteCount >> 24)); - bytes[offset + 2] = unchecked((byte)(byteCount >> 16)); - bytes[offset + 3] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 4] = unchecked((byte)byteCount); - Buffer.BlockCopy(utf8stringBytes, 0, bytes, offset + 5, byteCount); - return byteCount + 5; + var span = writer.GetSpan(byteCount + 5); + span[0] = MessagePackCode.Str32; + span[1] = unchecked((byte)(byteCount >> 24)); + span[2] = unchecked((byte)(byteCount >> 16)); + span[3] = unchecked((byte)(byteCount >> 8)); + span[4] = unchecked((byte)byteCount); + utf8stringBytes.CopyTo(span.Slice(5)); + writer.Advance(5); } } @@ -1826,15 +1510,19 @@ namespace MessagePack } } - public static int WriteString(ref byte[] bytes, int offset, string value) + public static void WriteString(IBufferWriter<byte> writer, string value) { - if (value == null) return WriteNil(ref bytes, offset); + if (value == null) + { + WriteNil(writer); + return; + } // MaxByteCount -> WritePrefix -> GetBytes has some overheads of `MaxByteCount` // solves heuristic length check // ensure buffer by MaxByteCount(faster than GetByteCount) - MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5); + var span = writer.GetSpan(StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5); int useOffset; if (value.Length <= MessagePackRange.MaxFixStringLength) @@ -1855,151 +1543,154 @@ namespace MessagePack } // skip length area - var writeBeginOffset = offset + useOffset; - var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, writeBeginOffset); + var byteCount = StringEncoding.UTF8.GetBytes(value, span.Slice(useOffset)); // move body and write prefix if (byteCount <= MessagePackRange.MaxFixStringLength) { if (useOffset != 1) { - Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 1, byteCount); + span.Slice(useOffset, byteCount).CopyTo(span.Slice(1)); } - bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount); - return byteCount + 1; + span[0] = (byte)(MessagePackCode.MinFixStr | byteCount); + writer.Advance(byteCount + 1); } else if (byteCount <= byte.MaxValue) { if (useOffset != 2) { - Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 2, byteCount); + span.Slice(useOffset, byteCount).CopyTo(span.Slice(2)); } - bytes[offset] = MessagePackCode.Str8; - bytes[offset + 1] = unchecked((byte)byteCount); - return byteCount + 2; + span[0] = MessagePackCode.Str8; + span[1] = unchecked((byte)byteCount); + writer.Advance(byteCount + 2); } else if (byteCount <= ushort.MaxValue) { if (useOffset != 3) { - Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 3, byteCount); + span.Slice(useOffset, byteCount).CopyTo(span.Slice(3)); } - bytes[offset] = MessagePackCode.Str16; - bytes[offset + 1] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 2] = unchecked((byte)byteCount); - return byteCount + 3; + span[0] = MessagePackCode.Str16; + span[1] = unchecked((byte)(byteCount >> 8)); + span[2] = unchecked((byte)byteCount); + writer.Advance(byteCount + 3); } else { if (useOffset != 5) { - Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 5, byteCount); + span.Slice(useOffset, byteCount).CopyTo(span.Slice(5)); } - bytes[offset] = MessagePackCode.Str32; - bytes[offset + 1] = unchecked((byte)(byteCount >> 24)); - bytes[offset + 2] = unchecked((byte)(byteCount >> 16)); - bytes[offset + 3] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 4] = unchecked((byte)byteCount); - return byteCount + 5; + span[0] = MessagePackCode.Str32; + span[1] = unchecked((byte)(byteCount >> 24)); + span[2] = unchecked((byte)(byteCount >> 16)); + span[3] = unchecked((byte)(byteCount >> 8)); + span[4] = unchecked((byte)byteCount); + writer.Advance(byteCount + 5); } } - public static int WriteStringForceStr32Block(ref byte[] bytes, int offset, string value) + public static void WriteStringForceStr32Block(IBufferWriter<byte> writer, string value) { - if (value == null) return WriteNil(ref bytes, offset); + if (value == null) + { + WriteNil(writer); + return; + } - MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5); + var span = writer.GetSpan(StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5); - var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, offset + 5); + var byteCount = StringEncoding.UTF8.GetBytes(value, span.Slice(5)); - bytes[offset] = MessagePackCode.Str32; - bytes[offset + 1] = unchecked((byte)(byteCount >> 24)); - bytes[offset + 2] = unchecked((byte)(byteCount >> 16)); - bytes[offset + 3] = unchecked((byte)(byteCount >> 8)); - bytes[offset + 4] = unchecked((byte)byteCount); - return byteCount + 5; + span[0] = MessagePackCode.Str32; + span[1] = unchecked((byte)(byteCount >> 24)); + span[2] = unchecked((byte)(byteCount >> 16)); + span[3] = unchecked((byte)(byteCount >> 8)); + span[4] = unchecked((byte)byteCount); + writer.Advance(byteCount + 5); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static string ReadString(byte[] bytes, int offset, out int readSize) + public static string ReadString(ref ReadOnlySequence<byte> byteSequence) { - return stringDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return stringDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static ArraySegment<byte> ReadStringSegment(byte[] bytes, int offset, out int readSize) + public static ArraySegment<byte> ReadStringSegment(ref ReadOnlySequence<byte> byteSequence) { - return stringSegmentDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return stringSegmentDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteExtensionFormatHeader(ref byte[] bytes, int offset, sbyte typeCode, int dataLength) + public static int WriteExtensionFormatHeader(IBufferWriter<byte> writer, sbyte typeCode, int dataLength) { switch (dataLength) { case 1: - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.FixExt1; - bytes[offset + 1] = unchecked((byte)typeCode); + var span = writer.GetSpan(3); + span[0] = MessagePackCode.FixExt1; + span[1] = unchecked((byte)typeCode); return 2; case 2: - EnsureCapacity(ref bytes, offset, 4); - bytes[offset] = MessagePackCode.FixExt2; - bytes[offset + 1] = unchecked((byte)typeCode); + span = writer.GetSpan(4); + span[0] = MessagePackCode.FixExt2; + span[1] = unchecked((byte)typeCode); return 2; case 4: - EnsureCapacity(ref bytes, offset, 6); - bytes[offset] = MessagePackCode.FixExt4; - bytes[offset + 1] = unchecked((byte)typeCode); + span = writer.GetSpan(6); + span[0] = MessagePackCode.FixExt4; + span[1] = unchecked((byte)typeCode); return 2; case 8: - EnsureCapacity(ref bytes, offset, 10); - bytes[offset] = MessagePackCode.FixExt8; - bytes[offset + 1] = unchecked((byte)typeCode); + span = writer.GetSpan(10); + span[0] = MessagePackCode.FixExt8; + span[1] = unchecked((byte)typeCode); return 2; case 16: - EnsureCapacity(ref bytes, offset, 18); - bytes[offset] = MessagePackCode.FixExt16; - bytes[offset + 1] = unchecked((byte)typeCode); + span = writer.GetSpan(18); + span[0] = MessagePackCode.FixExt16; + span[1] = unchecked((byte)typeCode); return 2; default: unchecked { if (dataLength <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, dataLength + 3); - bytes[offset] = MessagePackCode.Ext8; - bytes[offset + 1] = unchecked((byte)(dataLength)); - bytes[offset + 2] = unchecked((byte)typeCode); + span = writer.GetSpan(dataLength + 3); + span[0] = MessagePackCode.Ext8; + span[1] = unchecked((byte)(dataLength)); + span[2] = unchecked((byte)typeCode); return 3; } else if (dataLength <= UInt16.MaxValue) { - EnsureCapacity(ref bytes, offset, dataLength + 4); - bytes[offset] = MessagePackCode.Ext16; - bytes[offset + 1] = unchecked((byte)(dataLength >> 8)); - bytes[offset + 2] = unchecked((byte)(dataLength)); - bytes[offset + 3] = unchecked((byte)typeCode); + span = writer.GetSpan(dataLength + 4); + span[0] = MessagePackCode.Ext16; + span[1] = unchecked((byte)(dataLength >> 8)); + span[2] = unchecked((byte)(dataLength)); + span[3] = unchecked((byte)typeCode); return 4; } else { - EnsureCapacity(ref bytes, offset, dataLength + 6); - bytes[offset] = MessagePackCode.Ext32; - bytes[offset + 1] = unchecked((byte)(dataLength >> 24)); - bytes[offset + 2] = unchecked((byte)(dataLength >> 16)); - bytes[offset + 3] = unchecked((byte)(dataLength >> 8)); - bytes[offset + 4] = unchecked((byte)dataLength); - bytes[offset + 5] = unchecked((byte)typeCode); + span = writer.GetSpan(dataLength + 6); + span[0] = MessagePackCode.Ext32; + span[1] = unchecked((byte)(dataLength >> 24)); + span[2] = unchecked((byte)(dataLength >> 16)); + span[3] = unchecked((byte)(dataLength >> 8)); + span[4] = unchecked((byte)dataLength); + span[5] = unchecked((byte)typeCode); return 6; } } @@ -2012,115 +1703,123 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteExtensionFormatHeaderForceExt32Block(ref byte[] bytes, int offset, sbyte typeCode, int dataLength) - { - EnsureCapacity(ref bytes, offset, dataLength + 6); - bytes[offset] = MessagePackCode.Ext32; - bytes[offset + 1] = unchecked((byte)(dataLength >> 24)); - bytes[offset + 2] = unchecked((byte)(dataLength >> 16)); - bytes[offset + 3] = unchecked((byte)(dataLength >> 8)); - bytes[offset + 4] = unchecked((byte)dataLength); - bytes[offset + 5] = unchecked((byte)typeCode); + public static int WriteExtensionFormatHeaderForceExt32Block(IBufferWriter<byte> writer, sbyte typeCode, int dataLength) + { + var span = writer.GetSpan(dataLength + 6); + span[0] = MessagePackCode.Ext32; + span[1] = unchecked((byte)(dataLength >> 24)); + span[2] = unchecked((byte)(dataLength >> 16)); + span[3] = unchecked((byte)(dataLength >> 8)); + span[4] = unchecked((byte)dataLength); + span[5] = unchecked((byte)typeCode); return 6; } #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteExtensionFormat(ref byte[] bytes, int offset, sbyte typeCode, byte[] data) + public static void WriteExtensionFormat(IBufferWriter<byte> writer, sbyte typeCode, ReadOnlySpan<byte> data) { var length = data.Length; switch (length) { case 1: - EnsureCapacity(ref bytes, offset, 3); - bytes[offset] = MessagePackCode.FixExt1; - bytes[offset + 1] = unchecked((byte)typeCode); - bytes[offset + 2] = data[0]; - return 3; + var span = writer.GetSpan(3); + span[0] = MessagePackCode.FixExt1; + span[1] = unchecked((byte)typeCode); + span[2] = data[0]; + writer.Advance(3); + return; case 2: - EnsureCapacity(ref bytes, offset, 4); - bytes[offset] = MessagePackCode.FixExt2; - bytes[offset + 1] = unchecked((byte)typeCode); - bytes[offset + 2] = data[0]; - bytes[offset + 3] = data[1]; - return 4; + span = writer.GetSpan(4); + span[0] = MessagePackCode.FixExt2; + span[1] = unchecked((byte)typeCode); + span[2] = data[0]; + span[3] = data[1]; + writer.Advance(4); + return; case 4: - EnsureCapacity(ref bytes, offset, 6); - bytes[offset] = MessagePackCode.FixExt4; - bytes[offset + 1] = unchecked((byte)typeCode); - bytes[offset + 2] = data[0]; - bytes[offset + 3] = data[1]; - bytes[offset + 4] = data[2]; - bytes[offset + 5] = data[3]; - return 6; + span = writer.GetSpan(6); + span[0] = MessagePackCode.FixExt4; + span[1] = unchecked((byte)typeCode); + span[2] = data[0]; + span[3] = data[1]; + span[4] = data[2]; + span[5] = data[3]; + writer.Advance(6); + return; case 8: - EnsureCapacity(ref bytes, offset, 10); - bytes[offset] = MessagePackCode.FixExt8; - bytes[offset + 1] = unchecked((byte)typeCode); - bytes[offset + 2] = data[0]; - bytes[offset + 3] = data[1]; - bytes[offset + 4] = data[2]; - bytes[offset + 5] = data[3]; - bytes[offset + 6] = data[4]; - bytes[offset + 7] = data[5]; - bytes[offset + 8] = data[6]; - bytes[offset + 9] = data[7]; - return 10; + span = writer.GetSpan(10); + span[0] = MessagePackCode.FixExt8; + span[1] = unchecked((byte)typeCode); + span[2] = data[0]; + span[3] = data[1]; + span[4] = data[2]; + span[5] = data[3]; + span[6] = data[4]; + span[7] = data[5]; + span[8] = data[6]; + span[9] = data[7]; + writer.Advance(10); + return; case 16: - EnsureCapacity(ref bytes, offset, 18); - bytes[offset] = MessagePackCode.FixExt16; - bytes[offset + 1] = unchecked((byte)typeCode); - bytes[offset + 2] = data[0]; - bytes[offset + 3] = data[1]; - bytes[offset + 4] = data[2]; - bytes[offset + 5] = data[3]; - bytes[offset + 6] = data[4]; - bytes[offset + 7] = data[5]; - bytes[offset + 8] = data[6]; - bytes[offset + 9] = data[7]; - bytes[offset + 10] = data[8]; - bytes[offset + 11] = data[9]; - bytes[offset + 12] = data[10]; - bytes[offset + 13] = data[11]; - bytes[offset + 14] = data[12]; - bytes[offset + 15] = data[13]; - bytes[offset + 16] = data[14]; - bytes[offset + 17] = data[15]; - return 18; + span = writer.GetSpan(18); + span[0] = MessagePackCode.FixExt16; + span[1] = unchecked((byte)typeCode); + span[2] = data[0]; + span[3] = data[1]; + span[4] = data[2]; + span[5] = data[3]; + span[6] = data[4]; + span[7] = data[5]; + span[8] = data[6]; + span[9] = data[7]; + span[10] = data[8]; + span[11] = data[9]; + span[12] = data[10]; + span[13] = data[11]; + span[14] = data[12]; + span[15] = data[13]; + span[16] = data[14]; + span[17] = data[15]; + writer.Advance(18); + return; default: unchecked { if (data.Length <= byte.MaxValue) { - EnsureCapacity(ref bytes, offset, length + 3); - bytes[offset] = MessagePackCode.Ext8; - bytes[offset + 1] = unchecked((byte)(length)); - bytes[offset + 2] = unchecked((byte)typeCode); - Buffer.BlockCopy(data, 0, bytes, offset + 3, length); - return length + 3; + span = writer.GetSpan(length + 3); + span[0] = MessagePackCode.Ext8; + span[1] = unchecked((byte)(length)); + span[2] = unchecked((byte)typeCode); + data.Slice(0, length).CopyTo(span.Slice(3)); + writer.Advance(length + 3); + return; } else if (data.Length <= UInt16.MaxValue) { - EnsureCapacity(ref bytes, offset, length + 4); - bytes[offset] = MessagePackCode.Ext16; - bytes[offset + 1] = unchecked((byte)(length >> 8)); - bytes[offset + 2] = unchecked((byte)(length)); - bytes[offset + 3] = unchecked((byte)typeCode); - Buffer.BlockCopy(data, 0, bytes, offset + 4, length); - return length + 4; + span = writer.GetSpan(length + 4); + span[0] = MessagePackCode.Ext16; + span[1] = unchecked((byte)(length >> 8)); + span[2] = unchecked((byte)(length)); + span[3] = unchecked((byte)typeCode); + data.Slice(0, length).CopyTo(span.Slice(4)); + writer.Advance(length + 4); + return; } else { - EnsureCapacity(ref bytes, offset, length + 6); - bytes[offset] = MessagePackCode.Ext32; - bytes[offset + 1] = unchecked((byte)(length >> 24)); - bytes[offset + 2] = unchecked((byte)(length >> 16)); - bytes[offset + 3] = unchecked((byte)(length >> 8)); - bytes[offset + 4] = unchecked((byte)length); - bytes[offset + 5] = unchecked((byte)typeCode); - Buffer.BlockCopy(data, 0, bytes, offset + 6, length); - return length + 6; + span = writer.GetSpan(length + 6); + span[0] = MessagePackCode.Ext32; + span[1] = unchecked((byte)(length >> 24)); + span[2] = unchecked((byte)(length >> 16)); + span[3] = unchecked((byte)(length >> 8)); + span[4] = unchecked((byte)length); + span[5] = unchecked((byte)typeCode); + data.Slice(0, length).CopyTo(span.Slice(6)); + writer.Advance(length + 6); + return; } } } @@ -2129,9 +1828,9 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static ExtensionResult ReadExtensionFormat(byte[] bytes, int offset, out int readSize) + public static ExtensionResult ReadExtensionFormat(ref ReadOnlySequence<byte> byteSequence) { - return extDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return extDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } /// <summary> @@ -2140,9 +1839,9 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static ExtensionHeader ReadExtensionFormatHeader(byte[] bytes, int offset, out int readSize) + public static ExtensionHeader ReadExtensionFormatHeader(ref ReadOnlySequence<byte> byteSequence) { - return extHeaderDecoders[bytes[offset]].Read(bytes, offset, out readSize); + return extHeaderDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } #if NETSTANDARD || NETFRAMEWORK @@ -2183,7 +1882,7 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static int WriteDateTime(ref byte[] bytes, int offset, DateTime dateTime) + public static int WriteDateTime(IBufferWriter<byte> writer, DateTime dateTime) { dateTime = dateTime.ToUniversalTime(); @@ -2226,51 +1925,51 @@ namespace MessagePack { // timestamp 32(seconds in 32-bit unsigned int) var data32 = (UInt32)data64; - EnsureCapacity(ref bytes, offset, 6); - bytes[offset] = MessagePackCode.FixExt4; - bytes[offset + 1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime); - bytes[offset + 2] = unchecked((byte)(data32 >> 24)); - bytes[offset + 3] = unchecked((byte)(data32 >> 16)); - bytes[offset + 4] = unchecked((byte)(data32 >> 8)); - bytes[offset + 5] = unchecked((byte)data32); + var span = writer.GetSpan(6); + span[0] = MessagePackCode.FixExt4; + span[1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime); + span[2] = unchecked((byte)(data32 >> 24)); + span[3] = unchecked((byte)(data32 >> 16)); + span[4] = unchecked((byte)(data32 >> 8)); + span[5] = unchecked((byte)data32); return 6; } else { // timestamp 64(nanoseconds in 30-bit unsigned int | seconds in 34-bit unsigned int) - EnsureCapacity(ref bytes, offset, 10); - bytes[offset] = MessagePackCode.FixExt8; - bytes[offset + 1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime); - bytes[offset + 2] = unchecked((byte)(data64 >> 56)); - bytes[offset + 3] = unchecked((byte)(data64 >> 48)); - bytes[offset + 4] = unchecked((byte)(data64 >> 40)); - bytes[offset + 5] = unchecked((byte)(data64 >> 32)); - bytes[offset + 6] = unchecked((byte)(data64 >> 24)); - bytes[offset + 7] = unchecked((byte)(data64 >> 16)); - bytes[offset + 8] = unchecked((byte)(data64 >> 8)); - bytes[offset + 9] = unchecked((byte)data64); + var span = writer.GetSpan(10); + span[0] = MessagePackCode.FixExt8; + span[1] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime); + span[2] = unchecked((byte)(data64 >> 56)); + span[3] = unchecked((byte)(data64 >> 48)); + span[4] = unchecked((byte)(data64 >> 40)); + span[5] = unchecked((byte)(data64 >> 32)); + span[6] = unchecked((byte)(data64 >> 24)); + span[7] = unchecked((byte)(data64 >> 16)); + span[8] = unchecked((byte)(data64 >> 8)); + span[9] = unchecked((byte)data64); return 10; } } else { // timestamp 96( nanoseconds in 32-bit unsigned int | seconds in 64-bit signed int ) - EnsureCapacity(ref bytes, offset, 15); - bytes[offset] = MessagePackCode.Ext8; - bytes[offset + 1] = (byte)12; - bytes[offset + 2] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime); - bytes[offset + 3] = unchecked((byte)(nanoseconds >> 24)); - bytes[offset + 4] = unchecked((byte)(nanoseconds >> 16)); - bytes[offset + 5] = unchecked((byte)(nanoseconds >> 8)); - bytes[offset + 6] = unchecked((byte)nanoseconds); - bytes[offset + 7] = unchecked((byte)(seconds >> 56)); - bytes[offset + 8] = unchecked((byte)(seconds >> 48)); - bytes[offset + 9] = unchecked((byte)(seconds >> 40)); - bytes[offset + 10] = unchecked((byte)(seconds >> 32)); - bytes[offset + 11] = unchecked((byte)(seconds >> 24)); - bytes[offset + 12] = unchecked((byte)(seconds >> 16)); - bytes[offset + 13] = unchecked((byte)(seconds >> 8)); - bytes[offset + 14] = unchecked((byte)seconds); + var span = writer.GetSpan(15); + span[0] = MessagePackCode.Ext8; + span[1] = 12; + span[2] = unchecked((byte)ReservedMessagePackExtensionTypeCode.DateTime); + span[3] = unchecked((byte)(nanoseconds >> 24)); + span[4] = unchecked((byte)(nanoseconds >> 16)); + span[5] = unchecked((byte)(nanoseconds >> 8)); + span[6] = unchecked((byte)nanoseconds); + span[7] = unchecked((byte)(seconds >> 56)); + span[8] = unchecked((byte)(seconds >> 48)); + span[9] = unchecked((byte)(seconds >> 40)); + span[10] = unchecked((byte)(seconds >> 32)); + span[11] = unchecked((byte)(seconds >> 24)); + span[12] = unchecked((byte)(seconds >> 16)); + span[13] = unchecked((byte)(seconds >> 8)); + span[14] = unchecked((byte)seconds); return 15; } } @@ -2278,1084 +1977,38 @@ namespace MessagePack #if NETSTANDARD || NETFRAMEWORK [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] #endif - public static DateTime ReadDateTime(byte[] bytes, int offset, out int readSize) - { - return dateTimeDecoders[bytes[offset]].Read(bytes, offset, out readSize); - } - } - - // Stream Overload - public static partial class MessagePackBinary - { - static class StreamDecodeMemoryPool + public static DateTime ReadDateTime(ref ReadOnlySequence<byte> byteSequence) { - [ThreadStatic] - static byte[] buffer = null; - - public static byte[] GetBuffer() - { - if (buffer == null) - { - buffer = new byte[65536]; - } - return buffer; - } + return dateTimeDecoders[byteSequence.First.Span[0]].Read(ref byteSequence); } - static byte[] ReadMessageBlockFromStreamUnsafe(Stream stream) - { - int _; - return ReadMessageBlockFromStreamUnsafe(stream, false, out _); - } + internal delegate T ContiguousMemoryReader<T>(ReadOnlySpan<byte> span); - /// <summary> - /// Read MessageBlock, returns byte[] block is in MemoryPool so careful to use. - /// </summary> - public static byte[] ReadMessageBlockFromStreamUnsafe(Stream stream, bool readOnlySingleMessage, out int readSize) + internal static T Read<T>(ref ReadOnlySequence<byte> byteSequence, int length, ContiguousMemoryReader<T> reader) { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - readSize = ReadMessageBlockFromStreamCore(stream, ref bytes, 0, readOnlySingleMessage); - return bytes; - } - - static int ReadMessageBlockFromStreamCore(Stream stream, ref byte[] bytes, int offset, bool readOnlySingleMessage) - { - var byteCode = stream.ReadByte(); - if (byteCode < 0 || byte.MaxValue < byteCode) - { - throw new InvalidOperationException("Invalid MessagePack code was detected, code:" + byteCode); - } - - var code = (byte)byteCode; - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 1); - bytes[offset] = code; - - var type = MessagePackCode.ToMessagePackType(code); - switch (type) - { - case MessagePackType.Integer: - { - var readCount = 0; - if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) return 1; - else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) return 1; - - switch (code) - { - case MessagePackCode.Int8: readCount = 1; break; - case MessagePackCode.Int16: readCount = 2; break; - case MessagePackCode.Int32: readCount = 4; break; - case MessagePackCode.Int64: readCount = 8; break; - case MessagePackCode.UInt8: readCount = 1; break; - case MessagePackCode.UInt16: readCount = 2; break; - case MessagePackCode.UInt32: readCount = 4; break; - case MessagePackCode.UInt64: readCount = 8; break; - default: throw new InvalidOperationException("Invalid Code"); - } - - MessagePackBinary.EnsureCapacity(ref bytes, offset, readCount + 1); - ReadFully(stream, bytes, offset + 1, readCount); - return readCount + 1; - } - case MessagePackType.Unknown: - case MessagePackType.Nil: - case MessagePackType.Boolean: - return 1; - case MessagePackType.Float: - if (code == MessagePackCode.Float32) - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 5); - ReadFully(stream, bytes, offset + 1, 4); - return 5; - } - else - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 9); - ReadFully(stream, bytes, offset + 1, 8); - return 9; - } - case MessagePackType.String: - { - if (MessagePackCode.MinFixStr <= code && code <= MessagePackCode.MaxFixStr) - { - var length = bytes[offset] & 0x1F; - MessagePackBinary.EnsureCapacity(ref bytes, offset, 1 + length); - ReadFully(stream, bytes, offset + 1, length); - return length + 1; - } - - switch (code) - { - case MessagePackCode.Str8: - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 2); - ReadFully(stream, bytes, offset + 1, 1); - var length = bytes[offset + 1]; - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 2 + length); - ReadFully(stream, bytes, offset + 2, length); - - return length + 2; - } - case MessagePackCode.Str16: - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 3); - ReadFully(stream, bytes, offset + 1, 2); - var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]); - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 3 + length); - ReadFully(stream, bytes, offset + 3, length); - - return length + 3; - } - case MessagePackCode.Str32: - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 5); - ReadFully(stream, bytes, offset + 1, 4); - var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]); - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 5 + length); - ReadFully(stream, bytes, offset + 5, length); - - return length + 5; - } - default: throw new InvalidOperationException("Invalid Code"); - } - } - case MessagePackType.Binary: - { - switch (code) - { - case MessagePackCode.Bin8: - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 2); - ReadFully(stream, bytes, offset + 1, 1); - var length = bytes[offset + 1]; - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 2 + length); - ReadFully(stream, bytes, offset + 2, length); - - return length + 2; - } - case MessagePackCode.Bin16: - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 3); - ReadFully(stream, bytes, offset + 1, 2); - var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]); - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 3 + length); - ReadFully(stream, bytes, offset + 3, length); - - return length + 3; - } - case MessagePackCode.Bin32: - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, 5); - ReadFully(stream, bytes, offset + 1, 4); - var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]); - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 5 + length); - ReadFully(stream, bytes, offset + 5, length); - - return length + 5; - } - default: throw new InvalidOperationException("Invalid Code"); - } - } - case MessagePackType.Array: - { - var readHeaderSize = 0; - - if (MessagePackCode.MinFixArray <= code && code <= MessagePackCode.MaxFixArray) readHeaderSize = 0; - else if (code == MessagePackCode.Array16) readHeaderSize = 2; - else if (code == MessagePackCode.Array32) readHeaderSize = 4; - if (readHeaderSize != 0) - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, readHeaderSize + 1); - ReadFully(stream, bytes, offset + 1, readHeaderSize); - } - - var startOffset = offset; - offset += (readHeaderSize + 1); - - int _; - var length = ReadArrayHeaderRaw(bytes, startOffset, out _); - if (!readOnlySingleMessage) - { - for (int i = 0; i < length; i++) - { - offset += ReadMessageBlockFromStreamCore(stream, ref bytes, offset, readOnlySingleMessage); - } - } - - return offset - startOffset; - } - case MessagePackType.Map: - { - var readHeaderSize = 0; - - if (MessagePackCode.MinFixMap <= code && code <= MessagePackCode.MaxFixMap) readHeaderSize = 0; - else if (code == MessagePackCode.Map16) readHeaderSize = 2; - else if (code == MessagePackCode.Map32) readHeaderSize = 4; - if (readHeaderSize != 0) - { - MessagePackBinary.EnsureCapacity(ref bytes, offset, readHeaderSize + 1); - ReadFully(stream, bytes, offset + 1, readHeaderSize); - } - - var startOffset = offset; - offset += (readHeaderSize + 1); - - int _; - var length = ReadMapHeaderRaw(bytes, startOffset, out _); - if (!readOnlySingleMessage) - { - for (int i = 0; i < length; i++) - { - offset += ReadMessageBlockFromStreamCore(stream, ref bytes, offset, readOnlySingleMessage); // key - offset += ReadMessageBlockFromStreamCore(stream, ref bytes, offset, readOnlySingleMessage); // value - } - } - - return offset - startOffset; - } - case MessagePackType.Extension: - { - var readHeaderSize = 0; - - switch (code) - { - case MessagePackCode.FixExt1: readHeaderSize = 1; break; - case MessagePackCode.FixExt2: readHeaderSize = 1; break; - case MessagePackCode.FixExt4: readHeaderSize = 1; break; - case MessagePackCode.FixExt8: readHeaderSize = 1; break; - case MessagePackCode.FixExt16: readHeaderSize = 1; break; - case MessagePackCode.Ext8: readHeaderSize = 2; break; - case MessagePackCode.Ext16: readHeaderSize = 3; break; - case MessagePackCode.Ext32: readHeaderSize = 5; break; - default: throw new InvalidOperationException("Invalid Code"); - } - - MessagePackBinary.EnsureCapacity(ref bytes, offset, readHeaderSize + 1); - ReadFully(stream, bytes, offset + 1, readHeaderSize); - - if (!readOnlySingleMessage) - { - int _; - var header = ReadExtensionFormatHeader(bytes, offset, out _); - - MessagePackBinary.EnsureCapacity(ref bytes, offset, 1 + readHeaderSize + (int)header.Length); - ReadFully(stream, bytes, offset + 1 + readHeaderSize, (int)header.Length); - - return 1 + readHeaderSize + (int)header.Length; - } - else - { - return readHeaderSize + 1; - } - } - default: throw new InvalidOperationException("Invalid Code"); - } - } - - static void ReadFully(Stream stream, byte[] bytes, int offset, int readSize) - { - var nextLen = readSize; - while (nextLen != 0) - { - var len = stream.Read(bytes, offset, nextLen); - if (len == -1) return; - offset += len; - nextLen = nextLen - len; - } - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int ReadNext(Stream stream) - { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - return ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int ReadNextBlock(Stream stream) - { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - var offset = 0; - return ReadMessageBlockFromStreamCore(stream, ref bytes, offset, false); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteNil(Stream stream) - { - stream.WriteByte(MessagePackCode.Nil); - return 1; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static Nil ReadNil(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadNil(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static bool IsNil(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - - return bytes[offset] == MessagePackCode.Nil; - } - - /// <summary> - /// Unsafe. If value is guranteed 0 ~ MessagePackRange.MaxFixMapCount(15), can use this method. - /// </summary> - /// <returns></returns> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteFixedMapHeaderUnsafe(Stream stream, int count) - { - stream.WriteByte((byte)(MessagePackCode.MinFixMap | count)); - return 1; - } - - /// <summary> - /// Write map count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteMapHeader(Stream stream, int count) - { - checked + const int StackAllocLimit = 64 * 1024; + T result; + if (byteSequence.First.Length >= length) { - return WriteMapHeader(stream, (uint)count); + result = reader(byteSequence.First.Span); } - } - - /// <summary> - /// Write map count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteMapHeader(Stream stream, uint count) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteMapHeader(ref buffer, 0, count); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Write map format header, always use map32 format(length is fixed, 5). - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteMapHeaderForceMap32Block(Stream stream, uint count) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteMapHeaderForceMap32Block(ref buffer, 0, count); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Return map count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int ReadMapHeader(Stream stream) - { - checked + else if (length <= StackAllocLimit) { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true); - int readSize; - return ReadMapHeader(bytes, 0, out readSize); + Span<byte> span = stackalloc byte[length]; + byteSequence.Slice(0, length).CopyTo(span); + result = reader(span); } - } - - /// <summary> - /// Return map count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static uint ReadMapHeaderRaw(Stream stream) - { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true); - int readSize; - return ReadMapHeaderRaw(bytes, 0, out readSize); - } - - /// <summary> - /// Unsafe. If value is guranteed 0 ~ MessagePackRange.MaxFixArrayCount(15), can use this method. - /// </summary> - /// <returns></returns> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteFixedArrayHeaderUnsafe(Stream stream, int count) - { - stream.WriteByte((byte)(MessagePackCode.MinFixArray | count)); - return 1; - } - - /// <summary> - /// Write array count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteArrayHeader(Stream stream, int count) - { - checked + else { - return WriteArrayHeader(stream, (uint)count); + using (var rental = MemoryPool<byte>.Shared.Rent(length)) + { + byteSequence.Slice(0, length).CopyTo(rental.Memory.Span); + result = reader(rental.Memory.Span.Slice(0, length)); + } } - } - - /// <summary> - /// Write array count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteArrayHeader(Stream stream, uint count) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteArrayHeader(ref buffer, 0, count); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Write array format header, always use array32 format(length is fixed, 5). - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteArrayHeaderForceArray32Block(Stream stream, uint count) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteArrayHeaderForceArray32Block(ref buffer, 0, count); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Return array count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int ReadArrayHeader(Stream stream) - { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true); - int readSize; - return ReadArrayHeader(bytes, 0, out readSize); - } - /// <summary> - /// Return array count. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static uint ReadArrayHeaderRaw(Stream stream) - { - var bytes = StreamDecodeMemoryPool.GetBuffer(); - ReadMessageBlockFromStreamCore(stream, ref bytes, 0, true); - int readSize; - return ReadArrayHeaderRaw(bytes, 0, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteBoolean(Stream stream, bool value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteBoolean(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static bool ReadBoolean(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadBoolean(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteByte(Stream stream, byte value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteByte(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteByteForceByteBlock(Stream stream, byte value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteByteForceByteBlock(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static byte ReadByte(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadByte(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteBytes(Stream stream, byte[] value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteBytes(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteBytes(Stream stream, byte[] src, int srcOffset, int count) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteBytes(ref buffer, 0, src, srcOffset, count); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static byte[] ReadBytes(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadBytes(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteSByte(Stream stream, sbyte value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteSByte(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteSByteForceSByteBlock(Stream stream, sbyte value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteSByteForceSByteBlock(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static sbyte ReadSByte(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadSByte(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteSingle(Stream stream, float value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteSingle(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static float ReadSingle(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadSingle(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteDouble(Stream stream, double value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteDouble(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static double ReadDouble(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadDouble(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteInt16(Stream stream, short value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteInt16(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteInt16ForceInt16Block(Stream stream, short value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteInt16ForceInt16Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static short ReadInt16(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadInt16(bytes, offset, out readSize); - } - - /// <summary> - /// Unsafe. If value is guranteed 0 ~ MessagePackCode.MaxFixInt(127), can use this method. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WritePositiveFixedIntUnsafe(Stream stream, int value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WritePositiveFixedIntUnsafe(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteInt32(Stream stream, int value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteInt32(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Acquire static message block(always 5 bytes). - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteInt32ForceInt32Block(Stream stream, int value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteInt32ForceInt32Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int ReadInt32(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadInt32(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteInt64(Stream stream, long value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteInt64(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteInt64ForceInt64Block(Stream stream, long value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteInt64ForceInt64Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static long ReadInt64(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadInt64(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteUInt16(Stream stream, ushort value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteUInt16(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteUInt16ForceUInt16Block(Stream stream, ushort value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteUInt16ForceUInt16Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static ushort ReadUInt16(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadUInt16(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteUInt32(Stream stream, uint value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteUInt32(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteUInt32ForceUInt32Block(Stream stream, uint value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteUInt32ForceUInt32Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static uint ReadUInt32(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadUInt32(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteUInt64(Stream stream, ulong value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteUInt64(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteUInt64ForceUInt64Block(Stream stream, ulong value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteUInt64ForceUInt64Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static ulong ReadUInt64(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadUInt64(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteChar(Stream stream, char value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteChar(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static char ReadChar(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadChar(bytes, offset, out readSize); - } - - /// <summary> - /// Unsafe. If value is guranteed length is 0 ~ 31, can use this method. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteFixedStringUnsafe(Stream stream, string value, int byteCount) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteFixedStringUnsafe(ref buffer, 0, value, byteCount); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Unsafe. If pre-calculated byteCount of target string, can use this method. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteStringUnsafe(Stream stream, string value, int byteCount) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteStringUnsafe(ref buffer, 0, value, byteCount); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteStringBytes(Stream stream, byte[] utf8stringBytes) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteStringBytes(ref buffer, 0, utf8stringBytes); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - public static int WriteString(Stream stream, string value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteString(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - public static int WriteStringForceStr32Block(Stream stream, string value) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteStringForceStr32Block(ref buffer, 0, value); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static string ReadString(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadString(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteExtensionFormatHeader(Stream stream, sbyte typeCode, int dataLength) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteExtensionFormatHeader(ref buffer, 0, typeCode, dataLength); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - - /// <summary> - /// Write extension format header, always use ext32 format(length is fixed, 6). - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteExtensionFormatHeaderForceExt32Block(Stream stream, sbyte typeCode, int dataLength) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteExtensionFormatHeaderForceExt32Block(ref buffer, 0, typeCode, dataLength); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteExtensionFormat(Stream stream, sbyte typeCode, byte[] data) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteExtensionFormat(ref buffer, 0, typeCode, data); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static ExtensionResult ReadExtensionFormat(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadExtensionFormat(bytes, offset, out readSize); - } - - /// <summary> - /// return byte length of ExtensionFormat. - /// </summary> -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static ExtensionHeader ReadExtensionFormatHeader(Stream stream) - { - int readSize; - var bytes = ReadMessageBlockFromStreamUnsafe(stream, true, out readSize); - var offset = 0; - - return ReadExtensionFormatHeader(bytes, offset, out readSize); - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static int WriteDateTime(Stream stream, DateTime dateTime) - { - var buffer = StreamDecodeMemoryPool.GetBuffer(); - var writeCount = WriteDateTime(ref buffer, 0, dateTime); - stream.Write(buffer, 0, writeCount); - return writeCount; - } - -#if NETSTANDARD || NETFRAMEWORK - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -#endif - public static DateTime ReadDateTime(Stream stream) - { - var bytes = ReadMessageBlockFromStreamUnsafe(stream); - var offset = 0; - int readSize; - - return ReadDateTime(bytes, offset, out readSize); + byteSequence = byteSequence.Slice(length); + return result; } } @@ -3398,22 +2051,23 @@ namespace MessagePack.Decoders { internal interface IMapHeaderDecoder { - uint Read(byte[] bytes, int offset, out int readSize); + uint Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixMapHeader : IMapHeaderDecoder { internal static readonly IMapHeaderDecoder Instance = new FixMapHeader(); - FixMapHeader() + private FixMapHeader() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return (uint)(bytes[offset] & 0xF); + uint result = (uint)(byteSequence.First.Span[0] & 0xF); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -3421,18 +2075,14 @@ namespace MessagePack.Decoders { internal static readonly IMapHeaderDecoder Instance = new Map16Header(); - Map16Header() + private Map16Header() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 3; - unchecked - { - return (uint)((bytes[offset + 1] << 8) | (bytes[offset + 2])); - } + return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((uint)((span[1] << 8) | (span[2])))); } } @@ -3440,18 +2090,14 @@ namespace MessagePack.Decoders { internal static readonly IMapHeaderDecoder Instance = new Map32Header(); - Map32Header() + private Map32Header() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 5; - unchecked - { - return (uint)((bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | bytes[offset + 4]); - } + return MessagePackBinary.Read(ref byteSequence, 5, span => unchecked((uint)((span[1] << 24) | (span[2] << 16) | (span[3] << 8) | span[4]))); } } @@ -3459,35 +2105,36 @@ namespace MessagePack.Decoders { internal static readonly IMapHeaderDecoder Instance = new InvalidMapHeader(); - InvalidMapHeader() + private InvalidMapHeader() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IArrayHeaderDecoder { - uint Read(byte[] bytes, int offset, out int readSize); + uint Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixArrayHeader : IArrayHeaderDecoder { internal static readonly IArrayHeaderDecoder Instance = new FixArrayHeader(); - FixArrayHeader() + private FixArrayHeader() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return (uint)(bytes[offset] & 0xF); + uint result = (uint)(byteSequence.First.Span[0] & 0xF); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -3495,18 +2142,14 @@ namespace MessagePack.Decoders { internal static readonly IArrayHeaderDecoder Instance = new Array16Header(); - Array16Header() + private Array16Header() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 3; - unchecked - { - return (uint)((bytes[offset + 1] << 8) | (bytes[offset + 2])); - } + return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((uint)((span[1] << 8) | (span[2])))); } } @@ -3514,18 +2157,14 @@ namespace MessagePack.Decoders { internal static readonly IArrayHeaderDecoder Instance = new Array32Header(); - Array32Header() + private Array32Header() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 5; - unchecked - { - return (uint)((bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | bytes[offset + 4]); - } + return MessagePackBinary.Read(ref byteSequence, 5, span => unchecked((uint)((span[1] << 24) | (span[2] << 16) | (span[3] << 8) | span[4]))); } } @@ -3533,14 +2172,14 @@ namespace MessagePack.Decoders { internal static readonly IArrayHeaderDecoder Instance = new InvalidArrayHeader(); - InvalidArrayHeader() + private InvalidArrayHeader() { } - public uint Read(byte[] bytes, int offset, out int readSize) + public uint Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } @@ -3553,7 +2192,7 @@ namespace MessagePack.Decoders { internal static IBooleanDecoder Instance = new True(); - True() { } + private True() { } public bool Read() { @@ -3565,7 +2204,7 @@ namespace MessagePack.Decoders { internal static IBooleanDecoder Instance = new False(); - False() { } + private False() { } public bool Read() { @@ -3577,7 +2216,7 @@ namespace MessagePack.Decoders { internal static IBooleanDecoder Instance = new InvalidBoolean(); - InvalidBoolean() { } + private InvalidBoolean() { } public bool Read() { @@ -3587,22 +2226,23 @@ namespace MessagePack.Decoders internal interface IByteDecoder { - byte Read(byte[] bytes, int offset, out int readSize); + byte Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixByte : IByteDecoder { internal static readonly IByteDecoder Instance = new FixByte(); - FixByte() + private FixByte() { } - public byte Read(byte[] bytes, int offset, out int readSize) + public byte Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return bytes[offset]; + byte result = byteSequence.First.Span[0]; + byteSequence = byteSequence.Slice(1); + return result; } } @@ -3610,15 +2250,14 @@ namespace MessagePack.Decoders { internal static readonly IByteDecoder Instance = new UInt8Byte(); - UInt8Byte() + private UInt8Byte() { } - public byte Read(byte[] bytes, int offset, out int readSize) + public byte Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 2; - return bytes[offset + 1]; + return MessagePackBinary.Read(ref byteSequence, 2, span => span[1]); } } @@ -3626,34 +2265,34 @@ namespace MessagePack.Decoders { internal static readonly IByteDecoder Instance = new InvalidByte(); - InvalidByte() + private InvalidByte() { } - public byte Read(byte[] bytes, int offset, out int readSize) + public byte Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IBytesDecoder { - byte[] Read(byte[] bytes, int offset, out int readSize); + byte[] Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class NilBytes : IBytesDecoder { internal static readonly IBytesDecoder Instance = new NilBytes(); - NilBytes() + private NilBytes() { } - public byte[] Read(byte[] bytes, int offset, out int readSize) + public byte[] Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return null; } } @@ -3662,18 +2301,17 @@ namespace MessagePack.Decoders { internal static readonly IBytesDecoder Instance = new Bin8Bytes(); - Bin8Bytes() + private Bin8Bytes() { } - public byte[] Read(byte[] bytes, int offset, out int readSize) + public byte[] Read(ref ReadOnlySequence<byte> byteSequence) { - var length = bytes[offset + 1]; + var length = MessagePackBinary.Read(ref byteSequence, 2, span => span[1]); var newBytes = new byte[length]; - Buffer.BlockCopy(bytes, offset + 2, newBytes, 0, length); - - readSize = length + 2; + byteSequence.Slice(0, length).CopyTo(newBytes); + byteSequence = byteSequence.Slice(length); return newBytes; } } @@ -3682,18 +2320,17 @@ namespace MessagePack.Decoders { internal static readonly IBytesDecoder Instance = new Bin16Bytes(); - Bin16Bytes() + private Bin16Bytes() { } - public byte[] Read(byte[] bytes, int offset, out int readSize) + public byte[] Read(ref ReadOnlySequence<byte> byteSequence) { - var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]); + var length = MessagePackBinary.Read(ref byteSequence, 3, span => (span[1] << 8) + (span[2])); var newBytes = new byte[length]; - Buffer.BlockCopy(bytes, offset + 3, newBytes, 0, length); - - readSize = length + 3; + byteSequence.Slice(0, length).CopyTo(newBytes); + byteSequence = byteSequence.Slice(length); return newBytes; } } @@ -3702,18 +2339,17 @@ namespace MessagePack.Decoders { internal static readonly IBytesDecoder Instance = new Bin32Bytes(); - Bin32Bytes() + private Bin32Bytes() { } - public byte[] Read(byte[] bytes, int offset, out int readSize) + public byte[] Read(ref ReadOnlySequence<byte> byteSequence) { - var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]); + var length = MessagePackBinary.Read(ref byteSequence, 5, span => (span[1] << 24) | (span[2] << 16) | (span[3] << 8) | (span[4])); var newBytes = new byte[length]; - Buffer.BlockCopy(bytes, offset + 5, newBytes, 0, length); - - readSize = length + 5; + byteSequence.Slice(0, length).CopyTo(newBytes); + byteSequence = byteSequence.Slice(length); return newBytes; } } @@ -3722,34 +2358,34 @@ namespace MessagePack.Decoders { internal static readonly IBytesDecoder Instance = new InvalidBytes(); - InvalidBytes() + private InvalidBytes() { } - public byte[] Read(byte[] bytes, int offset, out int readSize) + public byte[] Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IBytesSegmentDecoder { - ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize); + ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class NilBytesSegment : IBytesSegmentDecoder { internal static readonly IBytesSegmentDecoder Instance = new NilBytesSegment(); - NilBytesSegment() + private NilBytesSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; + byteSequence = byteSequence.Slice(1); return default(ArraySegment<byte>); } } @@ -3758,17 +2394,40 @@ namespace MessagePack.Decoders { internal static readonly IBytesSegmentDecoder Instance = new Bin8BytesSegment(); - Bin8BytesSegment() + private Bin8BytesSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - var length = bytes[offset + 1]; + return Read(ref byteSequence, 2, span => span[1]); + } - readSize = length + 2; - return new ArraySegment<byte>(bytes, offset + 2, length); + internal delegate int SegmentLengthReader(ReadOnlySpan<byte> span); + + internal static ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence, int lengthOfLengthHeader, SegmentLengthReader readLength) + { + Span<byte> lengthHeaderSpan = stackalloc byte[lengthOfLengthHeader]; + byteSequence.Slice(0, lengthOfLengthHeader).CopyTo(lengthHeaderSpan); + byteSequence = byteSequence.Slice(lengthOfLengthHeader); + + int length = readLength(lengthHeaderSpan); + ArraySegment<byte> result; + if (byteSequence.First.Length >= length && MemoryMarshal.TryGetArray(byteSequence.First, out ArraySegment<byte> segment)) + { + // Everything we need to return happens to already be in a single array. Return a segment into that same array. + result = new ArraySegment<byte>(segment.Array, segment.Offset, length); + } + else + { + // We need to create a new array in order to return a continguous segment in our result. + result = new ArraySegment<byte>(new byte[length]); + byteSequence.Slice(0, length).CopyTo(result.Array); + } + + byteSequence = byteSequence.Slice(length); + return result; } } @@ -3776,34 +2435,30 @@ namespace MessagePack.Decoders { internal static readonly IBytesSegmentDecoder Instance = new Bin16BytesSegment(); - Bin16BytesSegment() + private Bin16BytesSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]); - - readSize = length + 3; - return new ArraySegment<byte>(bytes, offset + 3, length); + return Bin8BytesSegment.Read(ref byteSequence, 3, span => (span[1] << 8) + (span[2])); } + } internal sealed class Bin32BytesSegment : IBytesSegmentDecoder { internal static readonly IBytesSegmentDecoder Instance = new Bin32BytesSegment(); - Bin32BytesSegment() + private Bin32BytesSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]); - readSize = length + 5; - return new ArraySegment<byte>(bytes, offset + 5, length); + return Bin8BytesSegment.Read(ref byteSequence, 5, span => (span[1] << 24) | (span[2] << 16) | (span[3] << 8) | (span[4])); } } @@ -3811,35 +2466,36 @@ namespace MessagePack.Decoders { internal static readonly IBytesSegmentDecoder Instance = new InvalidBytesSegment(); - InvalidBytesSegment() + private InvalidBytesSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface ISByteDecoder { - sbyte Read(byte[] bytes, int offset, out int readSize); + sbyte Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixSByte : ISByteDecoder { internal static readonly ISByteDecoder Instance = new FixSByte(); - FixSByte() + private FixSByte() { } - public sbyte Read(byte[] bytes, int offset, out int readSize) + public sbyte Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((sbyte)bytes[offset]); + sbyte result = unchecked((sbyte)byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -3847,15 +2503,14 @@ namespace MessagePack.Decoders { internal static readonly ISByteDecoder Instance = new Int8SByte(); - Int8SByte() + private Int8SByte() { } - public sbyte Read(byte[] bytes, int offset, out int readSize) + public sbyte Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 2; - return unchecked((sbyte)(bytes[offset + 1])); + return MessagePackBinary.Read(ref byteSequence, 2, span => (sbyte)span[1]); } } @@ -3863,34 +2518,34 @@ namespace MessagePack.Decoders { internal static readonly ISByteDecoder Instance = new InvalidSByte(); - InvalidSByte() + private InvalidSByte() { } - public sbyte Read(byte[] bytes, int offset, out int readSize) + public sbyte Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface ISingleDecoder { - float Read(byte[] bytes, int offset, out int readSize); + float Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixNegativeFloat : ISingleDecoder { internal static readonly ISingleDecoder Instance = new FixNegativeFloat(); - FixNegativeFloat() + private FixNegativeFloat() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return FixSByte.Instance.Read(bytes, offset, out readSize); + return FixSByte.Instance.Read(ref byteSequence); } } @@ -3898,14 +2553,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new FixFloat(); - FixFloat() + private FixFloat() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return FixByte.Instance.Read(bytes, offset, out readSize); + return FixByte.Instance.Read(ref byteSequence); } } @@ -3913,14 +2568,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new Int8Single(); - Int8Single() + private Int8Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return Int8SByte.Instance.Read(bytes, offset, out readSize); + return Int8SByte.Instance.Read(ref byteSequence); } } @@ -3928,14 +2583,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new Int16Single(); - Int16Single() + private Int16Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return Int16Int16.Instance.Read(bytes, offset, out readSize); + return Int16Int16.Instance.Read(ref byteSequence); } } @@ -3943,14 +2598,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new Int32Single(); - Int32Single() + private Int32Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return Int32Int32.Instance.Read(bytes, offset, out readSize); + return Int32Int32.Instance.Read(ref byteSequence); } } @@ -3958,14 +2613,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new Int64Single(); - Int64Single() + private Int64Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return Int64Int64.Instance.Read(bytes, offset, out readSize); + return Int64Int64.Instance.Read(ref byteSequence); } } @@ -3974,14 +2629,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new UInt8Single(); - UInt8Single() + private UInt8Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt8Byte.Instance.Read(bytes, offset, out readSize); + return UInt8Byte.Instance.Read(ref byteSequence); } } @@ -3989,14 +2644,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new UInt16Single(); - UInt16Single() + private UInt16Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt16UInt16.Instance.Read(bytes, offset, out readSize); + return UInt16UInt16.Instance.Read(ref byteSequence); } } @@ -4004,14 +2659,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new UInt32Single(); - UInt32Single() + private UInt32Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt32UInt32.Instance.Read(bytes, offset, out readSize); + return UInt32UInt32.Instance.Read(ref byteSequence); } } @@ -4019,14 +2674,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new UInt64Single(); - UInt64Single() + private UInt64Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt64UInt64.Instance.Read(bytes, offset, out readSize); + return UInt64UInt64.Instance.Read(ref byteSequence); } } @@ -4034,15 +2689,14 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new Float32Single(); - Float32Single() + private Float32Single() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 5; - return new Float32Bits(bytes, offset + 1).Value; + return MessagePackBinary.Read(ref byteSequence, 5, span => new Float32Bits(span.Slice(1)).Value); } } @@ -4050,34 +2704,34 @@ namespace MessagePack.Decoders { internal static readonly ISingleDecoder Instance = new InvalidSingle(); - InvalidSingle() + private InvalidSingle() { } - public Single Read(byte[] bytes, int offset, out int readSize) + public Single Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IDoubleDecoder { - double Read(byte[] bytes, int offset, out int readSize); + double Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixNegativeDouble : IDoubleDecoder { internal static readonly IDoubleDecoder Instance = new FixNegativeDouble(); - FixNegativeDouble() + private FixNegativeDouble() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return FixSByte.Instance.Read(bytes, offset, out readSize); + return FixSByte.Instance.Read(ref byteSequence); } } @@ -4085,14 +2739,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new FixDouble(); - FixDouble() + private FixDouble() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return FixByte.Instance.Read(bytes, offset, out readSize); + return FixByte.Instance.Read(ref byteSequence); } } @@ -4100,14 +2754,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new Int8Double(); - Int8Double() + private Int8Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return Int8SByte.Instance.Read(bytes, offset, out readSize); + return Int8SByte.Instance.Read(ref byteSequence); } } @@ -4115,14 +2769,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new Int16Double(); - Int16Double() + private Int16Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return Int16Int16.Instance.Read(bytes, offset, out readSize); + return Int16Int16.Instance.Read(ref byteSequence); } } @@ -4130,14 +2784,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new Int32Double(); - Int32Double() + private Int32Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return Int32Int32.Instance.Read(bytes, offset, out readSize); + return Int32Int32.Instance.Read(ref byteSequence); } } @@ -4145,14 +2799,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new Int64Double(); - Int64Double() + private Int64Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return Int64Int64.Instance.Read(bytes, offset, out readSize); + return Int64Int64.Instance.Read(ref byteSequence); } } @@ -4161,14 +2815,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new UInt8Double(); - UInt8Double() + private UInt8Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt8Byte.Instance.Read(bytes, offset, out readSize); + return UInt8Byte.Instance.Read(ref byteSequence); } } @@ -4176,14 +2830,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new UInt16Double(); - UInt16Double() + private UInt16Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt16UInt16.Instance.Read(bytes, offset, out readSize); + return UInt16UInt16.Instance.Read(ref byteSequence); } } @@ -4191,14 +2845,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new UInt32Double(); - UInt32Double() + private UInt32Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt32UInt32.Instance.Read(bytes, offset, out readSize); + return UInt32UInt32.Instance.Read(ref byteSequence); } } @@ -4206,14 +2860,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new UInt64Double(); - UInt64Double() + private UInt64Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - return UInt64UInt64.Instance.Read(bytes, offset, out readSize); + return UInt64UInt64.Instance.Read(ref byteSequence); } } @@ -4221,15 +2875,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new Float32Double(); - Float32Double() + private Float32Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 5; - return new Float32Bits(bytes, offset + 1).Value; + return MessagePackBinary.Read(ref byteSequence, 5, span => new Float32Bits(span.Slice(1)).Value); } } @@ -4237,15 +2890,14 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new Float64Double(); - Float64Double() + private Float64Double() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 9; - return new Float64Bits(bytes, offset + 1).Value; + return MessagePackBinary.Read(ref byteSequence, 9, span => new Float64Bits(span.Slice(1)).Value); } } @@ -4253,35 +2905,36 @@ namespace MessagePack.Decoders { internal static readonly IDoubleDecoder Instance = new InvalidDouble(); - InvalidDouble() + private InvalidDouble() { } - public Double Read(byte[] bytes, int offset, out int readSize) + public Double Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IInt16Decoder { - Int16 Read(byte[] bytes, int offset, out int readSize); + Int16 Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixNegativeInt16 : IInt16Decoder { internal static readonly IInt16Decoder Instance = new FixNegativeInt16(); - FixNegativeInt16() + private FixNegativeInt16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((short)(sbyte)bytes[offset]); + Int16 result = unchecked((sbyte)byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4289,15 +2942,16 @@ namespace MessagePack.Decoders { internal static readonly IInt16Decoder Instance = new FixInt16(); - FixInt16() + private FixInt16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((short)bytes[offset]); + var result = unchecked(byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4305,15 +2959,14 @@ namespace MessagePack.Decoders { internal static readonly IInt16Decoder Instance = new UInt8Int16(); - UInt8Int16() + private UInt8Int16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 2; - return unchecked((short)(byte)(bytes[offset + 1])); + return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked(span[1])); } } @@ -4321,15 +2974,14 @@ namespace MessagePack.Decoders { internal static readonly IInt16Decoder Instance = new UInt16Int16(); - UInt16Int16() + private UInt16Int16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 3; - return checked((Int16)((bytes[offset + 1] << 8) + (bytes[offset + 2]))); + return MessagePackBinary.Read(ref byteSequence, 5, span => checked((Int16)((span[1] << 8) + (span[2])))); } } @@ -4337,15 +2989,14 @@ namespace MessagePack.Decoders { internal static readonly IInt16Decoder Instance = new Int8Int16(); - Int8Int16() + private Int8Int16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 2; - return unchecked((short)(sbyte)(bytes[offset + 1])); + return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked((sbyte)(span[1]))); } } @@ -4353,18 +3004,14 @@ namespace MessagePack.Decoders { internal static readonly IInt16Decoder Instance = new Int16Int16(); - Int16Int16() + private Int16Int16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 3; - unchecked - { - return (short)((bytes[offset + 1] << 8) | (bytes[offset + 2])); - } + return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((short)((span[1] << 8) | (span[2])))); } } @@ -4372,35 +3019,36 @@ namespace MessagePack.Decoders { internal static readonly IInt16Decoder Instance = new InvalidInt16(); - InvalidInt16() + private InvalidInt16() { } - public Int16 Read(byte[] bytes, int offset, out int readSize) + public Int16 Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IInt32Decoder { - Int32 Read(byte[] bytes, int offset, out int readSize); + Int32 Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixNegativeInt32 : IInt32Decoder { internal static readonly IInt32Decoder Instance = new FixNegativeInt32(); - FixNegativeInt32() + private FixNegativeInt32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((int)(sbyte)bytes[offset]); + var result = unchecked((sbyte)byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4408,15 +3056,16 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new FixInt32(); - FixInt32() + private FixInt32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((int)bytes[offset]); + var result = unchecked(byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4424,30 +3073,28 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new UInt8Int32(); - UInt8Int32() + private UInt8Int32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 2; - return unchecked((int)(byte)(bytes[offset + 1])); + return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked(span[1])); } } internal sealed class UInt16Int32 : IInt32Decoder { internal static readonly IInt32Decoder Instance = new UInt16Int32(); - UInt16Int32() + private UInt16Int32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 3; - return (Int32)((bytes[offset + 1] << 8) | (bytes[offset + 2])); + return MessagePackBinary.Read(ref byteSequence, 3, span => (span[1] << 8) | (span[2])); } } @@ -4455,18 +3102,14 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new UInt32Int32(); - UInt32Int32() + private UInt32Int32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 5; - checked - { - return (Int32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]); - } + return MessagePackBinary.Read(ref byteSequence, 5, span => checked((Int32)((UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4]))); } } @@ -4474,15 +3117,14 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new Int8Int32(); - Int8Int32() + private Int8Int32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 2; - return unchecked((int)(sbyte)(bytes[offset + 1])); + return MessagePackBinary.Read(ref byteSequence, 2, span => unchecked((sbyte)(span[1]))); } } @@ -4490,18 +3132,14 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new Int16Int32(); - Int16Int32() + private Int16Int32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 3; - unchecked - { - return (int)(short)((bytes[offset + 1] << 8) | (bytes[offset + 2])); - } + return MessagePackBinary.Read(ref byteSequence, 3, span => unchecked((short)((span[1] << 8) | (span[2])))); } } @@ -4509,18 +3147,14 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new Int32Int32(); - Int32Int32() + private Int32Int32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 5; - unchecked - { - return (int)((bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | bytes[offset + 4]); - } + return MessagePackBinary.Read(ref byteSequence, 5, span => unchecked((span[1] << 24) | (span[2] << 16) | (span[3] << 8) | span[4])); } } @@ -4528,34 +3162,35 @@ namespace MessagePack.Decoders { internal static readonly IInt32Decoder Instance = new InvalidInt32(); - InvalidInt32() + private InvalidInt32() { } - public Int32 Read(byte[] bytes, int offset, out int readSize) + public Int32 Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IInt64Decoder { - Int64 Read(byte[] bytes, int offset, out int readSize); + Int64 Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixNegativeInt64 : IInt64Decoder { internal static readonly IInt64Decoder Instance = new FixNegativeInt64(); - FixNegativeInt64() + private FixNegativeInt64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((long)(sbyte)bytes[offset]); + var result = unchecked((sbyte)byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4563,15 +3198,16 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new FixInt64(); - FixInt64() + private FixInt64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((long)bytes[offset]); + var result = unchecked(byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4579,30 +3215,30 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new UInt8Int64(); - UInt8Int64() + private UInt8Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - return unchecked((int)(byte)(bytes[offset + 1])); + return unchecked(span[1]); } } internal sealed class UInt16Int64 : IInt64Decoder { internal static readonly IInt64Decoder Instance = new UInt16Int64(); - UInt16Int64() + private UInt16Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 3; - return (Int64)((bytes[offset + 1] << 8) | (bytes[offset + 2])); + return (span[1] << 8) | (span[2]); } } @@ -4610,15 +3246,15 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new UInt32Int64(); - UInt32Int64() + private UInt32Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 5; - return unchecked((Int64)((uint)(bytes[offset + 1] << 24) | ((uint)bytes[offset + 2] << 16) | ((uint)bytes[offset + 3] << 8) | (uint)bytes[offset + 4])); + return unchecked((uint)(span[1] << 24) | ((uint)span[2] << 16) | ((uint)span[3] << 8) | span[4]); } } @@ -4626,18 +3262,18 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new UInt64Int64(); - UInt64Int64() + private UInt64Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 9; checked { - return (Int64)bytes[offset + 1] << 56 | (Int64)bytes[offset + 2] << 48 | (Int64)bytes[offset + 3] << 40 | (Int64)bytes[offset + 4] << 32 - | (Int64)bytes[offset + 5] << 24 | (Int64)bytes[offset + 6] << 16 | (Int64)bytes[offset + 7] << 8 | (Int64)bytes[offset + 8]; + return (Int64)span[1] << 56 | (Int64)span[2] << 48 | (Int64)span[3] << 40 | (Int64)span[4] << 32 + | (Int64)span[5] << 24 | (Int64)span[6] << 16 | (Int64)span[7] << 8 | span[8]; } } } @@ -4647,15 +3283,15 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new Int8Int64(); - Int8Int64() + private Int8Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - return unchecked((long)(sbyte)(bytes[offset + 1])); + return unchecked((sbyte)(span[1])); } } @@ -4663,17 +3299,17 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new Int16Int64(); - Int16Int64() + private Int16Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 3; unchecked { - return (long)(short)((bytes[offset + 1] << 8) | (bytes[offset + 2])); + return (short)((span[1] << 8) | (span[2])); } } } @@ -4682,17 +3318,17 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new Int32Int64(); - Int32Int64() + private Int32Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 5; unchecked { - return (long)((long)(bytes[offset + 1] << 24) + (long)(bytes[offset + 2] << 16) + (long)(bytes[offset + 3] << 8) + (long)bytes[offset + 4]); + return (span[1] << 24) + (long)(span[2] << 16) + (span[3] << 8) + span[4]; } } } @@ -4701,18 +3337,18 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new Int64Int64(); - Int64Int64() + private Int64Int64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 9; unchecked { - return (long)bytes[offset + 1] << 56 | (long)bytes[offset + 2] << 48 | (long)bytes[offset + 3] << 40 | (long)bytes[offset + 4] << 32 - | (long)bytes[offset + 5] << 24 | (long)bytes[offset + 6] << 16 | (long)bytes[offset + 7] << 8 | (long)bytes[offset + 8]; + return (long)span[1] << 56 | (long)span[2] << 48 | (long)span[3] << 40 | (long)span[4] << 32 + | (long)span[5] << 24 | (long)span[6] << 16 | (long)span[7] << 8 | span[8]; } } } @@ -4721,35 +3357,36 @@ namespace MessagePack.Decoders { internal static readonly IInt64Decoder Instance = new InvalidInt64(); - InvalidInt64() + private InvalidInt64() { } - public Int64 Read(byte[] bytes, int offset, out int readSize) + public Int64 Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IUInt16Decoder { - UInt16 Read(byte[] bytes, int offset, out int readSize); + UInt16 Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixUInt16 : IUInt16Decoder { internal static readonly IUInt16Decoder Instance = new FixUInt16(); - FixUInt16() + private FixUInt16() { } - public UInt16 Read(byte[] bytes, int offset, out int readSize) + public UInt16 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((UInt16)bytes[offset]); + var result = unchecked(byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4757,15 +3394,15 @@ namespace MessagePack.Decoders { internal static readonly IUInt16Decoder Instance = new UInt8UInt16(); - UInt8UInt16() + private UInt8UInt16() { } - public UInt16 Read(byte[] bytes, int offset, out int readSize) + public UInt16 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - return unchecked((UInt16)(bytes[offset + 1])); + return unchecked(span[1]); } } @@ -4773,17 +3410,17 @@ namespace MessagePack.Decoders { internal static readonly IUInt16Decoder Instance = new UInt16UInt16(); - UInt16UInt16() + private UInt16UInt16() { } - public UInt16 Read(byte[] bytes, int offset, out int readSize) + public UInt16 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 3; unchecked { - return (UInt16)((bytes[offset + 1] << 8) | (bytes[offset + 2])); + return (UInt16)((span[1] << 8) | (span[2])); } } } @@ -4792,35 +3429,36 @@ namespace MessagePack.Decoders { internal static readonly IUInt16Decoder Instance = new InvalidUInt16(); - InvalidUInt16() + private InvalidUInt16() { } - public UInt16 Read(byte[] bytes, int offset, out int readSize) + public UInt16 Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IUInt32Decoder { - UInt32 Read(byte[] bytes, int offset, out int readSize); + UInt32 Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixUInt32 : IUInt32Decoder { internal static readonly IUInt32Decoder Instance = new FixUInt32(); - FixUInt32() + private FixUInt32() { } - public UInt32 Read(byte[] bytes, int offset, out int readSize) + public UInt32 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((UInt32)bytes[offset]); + var result = unchecked(byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4828,15 +3466,15 @@ namespace MessagePack.Decoders { internal static readonly IUInt32Decoder Instance = new UInt8UInt32(); - UInt8UInt32() + private UInt8UInt32() { } - public UInt32 Read(byte[] bytes, int offset, out int readSize) + public UInt32 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - return unchecked((UInt32)(bytes[offset + 1])); + return unchecked(span[1]); } } @@ -4844,17 +3482,17 @@ namespace MessagePack.Decoders { internal static readonly IUInt32Decoder Instance = new UInt16UInt32(); - UInt16UInt32() + private UInt16UInt32() { } - public UInt32 Read(byte[] bytes, int offset, out int readSize) + public UInt32 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 3; unchecked { - return (UInt32)((bytes[offset + 1] << 8) | (bytes[offset + 2])); + return (UInt32)((span[1] << 8) | (span[2])); } } } @@ -4863,17 +3501,17 @@ namespace MessagePack.Decoders { internal static readonly IUInt32Decoder Instance = new UInt32UInt32(); - UInt32UInt32() + private UInt32UInt32() { } - public UInt32 Read(byte[] bytes, int offset, out int readSize) + public UInt32 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 5; unchecked { - return (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]); + return (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4]; } } } @@ -4882,35 +3520,36 @@ namespace MessagePack.Decoders { internal static readonly IUInt32Decoder Instance = new InvalidUInt32(); - InvalidUInt32() + private InvalidUInt32() { } - public UInt32 Read(byte[] bytes, int offset, out int readSize) + public UInt32 Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IUInt64Decoder { - UInt64 Read(byte[] bytes, int offset, out int readSize); + UInt64 Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixUInt64 : IUInt64Decoder { internal static readonly IUInt64Decoder Instance = new FixUInt64(); - FixUInt64() + private FixUInt64() { } - public UInt64 Read(byte[] bytes, int offset, out int readSize) + public UInt64 Read(ref ReadOnlySequence<byte> byteSequence) { - readSize = 1; - return unchecked((UInt64)bytes[offset]); + var result = unchecked(byteSequence.First.Span[0]); + byteSequence = byteSequence.Slice(1); + return result; } } @@ -4918,15 +3557,15 @@ namespace MessagePack.Decoders { internal static readonly IUInt64Decoder Instance = new UInt8UInt64(); - UInt8UInt64() + private UInt8UInt64() { } - public UInt64 Read(byte[] bytes, int offset, out int readSize) + public UInt64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - return unchecked((UInt64)(bytes[offset + 1])); + return unchecked(span[1]); } } @@ -4934,17 +3573,17 @@ namespace MessagePack.Decoders { internal static readonly IUInt64Decoder Instance = new UInt16UInt64(); - UInt16UInt64() + private UInt16UInt64() { } - public UInt64 Read(byte[] bytes, int offset, out int readSize) + public UInt64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 3; unchecked { - return (UInt64)((bytes[offset + 1] << 8) | (bytes[offset + 2])); + return (UInt64)((span[1] << 8) | (span[2])); } } } @@ -4953,17 +3592,17 @@ namespace MessagePack.Decoders { internal static readonly IUInt64Decoder Instance = new UInt32UInt64(); - UInt32UInt64() + private UInt32UInt64() { } - public UInt64 Read(byte[] bytes, int offset, out int readSize) + public UInt64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 5; unchecked { - return (UInt64)(((UInt64)bytes[offset + 1] << 24) + (ulong)(bytes[offset + 2] << 16) + (UInt64)(bytes[offset + 3] << 8) + (UInt64)bytes[offset + 4]); + return ((UInt64)span[1] << 24) + (ulong)(span[2] << 16) + (UInt64)(span[3] << 8) + span[4]; } } } @@ -4972,18 +3611,18 @@ namespace MessagePack.Decoders { internal static readonly IUInt64Decoder Instance = new UInt64UInt64(); - UInt64UInt64() + private UInt64UInt64() { } - public UInt64 Read(byte[] bytes, int offset, out int readSize) + public UInt64 Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 9; unchecked { - return (UInt64)bytes[offset + 1] << 56 | (UInt64)bytes[offset + 2] << 48 | (UInt64)bytes[offset + 3] << 40 | (UInt64)bytes[offset + 4] << 32 - | (UInt64)bytes[offset + 5] << 24 | (UInt64)bytes[offset + 6] << 16 | (UInt64)bytes[offset + 7] << 8 | (UInt64)bytes[offset + 8]; + return (UInt64)span[1] << 56 | (UInt64)span[2] << 48 | (UInt64)span[3] << 40 | (UInt64)span[4] << 32 + | (UInt64)span[5] << 24 | (UInt64)span[6] << 16 | (UInt64)span[7] << 8 | span[8]; } } } @@ -4992,32 +3631,32 @@ namespace MessagePack.Decoders { internal static readonly IUInt64Decoder Instance = new InvalidUInt64(); - InvalidUInt64() + private InvalidUInt64() { } - public UInt64 Read(byte[] bytes, int offset, out int readSize) + public UInt64 Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IStringDecoder { - String Read(byte[] bytes, int offset, out int readSize); + String Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class NilString : IStringDecoder { internal static readonly IStringDecoder Instance = new NilString(); - NilString() + private NilString() { } - public String Read(byte[] bytes, int offset, out int readSize) + public String Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 1; return null; @@ -5028,14 +3667,14 @@ namespace MessagePack.Decoders { internal static readonly IStringDecoder Instance = new FixString(); - FixString() + private FixString() { } - public String Read(byte[] bytes, int offset, out int readSize) + public String Read(ref ReadOnlySequence<byte> byteSequence) { - var length = bytes[offset] & 0x1F; + var length = span[0] & 0x1F; readSize = length + 1; return StringEncoding.UTF8.GetString(bytes, offset + 1, length); } @@ -5045,14 +3684,14 @@ namespace MessagePack.Decoders { internal static readonly IStringDecoder Instance = new Str8String(); - Str8String() + private Str8String() { } - public String Read(byte[] bytes, int offset, out int readSize) + public String Read(ref ReadOnlySequence<byte> byteSequence) { - var length = (int)bytes[offset + 1]; + var length = (int)span[1]; readSize = length + 2; return StringEncoding.UTF8.GetString(bytes, offset + 2, length); } @@ -5062,16 +3701,16 @@ namespace MessagePack.Decoders { internal static readonly IStringDecoder Instance = new Str16String(); - Str16String() + private Str16String() { } - public String Read(byte[] bytes, int offset, out int readSize) + public String Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]); + var length = (span[1] << 8) + (span[2]); readSize = length + 3; return StringEncoding.UTF8.GetString(bytes, offset + 3, length); } @@ -5082,16 +3721,16 @@ namespace MessagePack.Decoders { internal static readonly IStringDecoder Instance = new Str32String(); - Str32String() + private Str32String() { } - public String Read(byte[] bytes, int offset, out int readSize) + public String Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]); + var length = (int)((uint)(span[1] << 24) | (uint)(span[2] << 16) | (uint)(span[3] << 8) | span[4]); readSize = length + 5; return StringEncoding.UTF8.GetString(bytes, offset + 5, length); } @@ -5102,32 +3741,32 @@ namespace MessagePack.Decoders { internal static readonly IStringDecoder Instance = new InvalidString(); - InvalidString() + private InvalidString() { } - public String Read(byte[] bytes, int offset, out int readSize) + public String Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IStringSegmentDecoder { - ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize); + ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class NilStringSegment : IStringSegmentDecoder { internal static readonly IStringSegmentDecoder Instance = new NilStringSegment(); - NilStringSegment() + private NilStringSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 1; return new ArraySegment<byte>(bytes, offset, 1); @@ -5138,14 +3777,14 @@ namespace MessagePack.Decoders { internal static readonly IStringSegmentDecoder Instance = new FixStringSegment(); - FixStringSegment() + private FixStringSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - var length = bytes[offset] & 0x1F; + var length = span[0] & 0x1F; readSize = length + 1; return new ArraySegment<byte>(bytes, offset + 1, length); } @@ -5155,14 +3794,14 @@ namespace MessagePack.Decoders { internal static readonly IStringSegmentDecoder Instance = new Str8StringSegment(); - Str8StringSegment() + private Str8StringSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - var length = (int)bytes[offset + 1]; + var length = (int)span[1]; readSize = length + 2; return new ArraySegment<byte>(bytes, offset + 2, length); } @@ -5172,16 +3811,16 @@ namespace MessagePack.Decoders { internal static readonly IStringSegmentDecoder Instance = new Str16StringSegment(); - Str16StringSegment() + private Str16StringSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]); + var length = (span[1] << 8) + (span[2]); readSize = length + 3; return new ArraySegment<byte>(bytes, offset + 3, length); } @@ -5192,16 +3831,16 @@ namespace MessagePack.Decoders { internal static readonly IStringSegmentDecoder Instance = new Str32StringSegment(); - Str32StringSegment() + private Str32StringSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]); + var length = (int)((uint)(span[1] << 24) | (uint)(span[2] << 16) | (uint)(span[3] << 8) | span[4]); readSize = length + 5; return new ArraySegment<byte>(bytes, offset + 5, length); } @@ -5212,36 +3851,36 @@ namespace MessagePack.Decoders { internal static readonly IStringSegmentDecoder Instance = new InvalidStringSegment(); - InvalidStringSegment() + private InvalidStringSegment() { } - public ArraySegment<byte> Read(byte[] bytes, int offset, out int readSize) + public ArraySegment<byte> Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IExtDecoder { - ExtensionResult Read(byte[] bytes, int offset, out int readSize); + ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixExt1 : IExtDecoder { internal static readonly IExtDecoder Instance = new FixExt1(); - FixExt1() + private FixExt1() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 3; - var typeCode = unchecked((sbyte)bytes[offset + 1]); - var body = new byte[1] { bytes[offset + 2] }; // make new bytes is overhead? + var typeCode = unchecked((sbyte)span[1]); + var body = new byte[1] { span[2] }; // make new bytes is overhead? return new ExtensionResult(typeCode, body); } } @@ -5250,19 +3889,19 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new FixExt2(); - FixExt2() + private FixExt2() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 4; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); var body = new byte[2] { - bytes[offset + 2], - bytes[offset + 3], + span[2], + span[3], }; return new ExtensionResult(typeCode, body); } @@ -5272,21 +3911,21 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new FixExt4(); - FixExt4() + private FixExt4() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 6; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); var body = new byte[4] { - bytes[offset + 2], - bytes[offset + 3], - bytes[offset + 4], - bytes[offset + 5], + span[2], + span[3], + span[4], + span[5], }; return new ExtensionResult(typeCode, body); } @@ -5296,25 +3935,25 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new FixExt8(); - FixExt8() + private FixExt8() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 10; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); var body = new byte[8] { - bytes[offset + 2], - bytes[offset + 3], - bytes[offset + 4], - bytes[offset + 5], - bytes[offset + 6], - bytes[offset + 7], - bytes[offset + 8], - bytes[offset + 9], + span[2], + span[3], + span[4], + span[5], + span[6], + span[7], + span[8], + span[9], }; return new ExtensionResult(typeCode, body); } @@ -5324,33 +3963,33 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new FixExt16(); - FixExt16() + private FixExt16() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 18; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); var body = new byte[16] { - bytes[offset + 2], - bytes[offset + 3], - bytes[offset + 4], - bytes[offset + 5], - bytes[offset + 6], - bytes[offset + 7], - bytes[offset + 8], - bytes[offset + 9], - bytes[offset + 10], - bytes[offset + 11], - bytes[offset + 12], - bytes[offset + 13], - bytes[offset + 14], - bytes[offset + 15], - bytes[offset + 16], - bytes[offset + 17] + span[2], + span[3], + span[4], + span[5], + span[6], + span[7], + span[8], + span[9], + span[10], + span[11], + span[12], + span[13], + span[14], + span[15], + span[16], + span[17] }; return new ExtensionResult(typeCode, body); } @@ -5360,21 +3999,21 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new Ext8(); - Ext8() + private Ext8() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = bytes[offset + 1]; - var typeCode = unchecked((sbyte)bytes[offset + 2]); + var length = span[1]; + var typeCode = unchecked((sbyte)span[2]); var body = new byte[length]; - readSize = (int)length + 3; - Buffer.BlockCopy(bytes, offset + 3, body, 0, (int)length); + readSize = length + 3; + Buffer.BlockCopy(bytes, offset + 3, body, 0, length); return new ExtensionResult(typeCode, body); } } @@ -5384,21 +4023,21 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new Ext16(); - Ext16() + private Ext16() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (int)((UInt16)(bytes[offset + 1] << 8) | (UInt16)bytes[offset + 2]); - var typeCode = unchecked((sbyte)bytes[offset + 3]); + var length = (UInt16)(span[1] << 8) | span[2]; + var typeCode = unchecked((sbyte)span[3]); var body = new byte[length]; readSize = length + 4; - Buffer.BlockCopy(bytes, offset + 4, body, 0, (int)length); + Buffer.BlockCopy(bytes, offset + 4, body, 0, length); return new ExtensionResult(typeCode, body); } } @@ -5408,17 +4047,17 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new Ext32(); - Ext32() + private Ext32() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]); - var typeCode = unchecked((sbyte)bytes[offset + 5]); + var length = (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4]; + var typeCode = unchecked((sbyte)span[5]); var body = new byte[length]; checked @@ -5435,14 +4074,14 @@ namespace MessagePack.Decoders { internal static readonly IExtDecoder Instance = new InvalidExt(); - InvalidExt() + private InvalidExt() { } - public ExtensionResult Read(byte[] bytes, int offset, out int readSize) + public ExtensionResult Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } @@ -5453,22 +4092,22 @@ namespace MessagePack.Decoders internal interface IExtHeaderDecoder { - ExtensionHeader Read(byte[] bytes, int offset, out int readSize); + ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixExt1Header : IExtHeaderDecoder { internal static readonly IExtHeaderDecoder Instance = new FixExt1Header(); - FixExt1Header() + private FixExt1Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); return new ExtensionHeader(typeCode, 1); } } @@ -5477,15 +4116,15 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new FixExt2Header(); - FixExt2Header() + private FixExt2Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); return new ExtensionHeader(typeCode, 2); } } @@ -5494,15 +4133,15 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new FixExt4Header(); - FixExt4Header() + private FixExt4Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); return new ExtensionHeader(typeCode, 4); } } @@ -5511,15 +4150,15 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new FixExt8Header(); - FixExt8Header() + private FixExt8Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); return new ExtensionHeader(typeCode, 8); } } @@ -5528,15 +4167,15 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new FixExt16Header(); - FixExt16Header() + private FixExt16Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { readSize = 2; - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); return new ExtensionHeader(typeCode, 16); } } @@ -5545,17 +4184,17 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new Ext8Header(); - Ext8Header() + private Ext8Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = bytes[offset + 1]; - var typeCode = unchecked((sbyte)bytes[offset + 2]); + var length = span[1]; + var typeCode = unchecked((sbyte)span[2]); readSize = 3; return new ExtensionHeader(typeCode, length); @@ -5567,17 +4206,17 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new Ext16Header(); - Ext16Header() + private Ext16Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (UInt32)((UInt16)(bytes[offset + 1] << 8) | (UInt16)bytes[offset + 2]); - var typeCode = unchecked((sbyte)bytes[offset + 3]); + var length = (UInt32)((UInt16)(span[1] << 8) | span[2]); + var typeCode = unchecked((sbyte)span[3]); readSize = 4; return new ExtensionHeader(typeCode, length); @@ -5589,17 +4228,17 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new Ext32Header(); - Ext32Header() + private Ext32Header() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { unchecked { - var length = (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]); - var typeCode = unchecked((sbyte)bytes[offset + 5]); + var length = (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4]; + var typeCode = unchecked((sbyte)span[5]); readSize = 6; return new ExtensionHeader(typeCode, length); @@ -5611,34 +4250,34 @@ namespace MessagePack.Decoders { internal static readonly IExtHeaderDecoder Instance = new InvalidExtHeader(); - InvalidExtHeader() + private InvalidExtHeader() { } - public ExtensionHeader Read(byte[] bytes, int offset, out int readSize) + public ExtensionHeader Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IDateTimeDecoder { - DateTime Read(byte[] bytes, int offset, out int readSize); + DateTime Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class FixExt4DateTime : IDateTimeDecoder { internal static readonly IDateTimeDecoder Instance = new FixExt4DateTime(); - FixExt4DateTime() + private FixExt4DateTime() { } - public DateTime Read(byte[] bytes, int offset, out int readSize) + public DateTime Read(ref ReadOnlySequence<byte> byteSequence) { - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); if (typeCode != ReservedMessagePackExtensionTypeCode.DateTime) { throw new InvalidOperationException(string.Format("typeCode is invalid. typeCode:{0}", typeCode)); @@ -5646,7 +4285,7 @@ namespace MessagePack.Decoders unchecked { - var seconds = (UInt32)((UInt32)(bytes[offset + 2] << 24) | (UInt32)(bytes[offset + 3] << 16) | (UInt32)(bytes[offset + 4] << 8) | (UInt32)bytes[offset + 5]); + var seconds = (UInt32)(span[2] << 24) | (UInt32)(span[3] << 16) | (UInt32)(span[4] << 8) | span[5]; readSize = 6; return DateTimeConstants.UnixEpoch.AddSeconds(seconds); @@ -5658,21 +4297,21 @@ namespace MessagePack.Decoders { internal static readonly IDateTimeDecoder Instance = new FixExt8DateTime(); - FixExt8DateTime() + private FixExt8DateTime() { } - public DateTime Read(byte[] bytes, int offset, out int readSize) + public DateTime Read(ref ReadOnlySequence<byte> byteSequence) { - var typeCode = unchecked((sbyte)bytes[offset + 1]); + var typeCode = unchecked((sbyte)span[1]); if (typeCode != ReservedMessagePackExtensionTypeCode.DateTime) { throw new InvalidOperationException(string.Format("typeCode is invalid. typeCode:{0}", typeCode)); } - var data64 = (UInt64)bytes[offset + 2] << 56 | (UInt64)bytes[offset + 3] << 48 | (UInt64)bytes[offset + 4] << 40 | (UInt64)bytes[offset + 5] << 32 - | (UInt64)bytes[offset + 6] << 24 | (UInt64)bytes[offset + 7] << 16 | (UInt64)bytes[offset + 8] << 8 | (UInt64)bytes[offset + 9]; + var data64 = (UInt64)span[2] << 56 | (UInt64)span[3] << 48 | (UInt64)span[4] << 40 | (UInt64)span[5] << 32 + | (UInt64)span[6] << 24 | (UInt64)span[7] << 16 | (UInt64)span[8] << 8 | span[9]; var nanoseconds = (long)(data64 >> 34); var seconds = data64 & 0x00000003ffffffffL; @@ -5686,25 +4325,25 @@ namespace MessagePack.Decoders { internal static readonly IDateTimeDecoder Instance = new Ext8DateTime(); - Ext8DateTime() + private Ext8DateTime() { } - public DateTime Read(byte[] bytes, int offset, out int readSize) + public DateTime Read(ref ReadOnlySequence<byte> byteSequence) { - var length = checked((byte)bytes[offset + 1]); - var typeCode = unchecked((sbyte)bytes[offset + 2]); + var length = checked(span[1]); + var typeCode = unchecked((sbyte)span[2]); if (length != 12 || typeCode != ReservedMessagePackExtensionTypeCode.DateTime) { throw new InvalidOperationException(string.Format("typeCode is invalid. typeCode:{0}", typeCode)); } - var nanoseconds = (UInt32)((UInt32)(bytes[offset + 3] << 24) | (UInt32)(bytes[offset + 4] << 16) | (UInt32)(bytes[offset + 5] << 8) | (UInt32)bytes[offset + 6]); + var nanoseconds = (UInt32)(span[3] << 24) | (UInt32)(span[4] << 16) | (UInt32)(span[5] << 8) | span[6]; unchecked { - var seconds = (long)bytes[offset + 7] << 56 | (long)bytes[offset + 8] << 48 | (long)bytes[offset + 9] << 40 | (long)bytes[offset + 10] << 32 - | (long)bytes[offset + 11] << 24 | (long)bytes[offset + 12] << 16 | (long)bytes[offset + 13] << 8 | (long)bytes[offset + 14]; + var seconds = (long)span[7] << 56 | (long)span[8] << 48 | (long)span[9] << 40 | (long)span[10] << 32 + | (long)span[11] << 24 | (long)span[12] << 16 | (long)span[13] << 8 | span[14]; readSize = 15; return DateTimeConstants.UnixEpoch.AddSeconds(seconds).AddTicks(nanoseconds / DateTimeConstants.NanosecondsPerTick); @@ -5716,26 +4355,27 @@ namespace MessagePack.Decoders { internal static readonly IDateTimeDecoder Instance = new InvalidDateTime(); - InvalidDateTime() + private InvalidDateTime() { } - public DateTime Read(byte[] bytes, int offset, out int readSize) + public DateTime Read(ref ReadOnlySequence<byte> byteSequence) { - throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset]))); + throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", byteSequence.First.Span[0], MessagePackCode.ToFormatName(byteSequence.First.Span[0]))); } } internal interface IReadNextDecoder { - int Read(byte[] bytes, int offset); + void Read(ref ReadOnlySequence<byte> byteSequence); } internal sealed class ReadNext1 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext1(); - ReadNext1() + + private ReadNext1() { } @@ -5745,7 +4385,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext2 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext2(); - ReadNext2() + + private ReadNext2() { } @@ -5755,7 +4396,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext3 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext3(); - ReadNext3() + + private ReadNext3() { } @@ -5764,7 +4406,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext4 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext4(); - ReadNext4() + + private ReadNext4() { } @@ -5773,7 +4416,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext5 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext5(); - ReadNext5() + + private ReadNext5() { } @@ -5782,7 +4426,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext6 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext6(); - ReadNext6() + + private ReadNext6() { } @@ -5792,7 +4437,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext9 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext9(); - ReadNext9() + + private ReadNext9() { } @@ -5801,7 +4447,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext10 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext10(); - ReadNext10() + + private ReadNext10() { } @@ -5810,7 +4457,8 @@ namespace MessagePack.Decoders internal sealed class ReadNext18 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNext18(); - ReadNext18() + + private ReadNext18() { } @@ -5820,7 +4468,8 @@ namespace MessagePack.Decoders internal sealed class ReadNextMap : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextMap(); - ReadNextMap() + + private ReadNextMap() { } @@ -5828,7 +4477,7 @@ namespace MessagePack.Decoders { var startOffset = offset; int readSize; - var length = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize); + var length = MessagePackBinary.ReadMapHeader(ref byteSequence); offset += readSize; for (int i = 0; i < length; i++) { @@ -5842,7 +4491,8 @@ namespace MessagePack.Decoders internal sealed class ReadNextArray : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextArray(); - ReadNextArray() + + private ReadNextArray() { } @@ -5850,7 +4500,7 @@ namespace MessagePack.Decoders { var startOffset = offset; int readSize; - var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize); + var length = MessagePackBinary.ReadArrayHeader(ref byteSequence); offset += readSize; for (int i = 0; i < length; i++) { @@ -5863,13 +4513,14 @@ namespace MessagePack.Decoders internal sealed class ReadNextFixStr : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextFixStr(); - ReadNextFixStr() + + private ReadNextFixStr() { } public int Read(byte[] bytes, int offset) { - var length = bytes[offset] & 0x1F; + var length = span[0] & 0x1F; return length + 1; } } @@ -5877,13 +4528,14 @@ namespace MessagePack.Decoders internal sealed class ReadNextStr8 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextStr8(); - ReadNextStr8() + + private ReadNextStr8() { } public int Read(byte[] bytes, int offset) { - var length = (int)bytes[offset + 1]; + var length = (int)span[1]; return length + 2; } } @@ -5891,14 +4543,15 @@ namespace MessagePack.Decoders internal sealed class ReadNextStr16 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextStr16(); - ReadNextStr16() + + private ReadNextStr16() { } public int Read(byte[] bytes, int offset) { - var length = (bytes[offset + 1] << 8) | (bytes[offset + 2]); + var length = (span[1] << 8) | (span[2]); return length + 3; } } @@ -5906,13 +4559,14 @@ namespace MessagePack.Decoders internal sealed class ReadNextStr32 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextStr32(); - ReadNextStr32() + + private ReadNextStr32() { } public int Read(byte[] bytes, int offset) { - var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]); + var length = (int)((uint)(span[1] << 24) | (uint)(span[2] << 16) | (uint)(span[3] << 8) | span[4]); return length + 5; } } @@ -5920,13 +4574,14 @@ namespace MessagePack.Decoders internal sealed class ReadNextBin8 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextBin8(); - ReadNextBin8() + + private ReadNextBin8() { } public int Read(byte[] bytes, int offset) { - var length = bytes[offset + 1]; + var length = span[1]; return length + 2; } } @@ -5934,14 +4589,15 @@ namespace MessagePack.Decoders internal sealed class ReadNextBin16 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextBin16(); - ReadNextBin16() + + private ReadNextBin16() { } public int Read(byte[] bytes, int offset) { - var length = (bytes[offset + 1] << 8) | (bytes[offset + 2]); + var length = (span[1] << 8) | (span[2]); return length + 3; } } @@ -5949,13 +4605,14 @@ namespace MessagePack.Decoders internal sealed class ReadNextBin32 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextBin32(); - ReadNextBin32() + + private ReadNextBin32() { } public int Read(byte[] bytes, int offset) { - var length = (bytes[offset + 1] << 24) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 8) | (bytes[offset + 4]); + var length = (span[1] << 24) | (span[2] << 16) | (span[3] << 8) | (span[4]); return length + 5; } } @@ -5963,27 +4620,29 @@ namespace MessagePack.Decoders internal sealed class ReadNextExt8 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextExt8(); - ReadNextExt8() + + private ReadNextExt8() { } public int Read(byte[] bytes, int offset) { - var length = bytes[offset + 1]; - return (int)length + 3; + var length = span[1]; + return length + 3; } } internal sealed class ReadNextExt16 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextExt16(); - ReadNextExt16() + + private ReadNextExt16() { } public int Read(byte[] bytes, int offset) { - var length = (int)((UInt16)(bytes[offset + 1] << 8) | (UInt16)bytes[offset + 2]); + var length = (UInt16)(span[1] << 8) | span[2]; return length + 4; } } @@ -5991,14 +4650,15 @@ namespace MessagePack.Decoders internal sealed class ReadNextExt32 : IReadNextDecoder { internal static readonly IReadNextDecoder Instance = new ReadNextExt32(); - ReadNextExt32() + + private ReadNextExt32() { } public int Read(byte[] bytes, int offset) { - var length = (UInt32)((UInt32)(bytes[offset + 1] << 24) | (UInt32)(bytes[offset + 2] << 16) | (UInt32)(bytes[offset + 3] << 8) | (UInt32)bytes[offset + 4]); + var length = (UInt32)(span[1] << 24) | (UInt32)(span[2] << 16) | (UInt32)(span[3] << 8) | span[4]; return (int)length + 6; } } -}
\ No newline at end of file +} diff --git a/src/MessagePack/MessagePackSerializer+Typeless.cs b/src/MessagePack/MessagePackSerializer+Typeless.cs index f10939a6..92b14eec 100644 --- a/src/MessagePack/MessagePackSerializer+Typeless.cs +++ b/src/MessagePack/MessagePackSerializer+Typeless.cs @@ -2,6 +2,7 @@ using System; using System.IO; +using System.Threading.Tasks; namespace MessagePack { @@ -34,11 +35,9 @@ namespace MessagePack public object Deserialize(Stream stream) => serializer.Deserialize<object>(stream); - public object Deserialize(Stream stream, bool readStrict) => serializer.Deserialize<object>(stream, readStrict); - public object Deserialize(ArraySegment<byte> bytes) => serializer.Deserialize<object>(bytes); - public System.Threading.Tasks.Task<object> DeserializeAsync(Stream stream) => serializer.DeserializeAsync<object>(stream); + public ValueTask<object> DeserializeAsync(Stream stream) => serializer.DeserializeAsync<object>(stream); } } } diff --git a/src/MessagePack/MessagePackSerializer.cs b/src/MessagePack/MessagePackSerializer.cs index b11efad4..b48ff70c 100644 --- a/src/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack/MessagePackSerializer.cs @@ -1,6 +1,8 @@ -using MessagePack.Internal; -using System; +using System; +using System.Buffers; using System.IO; +using System.Threading.Tasks; +using MessagePack.Internal; namespace MessagePack { @@ -42,233 +44,162 @@ namespace MessagePack } /// <summary> - /// Serialize to binary. + /// Serializes a given value with the specified buffer writer. /// </summary> - public byte[] Serialize<T>(T obj) => this.Serialize<T>(obj, this.DefaultResolver); - - /// <summary> - /// Serialize to binary. - /// </summary> - public virtual byte[] Serialize<T>(T obj, IFormatterResolver resolver) + /// <param name="writer">The buffer writer to serialize with.</param> + /// <param name="value">The value to serialize.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + public virtual void Serialize<T>(IBufferWriter<byte> writer, T value, IFormatterResolver resolver) { - if (resolver == null) resolver = DefaultResolver; - var formatter = resolver.GetFormatterWithVerify<T>(); - - var buffer = InternalMemoryPool.GetBuffer(); + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } - var len = formatter.Serialize(ref buffer, 0, obj, resolver); + if (resolver == null) + { + resolver = this.DefaultResolver; + } - // do not return MemoryPool.Buffer. - return MessagePackBinary.FastCloneWithResize(buffer, len); + var formatter = resolver.GetFormatterWithVerify<T>(); + formatter.Serialize(writer, value, resolver); } /// <summary> - /// Serialize to binary. Get the raw memory pool byte[]. The result can not share across thread and can not hold, so use quickly. + /// Deserializes a value of a given type from a sequence of bytes. /// </summary> - protected ArraySegment<byte> SerializeUnsafe<T>(T obj, IFormatterResolver resolver) + /// <typeparam name="T">The type of value to deserialize.</typeparam> + /// <param name="byteSequence">The sequence to deserialize from.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <param name="endPosition">The position in the <paramref name="byteSequence"/> just after the last read byte.</param> + /// <returns>The deserialized value.</returns> + public virtual T Deserialize<T>(ReadOnlySequence<byte> byteSequence, IFormatterResolver resolver, out SequencePosition endPosition) { - if (resolver == null) resolver = DefaultResolver; - var formatter = resolver.GetFormatterWithVerify<T>(); - - var buffer = InternalMemoryPool.GetBuffer(); - - var len = formatter.Serialize(ref buffer, 0, obj, resolver); + if (resolver == null) + { + resolver = this.DefaultResolver; + } - // return raw memory pool, unsafe! - return new ArraySegment<byte>(buffer, 0, len); + return resolver.GetFormatterWithVerify<T>().Deserialize(byteSequence, resolver, out endPosition); } /// <summary> - /// Serialize to stream. + /// Serializes a given value with the specified buffer writer. /// </summary> - public void Serialize<T>(Stream stream, T obj) => this.Serialize<T>(stream, obj, DefaultResolver); + /// <param name="value">The value to serialize.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <returns>A byte array with the serialized value.</returns> + public byte[] Serialize<T>(T value, IFormatterResolver resolver = null) + { + throw new NotImplementedException(); + } /// <summary> - /// Serialize to stream. + /// Serializes a given value to the specified stream. /// </summary> - public virtual void Serialize<T>(Stream stream, T obj, IFormatterResolver resolver) + /// <param name="stream">The stream to serialize to.</param> + /// <param name="value">The value to serialize.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + public void Serialize<T>(Stream stream, T value, IFormatterResolver resolver = null) { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } - - if (resolver == null) resolver = DefaultResolver; - var formatter = resolver.GetFormatterWithVerify<T>(); - - var buffer = InternalMemoryPool.GetBuffer(); - - var len = formatter.Serialize(ref buffer, 0, obj, resolver); - - // do not need resize. - stream.Write(buffer, 0, len); + throw new NotImplementedException(); } /// <summary> - /// Reflect of resolver.GetFormatterWithVerify[T].Serialize. + /// Serializes a given value to the specified stream. /// </summary> - public int Serialize<T>(ref byte[] bytes, int offset, T value, IFormatterResolver resolver) + /// <param name="stream">The stream to serialize to.</param> + /// <param name="value">The value to serialize.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <returns>A task that completes with the result of the async serialization operation.</returns> + public Task SerializeAsync<T>(Stream stream, T value, IFormatterResolver resolver = null) { - if (resolver == null) resolver = DefaultResolver; - return resolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value, resolver); + throw new NotImplementedException(); } -#if NETSTANDARD || NETFRAMEWORK - /// <summary> - /// Serialize to stream(async). + /// Deserializes a value of a given type from a sequence of bytes. /// </summary> - public System.Threading.Tasks.Task SerializeAsync<T>(Stream stream, T obj) => SerializeAsync<T>(stream, obj, DefaultResolver); + /// <typeparam name="T">The type of value to deserialize.</typeparam> + /// <param name="buffer">The buffer to deserialize from.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <returns>The deserialized value.</returns> + public T Deserialize<T>(Memory<byte> buffer, IFormatterResolver resolver = null) => this.Deserialize<T>(new ReadOnlySequence<byte>(buffer), resolver, out SequencePosition endPosition); /// <summary> - /// Serialize to stream(async). + /// Deserializes a value of a given type from a sequence of bytes. /// </summary> - public async System.Threading.Tasks.Task SerializeAsync<T>(Stream stream, T obj, IFormatterResolver resolver) + /// <typeparam name="T">The type of value to deserialize.</typeparam> + /// <param name="buffer">The array to deserialize from.</param> + /// <param name="offset">The position in the array to start deserialization.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <returns>The deserialized value.</returns> + public T Deserialize<T>(byte[] buffer, int offset = 0, IFormatterResolver resolver = null) { - if (resolver == null) resolver = DefaultResolver; - var formatter = resolver.GetFormatterWithVerify<T>(); - - var rentBuffer = BufferPool.Default.Rent(); - try - { - var buffer = rentBuffer; - var len = formatter.Serialize(ref buffer, 0, obj, resolver); - - // do not need resize. - await stream.WriteAsync(buffer, 0, len).ConfigureAwait(false); - } - finally + if (buffer == null) { - BufferPool.Default.Return(rentBuffer); + throw new ArgumentNullException(nameof(buffer)); } - } - -#endif - - public T Deserialize<T>(byte[] bytes) => Deserialize<T>(bytes, DefaultResolver); - public T Deserialize<T>(byte[] bytes, IFormatterResolver resolver) => Deserialize<T>(new ArraySegment<byte>(bytes), resolver); - - public T Deserialize<T>(ArraySegment<byte> bytes) => Deserialize<T>(bytes, DefaultResolver); - - public T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver) => Deserialize<T>(bytes, resolver, out int readSize); - - public virtual T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver, out int readSize) - { - if (resolver == null) resolver = DefaultResolver; - return resolver.GetFormatterWithVerify<T>().Deserialize(bytes.Array, bytes.Offset, resolver, out readSize); + var sequence = new ReadOnlySequence<byte>(buffer.AsMemory(offset)); + return Deserialize<T>(sequence, resolver, out SequencePosition endPosition); } - public T Deserialize<T>(Stream stream) => Deserialize<T>(stream, DefaultResolver, readStrict: false); - - public T Deserialize<T>(Stream stream, IFormatterResolver resolver) => Deserialize<T>(stream, resolver, readStrict: false); - - public T Deserialize<T>(Stream stream, bool readStrict) => Deserialize<T>(stream, DefaultResolver, readStrict); - - public virtual T Deserialize<T>(Stream stream, IFormatterResolver resolver, bool readStrict) + /// <summary> + /// Deserializes a value of a given type from a sequence of bytes. + /// </summary> + /// <typeparam name="T">The type of value to deserialize.</typeparam> + /// <param name="buffer">The array to deserialize from.</param> + /// <param name="offset">The position in the array to start deserialization.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <param name="bytesRead">The number of bytes read.</param> + /// <returns>The deserialized value.</returns> + public T Deserialize<T>(byte[] buffer, int offset, IFormatterResolver resolver, out int bytesRead) { - if (resolver == null) resolver = DefaultResolver; - var formatter = resolver.GetFormatterWithVerify<T>(); - - if (!readStrict) - { -#if NETSTANDARD && !NET45 - - var ms = stream as MemoryStream; - if (ms != null) - { - // optimize for MemoryStream - ArraySegment<byte> buffer; - if (ms.TryGetBuffer(out buffer)) - { - int readSize; - return formatter.Deserialize(buffer.Array, buffer.Offset, resolver, out readSize); - } - } -#endif - - // no else. - { - var buffer = InternalMemoryPool.GetBuffer(); - - FillFromStream(stream, ref buffer); - - int readSize; - return formatter.Deserialize(buffer, 0, resolver, out readSize); - } - } - else + if (buffer == null) { - int _; - var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out _); - int readSize; - return formatter.Deserialize(bytes, 0, resolver, out readSize); + throw new ArgumentNullException(nameof(buffer)); } + + var sequence = new ReadOnlySequence<byte>(buffer.AsMemory(offset)); + T result = Deserialize<T>(sequence, resolver, out SequencePosition endPosition); + bytesRead = endPosition.GetInteger(); // we know the sequence has just one segment, so this is safe. + return result; } /// <summary> - /// Reflect of resolver.GetFormatterWithVerify[T].Deserialize. + /// Deserializes a value of a given type from a stream. /// </summary> - public T Deserialize<T>(byte[] bytes, int offset, IFormatterResolver resolver, out int readSize) => Deserialize<T>(new ArraySegment<byte>(bytes, offset, bytes.Length - offset), resolver, out readSize); - -#if NETSTANDARD || NETFRAMEWORK - - public System.Threading.Tasks.Task<T> DeserializeAsync<T>(Stream stream) => DeserializeAsync<T>(stream, DefaultResolver); - - // readStrict async read is too slow(many Task garbage) so I don't provide async option. - - public async System.Threading.Tasks.Task<T> DeserializeAsync<T>(Stream stream, IFormatterResolver resolver) + /// <typeparam name="T">The type of value to deserialize.</typeparam> + /// <param name="stream">The stream to deserialize from.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <returns>The deserialized value.</returns> + public T Deserialize<T>(Stream stream, IFormatterResolver resolver = null) { - if (resolver == null) resolver = DefaultResolver; - var rentBuffer = BufferPool.Default.Rent(); - var buf = rentBuffer; - try - { - int length = 0; - int read; - while ((read = await stream.ReadAsync(buf, length, buf.Length - length).ConfigureAwait(false)) > 0) - { - length += read; - if (length == buf.Length) - { - MessagePackBinary.FastResize(ref buf, length * 2); - } - } - - return Deserialize<T>(buf, resolver); - } - finally - { - BufferPool.Default.Return(rentBuffer); - } + throw new NotImplementedException(); } -#endif - - static int FillFromStream(Stream input, ref byte[] buffer) + /// <summary> + /// Deserializes a value of a given type from a stream. + /// </summary> + /// <typeparam name="T">The type of value to deserialize.</typeparam> + /// <param name="stream">The stream to deserialize from.</param> + /// <param name="resolver">The resolver to use during deserialization. Use <c>null</c> to use the <see cref="DefaultResolver"/>.</param> + /// <returns>The deserialized value.</returns> + public ValueTask<T> DeserializeAsync<T>(Stream stream, IFormatterResolver resolver = null) { - int length = 0; - int read; - while ((read = input.Read(buffer, length, buffer.Length - length)) > 0) - { - length += read; - if (length == buffer.Length) - { - MessagePackBinary.FastResize(ref buffer, length * 2); - } - } - - return length; + throw new NotImplementedException(); } } } namespace MessagePack.Internal { + // TODO: remove this? internal static class InternalMemoryPool { [ThreadStatic] - static byte[] buffer = null; + private static byte[] buffer = null; public static byte[] GetBuffer() { diff --git a/src/MessagePack/Nil.cs b/src/MessagePack/Nil.cs index ea74f6ae..b457fd00 100644 --- a/src/MessagePack/Nil.cs +++ b/src/MessagePack/Nil.cs @@ -39,7 +39,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Nil value, IFormatterResolver typeResolver) + public void Serialize(IBufferWriter<byte> writer, Nil value, IFormatterResolver typeResolver) { return MessagePackBinary.WriteNil(ref bytes, offset); } @@ -60,7 +60,7 @@ namespace MessagePack.Formatters } - public int Serialize(ref byte[] bytes, int offset, Nil? value, IFormatterResolver typeResolver) + public void Serialize(IBufferWriter<byte> writer, Nil? value, IFormatterResolver typeResolver) { return MessagePackBinary.WriteNil(ref bytes, offset); } diff --git a/src/MessagePack/Resolvers/ContractlessReflectionObjectResolver.cs b/src/MessagePack/Resolvers/ContractlessReflectionObjectResolver.cs index f9d114f6..d1c15d40 100644 --- a/src/MessagePack/Resolvers/ContractlessReflectionObjectResolver.cs +++ b/src/MessagePack/Resolvers/ContractlessReflectionObjectResolver.cs @@ -149,7 +149,7 @@ namespace MessagePack.Resolvers // } // } - // public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) + // public void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver) // { // // reduce generic method size, avoid write code in <T> type. // if (metaInfo.IsIntKey) @@ -162,7 +162,7 @@ namespace MessagePack.Resolvers // } // } - // public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + // public T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) // { // return (T)ReflectionObjectFormatterHelper.Deserialize(metaInfo, readMembers, constructorParameterIndexes, mapMemberDictionary, bytes, offset, formatterResolver, out readSize); // } diff --git a/src/MessagePack/Resolvers/DynamicEnumResolver.cs b/src/MessagePack/Resolvers/DynamicEnumResolver.cs index 550e349b..e4382b3b 100644 --- a/src/MessagePack/Resolvers/DynamicEnumResolver.cs +++ b/src/MessagePack/Resolvers/DynamicEnumResolver.cs @@ -101,7 +101,7 @@ namespace MessagePack.Resolvers il.Emit(OpCodes.Ret); } - // T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize); + // T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver); { var method = typeBuilder.DefineMethod("Deserialize", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual, enumType, diff --git a/src/MessagePack/Resolvers/DynamicObjectResolver.cs b/src/MessagePack/Resolvers/DynamicObjectResolver.cs index cca2f1d6..17d71d68 100644 --- a/src/MessagePack/Resolvers/DynamicObjectResolver.cs +++ b/src/MessagePack/Resolvers/DynamicObjectResolver.cs @@ -12,6 +12,7 @@ using System.Text.RegularExpressions; using System.Runtime.Serialization; using System.Text; using System.Threading; +using System.Buffers; namespace MessagePack.Resolvers { @@ -1282,13 +1283,13 @@ typeof(int), typeof(int) }); this.deserialize = deserialize; } - public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) + public void Serialize(IBufferWriter<byte> writer, T value, IFormatterResolver formatterResolver) { if (serialize == null) throw new InvalidOperationException(this.GetType().Name + " does not support Serialize."); return serialize(stringByteKeysField, serializeCustomFormatters, ref bytes, offset, value, formatterResolver); } - public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize) + public T Deserialize(ref ReadOnlySequence<byte> byteSequence, IFormatterResolver formatterResolver) { if (deserialize == null) throw new InvalidOperationException(this.GetType().Name + " does not support Deserialize."); return deserialize(deserializeCustomFormatters, bytes, offset, formatterResolver, out readSize); diff --git a/src/MessagePack/StringEncoding.cs b/src/MessagePack/StringEncoding.cs index dfde7d40..caa6336d 100644 --- a/src/MessagePack/StringEncoding.cs +++ b/src/MessagePack/StringEncoding.cs @@ -1,9 +1,21 @@ -using System.Text; +using System; +using System.Text; namespace MessagePack { internal static class StringEncoding { - public static readonly Encoding UTF8 = new UTF8Encoding(false); + internal static readonly Encoding UTF8 = new UTF8Encoding(false); + + internal static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Span<byte> bytes) + { + fixed (char* pChars = &chars[0]) + fixed (byte* pBytes = &bytes[0]) + { + return encoding.GetBytes(pChars, chars.Length, pBytes, bytes.Length); + } + } + + internal static unsafe int GetBytes(this Encoding encoding, string chars, Span<byte> bytes) => GetBytes(encoding, chars.AsSpan(), bytes); } } |