Skip to content

Commit ce98e63

Browse files
committed
Buffer pooling in Wrap
1 parent 045d9ac commit ce98e63

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ Consider using `ThreadStatic` or pool of compressors for bulk processing.
8989
* `GetCompressBound` returns required destination buffer size for source data of size `size`.
9090

9191
```c#
92-
static int GetCompressBound(int size)
93-
static ulong GetCompressBoundLong(ulong size)
92+
static int GetCompressBound(int size);
93+
static ulong GetCompressBoundLong(ulong size);
9494
```
9595

9696
### CompressionStream class

ZstdNet/Compressor.cs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers;
23
using size_t = System.UIntPtr;
34

45
namespace ZstdNet
@@ -48,15 +49,20 @@ public byte[] Wrap(ReadOnlySpan<byte> src)
4849
{
4950
//NOTE: Wrap tries its best, but if src is uncompressible and the size is too large, ZSTD_error_dstSize_tooSmall will be thrown
5051
var dstCapacity = Math.Min(Consts.MaxByteArrayLength, GetCompressBoundLong((ulong)src.Length));
51-
var dst = new byte[dstCapacity];
52-
53-
var dstSize = Wrap(src, new Span<byte>(dst));
54-
if(dstCapacity == (ulong)dstSize)
55-
return dst;
56-
57-
var result = new byte[dstSize];
58-
Array.Copy(dst, result, dstSize);
59-
return result;
52+
var dst = ArrayPool<byte>.Shared.Rent((int)dstCapacity);
53+
54+
try
55+
{
56+
var dstSize = Wrap(src, new Span<byte>(dst));
57+
58+
var result = new byte[dstSize];
59+
Array.Copy(dst, result, dstSize);
60+
return result;
61+
}
62+
finally
63+
{
64+
ArrayPool<byte>.Shared.Return(dst);
65+
}
6066
}
6167

6268
public static int GetCompressBound(int size)

appveyor.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
version: 1.0.{build}
22
image: Visual Studio 2019
3+
install:
4+
- appveyor DownloadFile https://dist.nuget.org/win-x86-commandline/v5.8.0/nuget.exe
35
before_build:
4-
- cmd: nuget restore
6+
- nuget restore
57
build:
68
verbosity: minimal
7-
cache:
8-
- packages -> **\packages.config
9+
#cache:
10+
# - packages -> **\packages.config

0 commit comments

Comments
 (0)