Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an ImapToken cache to try and reduce ImapToken allocations #1630

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
22 changes: 20 additions & 2 deletions MailKit/ByteArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,37 @@
using System;
using System.Text;
using System.Buffers;
using System.Runtime.CompilerServices;

namespace MailKit
{
class ByteArrayBuilder : IDisposable
{
readonly int initialCapacity;
byte[] buffer;
int length;

public ByteArrayBuilder (int initialCapacity)
public ByteArrayBuilder (int capacity)
{
buffer = ArrayPool<byte>.Shared.Rent (initialCapacity);
buffer = ArrayPool<byte>.Shared.Rent (capacity);
initialCapacity = capacity;
length = 0;
}

public int Length {
get { return length; }
}

public byte this[int index] {
get { return buffer[index]; }
}

public byte[] GetBuffer ()
{
return buffer;
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
void EnsureCapacity (int capacity)
{
if (capacity > buffer.Length) {
Expand All @@ -70,6 +83,11 @@ public void Append (byte[] text, int startIndex, int count)

public void Clear ()
{
if (buffer.Length > initialCapacity * 4) {
ArrayPool<byte>.Shared.Return (buffer);
buffer = ArrayPool<byte>.Shared.Rent (initialCapacity);
}

length = 0;
}

Expand Down
2 changes: 2 additions & 0 deletions MailKit/MailKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

<ItemGroup>
<Compile Include="Net\Imap\AsyncImapClient.cs" />
<Compile Include="Net\Imap\HashCode.cs" Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' " />
<Compile Include="Net\Imap\IImapClient.cs" />
<Compile Include="Net\Imap\IImapFolder.cs" />
<Compile Include="Net\Imap\ImapAuthenticationSecretDetector.cs" />
Expand All @@ -89,6 +90,7 @@
<Compile Include="Net\Imap\ImapSearchQueryOptimizer.cs" />
<Compile Include="Net\Imap\ImapStream.cs" />
<Compile Include="Net\Imap\ImapToken.cs" />
<Compile Include="Net\Imap\ImapTokenCache.cs" />
<Compile Include="Net\Imap\ImapUtils.cs" />
<Compile Include="Net\Pop3\AsyncPop3Client.cs" />
<Compile Include="Net\Pop3\IPop3Client.cs" />
Expand Down
2 changes: 2 additions & 0 deletions MailKit/MailKitLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

<ItemGroup>
<Compile Include="Net\Imap\AsyncImapClient.cs" />
<Compile Include="Net\Imap\HashCode.cs" Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' " />
<Compile Include="Net\Imap\IImapClient.cs" />
<Compile Include="Net\Imap\IImapFolder.cs" />
<Compile Include="Net\Imap\ImapAuthenticationSecretDetector.cs" />
Expand All @@ -94,6 +95,7 @@
<Compile Include="Net\Imap\ImapSearchQueryOptimizer.cs" />
<Compile Include="Net\Imap\ImapStream.cs" />
<Compile Include="Net\Imap\ImapToken.cs" />
<Compile Include="Net\Imap\ImapTokenCache.cs" />
<Compile Include="Net\Imap\ImapUtils.cs" />
<Compile Include="Net\Pop3\AsyncPop3Client.cs" />
<Compile Include="Net\Pop3\IPop3Client.cs" />
Expand Down
Loading
Loading