Skip to content

Commit

Permalink
Add some tweaks to SnappyStream (#113)
Browse files Browse the repository at this point in the history
- Slightly more performant ReadByte/WriteByte overloads
- Include DisposeAsync in test coverage

Co-authored-by: Brant Burnett <[email protected]>
  • Loading branch information
brantburnett and btburnett3 authored Dec 16, 2024
1 parent 3dee7b9 commit f666781
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Couchbase.snk
BenchmarkDotNet.Artifacts/
test-results/
TestResults/
.DS_Store
39 changes: 39 additions & 0 deletions Snappier.Tests/SnappyStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ public void CompressAndDecompress(string filename)
Assert.Equal(sourceText, decompressedText);
}

[Fact]
public void CompressAndDecompress_SingleByte()
{
using var resource =
typeof(SnappyStreamTests).Assembly.GetManifestResourceStream($"Snappier.Tests.TestData.alice29.txt");
Assert.NotNull(resource);

var inBuffer = new byte[128];
var readBytes = resource.Read(inBuffer, 0, inBuffer.Length);

using var output = new MemoryStream();

using (var compressor = new SnappyStream(output, CompressionMode.Compress, true))
{
for (var i = 0; i < readBytes; i++)
{
compressor.WriteByte(inBuffer[i]);
}
}

output.Position = 0;

using var decompressor = new SnappyStream(output, CompressionMode.Decompress, true);

var outBuffer = new byte[128];
for (var i = 0; i < readBytes; i++)
{
outBuffer[i] = (byte)decompressor.ReadByte();
}

Assert.Equal(inBuffer, outBuffer);
}

[Theory]
[InlineData("alice29.txt")]
[InlineData("asyoulik.txt")]
Expand All @@ -80,13 +113,19 @@ public async Task CompressAndDecompressAsync(string filename)

using var output = new MemoryStream();

#if NET6_0_OR_GREATER
await
#endif
using (var compressor = new SnappyStream(output, CompressionMode.Compress, true))
{
await resource.CopyToAsync(compressor);
}

output.Position = 0;

#if NET6_0_OR_GREATER
await
#endif
using var decompressor = new SnappyStream(output, CompressionMode.Decompress, true);

using var streamReader = new StreamReader(decompressor, Encoding.UTF8);
Expand Down
16 changes: 16 additions & 0 deletions Snappier/SnappyStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Snappier.Internal;
Expand Down Expand Up @@ -180,8 +181,18 @@ public override long Seek(long offset, SeekOrigin origin)
public override int Read(byte[] buffer, int offset, int count) => ReadCore(buffer.AsSpan(offset, count));

#if !NETSTANDARD2_0

/// <inheritdoc />
public override int Read(Span<byte> buffer) => ReadCore(buffer);

/// <inheritdoc />
public override int ReadByte()
{
byte b = 0;
int r = ReadCore(MemoryMarshal.CreateSpan(ref b, 1));
return r == 0 ? -1 : b;
}

#endif

private int ReadCore(Span<byte> buffer)
Expand Down Expand Up @@ -350,8 +361,13 @@ public override void Write(byte[] buffer, int offset, int count) =>
WriteCore(buffer.AsSpan(offset, count));

#if !NETSTANDARD2_0

/// <inheritdoc />
public override void Write(ReadOnlySpan<byte> buffer) => WriteCore(buffer);

/// <inheritdoc />
public override void WriteByte(byte value) => WriteCore(MemoryMarshal.CreateReadOnlySpan(ref value, 1));

#endif

private void WriteCore(ReadOnlySpan<byte> buffer)
Expand Down

0 comments on commit f666781

Please sign in to comment.