Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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);
}
}
}
}
}
}
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.Generic;
using System.Text;

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

public StringBuilder Rent()
{
return pool.Count > 0 ? pool.Dequeue() : 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
Loading