Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow shorter compression output buffer lengths #60

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Snappier.Benchmarks/BlockCompressHtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using BenchmarkDotNet.Attributes;
using Snappier.Internal;

namespace Snappier.Benchmarks
{
public class BlockCompressHtml
{
private ReadOnlyMemory<byte> _input;
private Memory<byte> _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);
}
}
}
8 changes: 4 additions & 4 deletions Snappier/Internal/SnappyCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ internal class SnappyCompressor : IDisposable

public int Compress(ReadOnlySpan<byte> input, Span<byte> output)
{
if (output.Length < Helpers.MaxCompressedLength(input.Length))
{
ThrowHelper.ThrowArgumentException("Insufficient output buffer", nameof(output));
}
if (_workingMemory == null)
{
ThrowHelper.ThrowObjectDisposedException(nameof(SnappyCompressor));
Expand Down Expand Up @@ -46,6 +42,10 @@ public int Compress(ReadOnlySpan<byte> input, Span<byte> 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);
Expand Down