diff options
Diffstat (limited to 'src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs')
-rw-r--r-- | src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index 973f422f..8c4a454c 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -18,8 +18,7 @@ namespace MessagePack [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Each overload has sufficiently unique required parameters.")] public static partial class MessagePackSerializer { - private const int LZ4NotCompressionSizeInLz4BlockType = 64; - private const int MaxHintSize = 1024 * 1024; + private static MessagePackSerializerOptions defaultOptions; /// <summary> /// Gets or sets the default set of options to use when not explicitly specified for a method call. @@ -33,7 +32,20 @@ namespace MessagePack /// If you are an app author, realize that setting this property impacts the entire application so it should only be /// set once, and before any use of <see cref="MessagePackSerializer"/> occurs. /// </remarks> - public static MessagePackSerializerOptions DefaultOptions { get; set; } = MessagePackSerializerOptions.Standard; + public static MessagePackSerializerOptions DefaultOptions + { + get + { + if (defaultOptions is null) + { + defaultOptions = MessagePackSerializerOptions.Standard; + } + + return defaultOptions; + } + + set => defaultOptions = value; + } /// <summary> /// A thread-local, recyclable array that may be used for short bursts of code. @@ -79,13 +91,13 @@ namespace MessagePack { if (options.Compression.IsCompression() && !PrimitiveChecker<T>.IsMessagePackFixedSizePrimitive) { - using (var scratchRental = SequencePool.Shared.Rent()) + using (var scratchRental = options.SequencePool.Rent()) { var scratch = scratchRental.Value; MessagePackWriter scratchWriter = writer.Clone(scratch); options.Resolver.GetFormatterWithVerify<T>().Serialize(ref scratchWriter, value, options); scratchWriter.Flush(); - ToLZ4BinaryCore(scratch, ref writer, options.Compression); + ToLZ4BinaryCore(scratch, ref writer, options.Compression, options.CompressionMinLength); } } else @@ -119,7 +131,8 @@ namespace MessagePack scratchArray = array = new byte[65536]; } - var msgpackWriter = new MessagePackWriter(SequencePool.Shared, array) + options = options ?? DefaultOptions; + var msgpackWriter = new MessagePackWriter(options.SequencePool, array) { CancellationToken = cancellationToken, }; @@ -137,8 +150,10 @@ namespace MessagePack /// <exception cref="MessagePackSerializationException">Thrown when any error occurs during serialization.</exception> public static void Serialize<T>(Stream stream, T value, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) { + options = options ?? DefaultOptions; cancellationToken.ThrowIfCancellationRequested(); - using (SequencePool.Rental sequenceRental = SequencePool.Shared.Rent()) + + using (SequencePool.Rental sequenceRental = options.SequencePool.Rent()) { Serialize<T>(sequenceRental.Value, value, options, cancellationToken); @@ -168,8 +183,10 @@ namespace MessagePack /// <exception cref="MessagePackSerializationException">Thrown when any error occurs during serialization.</exception> public static async Task SerializeAsync<T>(Stream stream, T value, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) { + options = options ?? DefaultOptions; cancellationToken.ThrowIfCancellationRequested(); - using (SequencePool.Rental sequenceRental = SequencePool.Shared.Rent()) + + using (SequencePool.Rental sequenceRental = options.SequencePool.Rent()) { Serialize<T>(sequenceRental.Value, value, options, cancellationToken); @@ -222,7 +239,7 @@ namespace MessagePack { if (options.Compression.IsCompression()) { - using (var msgPackUncompressedRental = SequencePool.Shared.Rent()) + using (var msgPackUncompressedRental = options.SequencePool.Rent()) { var msgPackUncompressed = msgPackUncompressedRental.Value; if (TryDecompress(ref reader, msgPackUncompressed)) @@ -315,12 +332,14 @@ namespace MessagePack /// </remarks> public static T Deserialize<T>(Stream stream, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) { + options = options ?? DefaultOptions; + if (TryDeserializeFromMemoryStream(stream, options, cancellationToken, out T result)) { return result; } - using (var sequenceRental = SequencePool.Shared.Rent()) + using (var sequenceRental = options.SequencePool.Rent()) { var sequence = sequenceRental.Value; try @@ -329,7 +348,7 @@ namespace MessagePack do { cancellationToken.ThrowIfCancellationRequested(); - Span<byte> span = sequence.GetSpan(stream.CanSeek ? (int)Math.Min(MaxHintSize, stream.Length - stream.Position) : 0); + Span<byte> span = sequence.GetSpan(stream.CanSeek ? (int)Math.Min(options.SuggestedContiguousMemorySize, stream.Length - stream.Position) : 0); bytesRead = stream.Read(span); sequence.Advance(bytesRead); } @@ -362,12 +381,14 @@ namespace MessagePack /// </remarks> public static async ValueTask<T> DeserializeAsync<T>(Stream stream, MessagePackSerializerOptions options = null, CancellationToken cancellationToken = default) { + options = options ?? DefaultOptions; + if (TryDeserializeFromMemoryStream(stream, options, cancellationToken, out T result)) { return result; } - using (var sequenceRental = SequencePool.Shared.Rent()) + using (var sequenceRental = options.SequencePool.Rent()) { var sequence = sequenceRental.Value; try @@ -375,7 +396,7 @@ namespace MessagePack int bytesRead; do { - Memory<byte> memory = sequence.GetMemory(stream.CanSeek ? (int)Math.Min(MaxHintSize, stream.Length - stream.Position) : 0); + Memory<byte> memory = sequence.GetMemory(stream.CanSeek ? (int)Math.Min(options.SuggestedContiguousMemorySize, stream.Length - stream.Position) : 0); bytesRead = await stream.ReadAsync(memory, cancellationToken).ConfigureAwait(false); sequence.Advance(bytesRead); } @@ -546,16 +567,16 @@ namespace MessagePack return false; } - private static void ToLZ4BinaryCore(in ReadOnlySequence<byte> msgpackUncompressedData, ref MessagePackWriter writer, MessagePackCompression compression) + private static void ToLZ4BinaryCore(in ReadOnlySequence<byte> msgpackUncompressedData, ref MessagePackWriter writer, MessagePackCompression compression, int minCompressionSize) { - if (compression == MessagePackCompression.Lz4Block) + if (msgpackUncompressedData.Length < minCompressionSize) { - if (msgpackUncompressedData.Length < LZ4NotCompressionSizeInLz4BlockType) - { - writer.WriteRaw(msgpackUncompressedData); - return; - } + writer.WriteRaw(msgpackUncompressedData); + return; + } + if (compression == MessagePackCompression.Lz4Block) + { var maxCompressedLength = LZ4Codec.MaximumOutputLength((int)msgpackUncompressedData.Length); var lz4Span = ArrayPool<byte>.Shared.Rent(maxCompressedLength); try |