Skip to content

Commit

Permalink
feat(tar): support for async streams (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-c committed May 24, 2022
1 parent e589b5e commit d843d6d
Show file tree
Hide file tree
Showing 15 changed files with 1,239 additions and 418 deletions.
4 changes: 1 addition & 3 deletions benchmark/ICSharpCode.SharpZipLib.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using BenchmarkDotNet;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.CsProj;
Expand Down
82 changes: 82 additions & 0 deletions benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarInputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using ICSharpCode.SharpZipLib.Tar;

namespace ICSharpCode.SharpZipLib.Benchmark.Tar
{
[MemoryDiagnoser]
[Config(typeof(MultipleRuntimes))]
public class TarInputStream
{
private readonly byte[] archivedData;
private readonly byte[] readBuffer = new byte[1024];

public TarInputStream()
{
using (var outputMemoryStream = new MemoryStream())
{
using (var zipOutputStream =
new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8))
{
var tarEntry = TarEntry.CreateTarEntry("some file");
tarEntry.Size = 1024 * 1024;
zipOutputStream.PutNextEntry(tarEntry);

var rng = RandomNumberGenerator.Create();
var inputBuffer = new byte[1024];
rng.GetBytes(inputBuffer);

for (int i = 0; i < 1024; i++)
{
zipOutputStream.Write(inputBuffer, 0, inputBuffer.Length);
}
}

archivedData = outputMemoryStream.ToArray();
}
}

[Benchmark]
public long ReadTarInputStream()
{
using (var memoryStream = new MemoryStream(archivedData))
using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8))
{
var entry = zipInputStream.GetNextEntry();

while (zipInputStream.Read(readBuffer, 0, readBuffer.Length) > 0)
{
}

return entry.Size;
}
}

[Benchmark]
public async Task<long> ReadTarInputStreamAsync()
{
using (var memoryStream = new MemoryStream(archivedData))
using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8))
{
var entry = await zipInputStream.GetNextEntryAsync(CancellationToken.None);

#if NETCOREAPP2_1_OR_GREATER
while (await zipInputStream.ReadAsync(readBuffer.AsMemory()) > 0)
{
}
#else
while (await zipInputStream.ReadAsync(readBuffer, 0, readBuffer.Length) > 0)
{
}
#endif

return entry.Size;
}
}
}
}
64 changes: 64 additions & 0 deletions benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using ICSharpCode.SharpZipLib.Tar;

namespace ICSharpCode.SharpZipLib.Benchmark.Tar
{
[MemoryDiagnoser]
[Config(typeof(MultipleRuntimes))]
public class TarOutputStream
{
private readonly byte[] backingArray = new byte[1024 * 1024 + (6 * 1024)];
private readonly byte[] inputBuffer = new byte[1024];
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();

[Benchmark]
public void WriteTarOutputStream()
{
using (var outputMemoryStream = new MemoryStream(backingArray))
{
using (var tarOutputStream =
new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8))
{
var tarEntry = TarEntry.CreateTarEntry("some file");
tarEntry.Size = 1024 * 1024;
tarOutputStream.PutNextEntry(tarEntry);

_rng.GetBytes(inputBuffer);

for (int i = 0; i < 1024; i++)
{
tarOutputStream.Write(inputBuffer, 0, inputBuffer.Length);
}
}
}
}

[Benchmark]
public async Task WriteTarOutputStreamAsync()
{
using (var outputMemoryStream = new MemoryStream(backingArray))
{
using (var tarOutputStream =
new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8))
{
var tarEntry = TarEntry.CreateTarEntry("some file");
tarEntry.Size = 1024 * 1024;

await tarOutputStream.PutNextEntryAsync(tarEntry, CancellationToken.None);

_rng.GetBytes(inputBuffer);

for (int i = 0; i < 1024; i++)
{
await tarOutputStream.WriteAsync(inputBuffer, 0, inputBuffer.Length);
}
}
}
}
}
}
71 changes: 71 additions & 0 deletions src/ICSharpCode.SharpZipLib/Core/ExactMemoryPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Buffers;

namespace ICSharpCode.SharpZipLib.Core
{
/// <summary>
/// A MemoryPool that will return a Memory which is exactly the length asked for using the bufferSize parameter.
/// This is in contrast to the default ArrayMemoryPool which will return a Memory of equal size to the underlying
/// array which at least as long as the minBufferSize parameter.
/// Note: The underlying array may be larger than the slice of Memory
/// </summary>
/// <typeparam name="T"></typeparam>
internal sealed class ExactMemoryPool<T> : MemoryPool<T>
{
public new static readonly MemoryPool<T> Shared = new ExactMemoryPool<T>();

public override IMemoryOwner<T> Rent(int bufferSize = -1)
{
if ((uint)bufferSize > int.MaxValue || bufferSize < 0)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize));
}

return new ExactMemoryPoolBuffer(bufferSize);
}

protected override void Dispose(bool disposing)
{
}

public override int MaxBufferSize => int.MaxValue;

private sealed class ExactMemoryPoolBuffer : IMemoryOwner<T>, IDisposable
{
private T[] array;
private readonly int size;

public ExactMemoryPoolBuffer(int size)
{
this.size = size;
this.array = ArrayPool<T>.Shared.Rent(size);
}

public Memory<T> Memory
{
get
{
T[] array = this.array;
if (array == null)
{
throw new ObjectDisposedException(nameof(ExactMemoryPoolBuffer));
}

return new Memory<T>(array).Slice(0, size);
}
}

public void Dispose()
{
T[] array = this.array;
if (array == null)
{
return;
}

this.array = null;
ArrayPool<T>.Shared.Return(array);
}
}
}
}
22 changes: 22 additions & 0 deletions src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Concurrent;
using System.Text;

namespace ICSharpCode.SharpZipLib.Core
{
internal class StringBuilderPool
{
public static StringBuilderPool Instance { get; } = new StringBuilderPool();
private readonly ConcurrentQueue<StringBuilder> pool = new ConcurrentQueue<StringBuilder>();

public StringBuilder Rent()
{
return pool.TryDequeue(out var builder) ? builder : new StringBuilder();
}

public void Return(StringBuilder builder)
{
builder.Clear();
pool.Enqueue(builder);
}
}
}
14 changes: 12 additions & 2 deletions src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ Please see https://github.com/icsharpcode/SharpZipLib/wiki/Release-1.3.3 for mor
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.2" />
</ItemGroup>

<ItemGroup>
<None Include="../../assets/sharpziplib-nuget-256x256.png">
<Pack>True</Pack>
<PackagePath>images</PackagePath>
Expand Down

0 comments on commit d843d6d

Please sign in to comment.