-
Notifications
You must be signed in to change notification settings - Fork 978
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zip): add optional inflater pooling (#843)
- Loading branch information
Showing
13 changed files
with
233 additions
and
29 deletions.
There are no files selected for viewing
14 changes: 6 additions & 8 deletions
14
benchmark/ICSharpCode.SharpZipLib.Benchmark/ICSharpCode.SharpZipLib.Benchmark.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net6.0;netcoreapp3.1;net462</TargetFrameworks> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net462;net6.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet"> | ||
<Version>0.12.1</Version> | ||
</PackageReference> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ICSharpCode.SharpZipLib\ICSharpCode.SharpZipLib.csproj" /> | ||
<ProjectReference Include="..\..\src\ICSharpCode.SharpZipLib\ICSharpCode.SharpZipLib.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using ICSharpCode.SharpZipLib.Zip; | ||
|
||
namespace ICSharpCode.SharpZipLib.Benchmark.Zip | ||
{ | ||
[MemoryDiagnoser] | ||
[Config(typeof(MultipleRuntimes))] | ||
public class ZipFile | ||
{ | ||
private readonly byte[] readBuffer = new byte[4096]; | ||
private string zipFileWithLargeAmountOfEntriesPath; | ||
|
||
[GlobalSetup] | ||
public async Task GlobalSetup() | ||
{ | ||
SharpZipLibOptions.InflaterPoolSize = 4; | ||
|
||
// large real-world test file from test262 repository | ||
string commitSha = "2e4e0e6b8ebe3348a207144204cb6d7a5571c863"; | ||
zipFileWithLargeAmountOfEntriesPath = Path.Combine(Path.GetTempPath(), $"{commitSha}.zip"); | ||
if (!File.Exists(zipFileWithLargeAmountOfEntriesPath)) | ||
{ | ||
var uri = $"https://github.com/tc39/test262/archive/{commitSha}.zip"; | ||
|
||
Console.WriteLine("Loading test262 repository archive from {0}", uri); | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
using (var downloadStream = await client.GetStreamAsync(uri)) | ||
{ | ||
using (var writeStream = File.OpenWrite(zipFileWithLargeAmountOfEntriesPath)) | ||
{ | ||
await downloadStream.CopyToAsync(writeStream); | ||
Console.WriteLine("File downloaded and saved to {0}", zipFileWithLargeAmountOfEntriesPath); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
[Benchmark] | ||
public void ReadLargeZipFile() | ||
{ | ||
using (var file = new SharpZipLib.Zip.ZipFile(zipFileWithLargeAmountOfEntriesPath)) | ||
{ | ||
foreach (ZipEntry entry in file) | ||
{ | ||
using (var stream = file.GetInputStream(entry)) | ||
{ | ||
while (stream.Read(readBuffer, 0, readBuffer.Length) > 0) | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using ICSharpCode.SharpZipLib.Zip.Compression; | ||
|
||
namespace ICSharpCode.SharpZipLib.Core | ||
{ | ||
/// <summary> | ||
/// Pool for <see cref="Inflater"/> instances as they can be costly due to byte array allocations. | ||
/// </summary> | ||
internal sealed class InflaterPool | ||
{ | ||
private readonly ConcurrentQueue<PooledInflater> noHeaderPool = new ConcurrentQueue<PooledInflater>(); | ||
private readonly ConcurrentQueue<PooledInflater> headerPool = new ConcurrentQueue<PooledInflater>(); | ||
|
||
internal static InflaterPool Instance { get; } = new InflaterPool(); | ||
|
||
private InflaterPool() | ||
{ | ||
} | ||
|
||
internal Inflater Rent(bool noHeader = false) | ||
{ | ||
if (SharpZipLibOptions.InflaterPoolSize <= 0) | ||
{ | ||
return new Inflater(noHeader); | ||
} | ||
|
||
var pool = GetPool(noHeader); | ||
|
||
PooledInflater inf; | ||
if (pool.TryDequeue(out var inflater)) | ||
{ | ||
inf = inflater; | ||
inf.Reset(); | ||
} | ||
else | ||
{ | ||
inf = new PooledInflater(noHeader); | ||
} | ||
|
||
return inf; | ||
} | ||
|
||
internal void Return(Inflater inflater) | ||
{ | ||
if (SharpZipLibOptions.InflaterPoolSize <= 0) | ||
{ | ||
return; | ||
} | ||
|
||
if (!(inflater is PooledInflater pooledInflater)) | ||
{ | ||
throw new ArgumentException("Returned inflater was not a pooled one"); | ||
} | ||
|
||
var pool = GetPool(inflater.noHeader); | ||
if (pool.Count < SharpZipLibOptions.InflaterPoolSize) | ||
{ | ||
pooledInflater.Reset(); | ||
pool.Enqueue(pooledInflater); | ||
} | ||
} | ||
|
||
private ConcurrentQueue<PooledInflater> GetPool(bool noHeader) => noHeader ? noHeaderPool : headerPool; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using ICSharpCode.SharpZipLib.Zip.Compression; | ||
|
||
namespace ICSharpCode.SharpZipLib | ||
{ | ||
/// <summary> | ||
/// Global options to alter behavior. | ||
/// </summary> | ||
public static class SharpZipLibOptions | ||
{ | ||
/// <summary> | ||
/// The max pool size allowed for reusing <see cref="Inflater"/> instances, defaults to 0 (disabled). | ||
/// </summary> | ||
public static int InflaterPoolSize { get; set; } = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/ICSharpCode.SharpZipLib/Zip/Compression/PooledInflater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ICSharpCode.SharpZipLib.Core; | ||
|
||
namespace ICSharpCode.SharpZipLib.Zip.Compression | ||
{ | ||
/// <summary> | ||
/// A marker type for pooled version of an inflator that we can return back to <see cref="InflaterPool"/>. | ||
/// </summary> | ||
internal sealed class PooledInflater : Inflater | ||
{ | ||
public PooledInflater(bool noHeader) : base(noHeader) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.