Merge pull request #10542 from jtattermusch/intptr_avoid_boxing

Avoid boxing of IntPtr in CompletionRegistry
pull/9626/head
Jan Tattermusch 8 years ago committed by GitHub
commit 830cfd4c58
  1. 18
      src/csharp/Grpc.Core/Internal/CompletionRegistry.cs

@ -51,7 +51,7 @@ namespace Grpc.Core.Internal
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<CompletionRegistry>(); static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<CompletionRegistry>();
readonly GrpcEnvironment environment; readonly GrpcEnvironment environment;
readonly ConcurrentDictionary<IntPtr, OpCompletionDelegate> dict = new ConcurrentDictionary<IntPtr, OpCompletionDelegate>(); readonly ConcurrentDictionary<IntPtr, OpCompletionDelegate> dict = new ConcurrentDictionary<IntPtr, OpCompletionDelegate>(new IntPtrComparer());
public CompletionRegistry(GrpcEnvironment environment) public CompletionRegistry(GrpcEnvironment environment)
{ {
@ -121,5 +121,21 @@ namespace Grpc.Core.Internal
} }
} }
} }
/// <summary>
/// IntPtr doesn't implement <c>IEquatable{IntPtr}</c> so we need to use custom comparer to avoid boxing.
/// </summary>
private class IntPtrComparer : IEqualityComparer<IntPtr>
{
public bool Equals(IntPtr x, IntPtr y)
{
return x == y;
}
public int GetHashCode(IntPtr obj)
{
return obj.GetHashCode();
}
}
} }
} }

Loading…
Cancel
Save