diff options
8 files changed, 63 insertions, 12 deletions
diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.Json.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.Json.cs index 1d604957..38e39985 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.Json.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.Json.cs @@ -171,7 +171,7 @@ namespace MessagePack } scratchWriter.Flush(); - ToLZ4BinaryCore(scratchRental.Value, ref writer, options.Compression); + ToLZ4BinaryCore(scratchRental.Value, ref writer, options.Compression, options.CompressionMinLength); } } else diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs index 93acd668..24b5373c 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializer.cs @@ -18,7 +18,6 @@ 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; @@ -99,7 +98,7 @@ namespace MessagePack 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 @@ -569,16 +568,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 diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs index 211874c4..771aa25f 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePackSerializerOptions.cs @@ -57,6 +57,7 @@ namespace MessagePack this.Resolver = copyFrom.Resolver; this.Compression = copyFrom.Compression; + this.CompressionMinLength = copyFrom.CompressionMinLength; this.OldSpec = copyFrom.OldSpec; this.OmitAssemblyVersion = copyFrom.OmitAssemblyVersion; this.AllowAssemblyVersionMismatch = copyFrom.AllowAssemblyVersionMismatch; @@ -82,6 +83,16 @@ namespace MessagePack public MessagePackCompression Compression { get; private set; } /// <summary> + /// Gets the length a serialized msgpack result must equal or exceed before <see cref="Compression"/> is applied. + /// </summary> + /// <value>The default value is 64.</value> + /// <remarks> + /// When compression is <em>not</em> applied due to a short serialized result, deserialization will still succeed + /// even if <see cref="Compression"/> is set to something other than <see cref="MessagePackCompression.None"/>. + /// </remarks> + public int CompressionMinLength { get; private set; } = 64; + + /// <summary> /// Gets a value indicating whether to serialize with <see cref="MessagePackWriter.OldSpec"/> set to some value /// causing messagepack spec compliance to be explicitly set to the old or new format. /// </summary> @@ -195,6 +206,28 @@ namespace MessagePack } /// <summary> + /// Gets a copy of these options with the <see cref="CompressionMinLength"/> property set to a new value. + /// </summary> + /// <param name="compressionMinLength">The new value for the <see cref="CompressionMinLength"/> property. Must be a positive integer.</param> + /// <returns>The new instance; or the original if the value is unchanged.</returns> + public MessagePackSerializerOptions WithCompressionMinLength(int compressionMinLength) + { + if (this.CompressionMinLength == compressionMinLength) + { + return this; + } + + if (compressionMinLength <= 0) + { + throw new ArgumentOutOfRangeException(nameof(compressionMinLength)); + } + + var result = this.Clone(); + result.CompressionMinLength = compressionMinLength; + return result; + } + + /// <summary> /// Gets a copy of these options with the <see cref="OldSpec"/> property set to a new value. /// </summary> /// <param name="oldSpec">The new value for the <see cref="OldSpec"/>.</param> diff --git a/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/MessagePackSerializerOptionsTests.cs b/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/MessagePackSerializerOptionsTests.cs index 96bec149..dfe988f8 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/MessagePackSerializerOptionsTests.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/MessagePackSerializerOptionsTests.cs @@ -1,6 +1,7 @@ // Copyright (c) All contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; using MessagePack; using MessagePack.Resolvers; using Xunit; @@ -37,6 +38,16 @@ public class MessagePackSerializerOptionsTests } [Fact] + public void CompressionMinLength() + { + Assert.Equal(64, MessagePackSerializerOptions.Standard.CompressionMinLength); + Assert.Throws<ArgumentOutOfRangeException>(() => MessagePackSerializerOptions.Standard.WithCompressionMinLength(0)); + Assert.Throws<ArgumentOutOfRangeException>(() => MessagePackSerializerOptions.Standard.WithCompressionMinLength(-1)); + MessagePackSerializerOptions options = MessagePackSerializerOptions.Standard.WithCompressionMinLength(128); + Assert.Equal(128, options.CompressionMinLength); + } + + [Fact] public void OldSpec() { Assert.Null(MessagePackSerializerOptions.Standard.OldSpec); diff --git a/src/MessagePack/net5.0/PublicAPI.Unshipped.txt b/src/MessagePack/net5.0/PublicAPI.Unshipped.txt index f79827d5..49e84ad1 100644 --- a/src/MessagePack/net5.0/PublicAPI.Unshipped.txt +++ b/src/MessagePack/net5.0/PublicAPI.Unshipped.txt @@ -1,4 +1,6 @@ MessagePack.Formatters.StringInterningFormatter MessagePack.Formatters.StringInterningFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions options) -> string MessagePack.Formatters.StringInterningFormatter.Serialize(ref MessagePack.MessagePackWriter writer, string value, MessagePack.MessagePackSerializerOptions options) -> void -MessagePack.Formatters.StringInterningFormatter.StringInterningFormatter() -> void
\ No newline at end of file +MessagePack.Formatters.StringInterningFormatter.StringInterningFormatter() -> void +MessagePack.MessagePackSerializerOptions.CompressionMinLength.get -> int +MessagePack.MessagePackSerializerOptions.WithCompressionMinLength(int compressionMinLength) -> MessagePack.MessagePackSerializerOptions
\ No newline at end of file diff --git a/src/MessagePack/net6.0/PublicAPI.Unshipped.txt b/src/MessagePack/net6.0/PublicAPI.Unshipped.txt index b78f6308..9ba64088 100644 --- a/src/MessagePack/net6.0/PublicAPI.Unshipped.txt +++ b/src/MessagePack/net6.0/PublicAPI.Unshipped.txt @@ -2,3 +2,5 @@ MessagePack.Formatters.StringInterningFormatter MessagePack.Formatters.StringInterningFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions options) -> string MessagePack.Formatters.StringInterningFormatter.Serialize(ref MessagePack.MessagePackWriter writer, string value, MessagePack.MessagePackSerializerOptions options) -> void MessagePack.Formatters.StringInterningFormatter.StringInterningFormatter() -> void +MessagePack.MessagePackSerializerOptions.CompressionMinLength.get -> int +MessagePack.MessagePackSerializerOptions.WithCompressionMinLength(int compressionMinLength) -> MessagePack.MessagePackSerializerOptions diff --git a/src/MessagePack/netcoreapp3.1/PublicAPI.Unshipped.txt b/src/MessagePack/netcoreapp3.1/PublicAPI.Unshipped.txt index b78f6308..9ba64088 100644 --- a/src/MessagePack/netcoreapp3.1/PublicAPI.Unshipped.txt +++ b/src/MessagePack/netcoreapp3.1/PublicAPI.Unshipped.txt @@ -2,3 +2,5 @@ MessagePack.Formatters.StringInterningFormatter MessagePack.Formatters.StringInterningFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions options) -> string MessagePack.Formatters.StringInterningFormatter.Serialize(ref MessagePack.MessagePackWriter writer, string value, MessagePack.MessagePackSerializerOptions options) -> void MessagePack.Formatters.StringInterningFormatter.StringInterningFormatter() -> void +MessagePack.MessagePackSerializerOptions.CompressionMinLength.get -> int +MessagePack.MessagePackSerializerOptions.WithCompressionMinLength(int compressionMinLength) -> MessagePack.MessagePackSerializerOptions diff --git a/src/MessagePack/netstandard2.0/PublicAPI.Unshipped.txt b/src/MessagePack/netstandard2.0/PublicAPI.Unshipped.txt index f79827d5..49e84ad1 100644 --- a/src/MessagePack/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/MessagePack/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,4 +1,6 @@ MessagePack.Formatters.StringInterningFormatter MessagePack.Formatters.StringInterningFormatter.Deserialize(ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions options) -> string MessagePack.Formatters.StringInterningFormatter.Serialize(ref MessagePack.MessagePackWriter writer, string value, MessagePack.MessagePackSerializerOptions options) -> void -MessagePack.Formatters.StringInterningFormatter.StringInterningFormatter() -> void
\ No newline at end of file +MessagePack.Formatters.StringInterningFormatter.StringInterningFormatter() -> void +MessagePack.MessagePackSerializerOptions.CompressionMinLength.get -> int +MessagePack.MessagePackSerializerOptions.WithCompressionMinLength(int compressionMinLength) -> MessagePack.MessagePackSerializerOptions
\ No newline at end of file |