Skip to content

Commit fbd47ff

Browse files
committed
Don't compress serialized data < 512 bytes
(cherry picked from commit eaa84cf)
1 parent 6aeda8d commit fbd47ff

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Source/Mocha.Common/Serialization/Serializer.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,28 @@ public static class Serializer
99
{
1010
var serialized = BinaryConverter.Serialize( obj );
1111

12-
return Compress( serialized );
12+
if ( serialized.Length < 512 )
13+
{
14+
return serialized;
15+
}
16+
17+
var header = "BILZ"u8.ToArray();
18+
return header.Concat( Compress( serialized ) ).ToArray();
1319
}
1420

1521
public static T? Deserialize<T>( byte[] data ) where T : new()
1622
{
17-
var serialized = Decompress( data );
23+
byte[] decompressedData = data;
24+
25+
if ( data.Length > 4 )
26+
{
27+
var header = data[0..4];
28+
29+
if ( Enumerable.SequenceEqual( header, "BILZ"u8.ToArray() ) )
30+
decompressedData = Decompress( data[4..] );
31+
}
1832

19-
return BinaryConverter.Deserialize<T>( serialized );
33+
return BinaryConverter.Deserialize<T>( decompressedData );
2034
}
2135

2236
public static byte[] Compress( byte[] uncompressedData )

0 commit comments

Comments
 (0)