diff --git a/Snappier.Benchmarks/BlockCompressHtml.cs b/Snappier.Benchmarks/BlockCompressHtml.cs new file mode 100644 index 0000000..8749847 --- /dev/null +++ b/Snappier.Benchmarks/BlockCompressHtml.cs @@ -0,0 +1,33 @@ +using System; +using BenchmarkDotNet.Attributes; +using Snappier.Internal; + +namespace Snappier.Benchmarks +{ + public class BlockCompressHtml + { + private ReadOnlyMemory _input; + private Memory _output; + + [GlobalSetup] + public void LoadToMemory() + { + using var resource = + typeof(BlockCompressHtml).Assembly.GetManifestResourceStream("Snappier.Benchmarks.TestData.html"); + + byte[] input = new byte[65536]; // Just test the first 64KB + int inputLength = resource!.Read(input, 0, input.Length); + _input = input.AsMemory(0, inputLength); + + _output = new byte[65536]; + } + + [Benchmark] + public int Compress() + { + using var compressor = new SnappyCompressor(); + + return compressor.Compress(_input.Span, _output.Span); + } + } +} diff --git a/Snappier/Internal/SnappyCompressor.cs b/Snappier/Internal/SnappyCompressor.cs index 7dab9dd..45f5c13 100644 --- a/Snappier/Internal/SnappyCompressor.cs +++ b/Snappier/Internal/SnappyCompressor.cs @@ -11,10 +11,6 @@ internal class SnappyCompressor : IDisposable public int Compress(ReadOnlySpan input, Span output) { - if (output.Length < Helpers.MaxCompressedLength(input.Length)) - { - ThrowHelper.ThrowArgumentException("Insufficient output buffer", nameof(output)); - } if (_workingMemory == null) { ThrowHelper.ThrowObjectDisposedException(nameof(SnappyCompressor)); @@ -46,6 +42,10 @@ public int Compress(ReadOnlySpan input, Span output) try { int written = CompressFragment(fragment, scratch.AsSpan(), hashTable); + if (output.Length < written) + { + ThrowHelper.ThrowArgumentException("Insufficient output buffer", nameof(output)); + } scratch.AsSpan(0, written).CopyTo(output); output = output.Slice(written);