Skip to content

Commit

Permalink
Update readme, add a package icon, and add package validation
Browse files Browse the repository at this point in the history
  • Loading branch information
brantburnett committed Dec 22, 2024
1 parent 67dd494 commit e18cdac
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
55 changes: 48 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Snappier

![.NET Core](https://github.com/brantburnett/Snappier/workflows/.NET%20Core/badge.svg)

## Introduction

Snappier is a pure C# port of Google's [Snappy](https://github.com/google/snappy) compression algorithm. It is designed with speed as the primary goal, rather than compression ratio, and is ideal for compressing network traffic. Please see [the Snappy README file](https://github.com/google/snappy/blob/master/README.md) for more details on Snappy.
Expand All @@ -24,7 +22,7 @@ The Snappier project aims to meet the following needs of the .NET community.
Simply add a NuGet package reference to the latest version of Snappier.

```xml
<PackageReference Include="Snappier" Version="1.0.0" />
<PackageReference Include="Snappier" Version="1.1.6" />
```

or
Expand All @@ -48,6 +46,12 @@ public class Program
// In this example, we're using heap allocated byte arrays, however in most cases
// you would get these buffers from a buffer pool like ArrayPool<byte> or MemoryPool<byte>.
// If the output buffer is too small, an ArgumentException is thrown. This will not
// occur in this example because a sufficient buffer is always allocated via
// Snappy.GetMaxCompressedLength or Snappy.GetUncompressedLength. There are TryCompress
// and TryDecompress overloads that return false if the output buffer is too small
// rather than throwing an exception.
// Compression
byte[] buffer = new byte[Snappy.GetMaxCompressedLength(Data)];
int compressedLength = Snappy.Compress(Data, buffer);
Expand Down Expand Up @@ -77,8 +81,8 @@ public class Program
public static void Main()
{
// This option uses `MemoryPool<byte>.Shared`. However, if you fail to
// dispose of the returned buffers correctly it can result in memory leaks.
// It is imperative to either call .Dispose() or use a using statement.
// dispose of the returned buffers correctly it can result in inefficient garbage collection.
// It is important to either call .Dispose() or use a using statement.
// Compression
using (IMemoryOwner<byte> compressed = Snappy.CompressToMemory(Data))
Expand All @@ -93,6 +97,42 @@ public class Program
}
```

## Block compression/decompression using a buffer writter

```cs
using Snappier;
using System.Buffers;

public class Program
{
private static byte[] Data = {0, 1, 2}; // Wherever you get the data from
public static void Main()
{
// This option uses `IBufferWriter<byte>`. In .NET 6 you can get a simple
// implementation such as `ArrayBufferWriter<byte>` but it may also be a `PipeWriter<byte>`
// or any other more advanced implementation of `IBufferWriter<byte>`.
// These overloads also accept a `ReadOnlySequence<byte>` which allows the source data
// to be made up of buffer segments rather than one large buffer. However, segment size
// may be a factor in performance. For compression, segments that are some multiple of
// 64KB are recommended. For decompression, simply avoid small segments.
// Compression
var compressedBufferWriter = new ArrayBufferWriter<byte>();
Snappy.Compress(new ReadOnlySequence<byte>(Data), compressedBufferWriter);
var compressedData = compressedBufferWriter.WrittenMemory;

// Decompression
var decompressedBufferWriter = new ArrayBufferWriter<byte>();
Snappy.Decompress(new ReadOnlySequence<byte>(compressedData), decompressedBufferWriter);
var decompressedData = decompressedBufferWriter.WrittenMemory;

// Do something with the data
}
}
```

## Block compression/decompression using heap allocated byte[]

```cs
Expand Down Expand Up @@ -136,7 +176,8 @@ public class Program
// First, compression
using var compressed = new MemoryStream();

using (var compressor = new SnappyStream(compressed, CompressionMode.Compress, true)) {
using (var compressor = new SnappyStream(compressed, CompressionMode.Compress, leaveOpen: true))
{
await fileStream.CopyToAsync(compressor);

// Disposing the compressor also flushes the buffers to the inner stream
Expand Down Expand Up @@ -166,4 +207,4 @@ public class Program
There are other projects available for C#/.NET which implement Snappy compression.

- [Snappy.NET](https://snappy.machinezoo.com/) - Uses P/Invoke to C++ for great performance. However, it only works on Windows, and is a bit heap allocation heavy in some cases. It also hasn't been updated since 2014 (as of 10/2020). This project may still be the best choice if your project is on the legacy .NET Framework on Windows, where Snappier is much less performant.
- [IronSnappy](https://github.com/aloneguid/IronSnappy) - Another pure C# port, based on the Golang implemenation instead of the C++ implementation.
- [IronSnappy](https://github.com/aloneguid/IronSnappy) - Another pure C# port, based on the Golang implementation instead of the C++ implementation.
5 changes: 5 additions & 0 deletions Snappier/Snappier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\Snappier.snk</AssemblyOriginatorKeyFile>

<EnablePackageValidation>true</EnablePackageValidation>
<PackageValidationBaselineVersion>1.1.6</PackageValidationBaselineVersion>

<Authors>btburnett3</Authors>
<PackageTags>snappy;compression;fast;io</PackageTags>
<!-- Copyright remains with Google since this is a direct port of the C++ source from https://github.com/google/snappy -->
Expand All @@ -22,6 +25,7 @@
</Description>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand All @@ -40,6 +44,7 @@

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="$(PackageReadmeFile)" />
<None Include="..\icon.png" Pack="true" PackagePath="$(PackageIcon)" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand Down
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e18cdac

Please sign in to comment.