From eec8b84c71bc81a7073142506f369a9b8a3a63a6 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Nov 2017 20:23:30 +0100 Subject: spinlock in completion registry is slightly faster --- .../Grpc.Core/Internal/CompletionRegistry.cs | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/csharp/Grpc.Core') diff --git a/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs b/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs index f4aea685c3..b68655b33c 100644 --- a/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs +++ b/src/csharp/Grpc.Core/Internal/CompletionRegistry.cs @@ -19,7 +19,9 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.InteropServices; +using System.Threading; using Grpc.Core.Logging; using Grpc.Core.Utils; @@ -35,7 +37,7 @@ namespace Grpc.Core.Internal readonly GrpcEnvironment environment; readonly Dictionary dict = new Dictionary(new IntPtrComparer()); - readonly object myLock = new object(); + SpinLock spinLock = new SpinLock(Debugger.IsAttached); IntPtr lastRegisteredKey; // only for testing public CompletionRegistry(GrpcEnvironment environment) @@ -46,11 +48,19 @@ namespace Grpc.Core.Internal public void Register(IntPtr key, IOpCompletionCallback callback) { environment.DebugStats.PendingBatchCompletions.Increment(); - lock (myLock) + + bool lockTaken = false; + try { + spinLock.Enter(ref lockTaken); + dict.Add(key, callback); this.lastRegisteredKey = key; } + finally + { + if (lockTaken) spinLock.Exit(); + } } public void RegisterBatchCompletion(BatchContextSafeHandle ctx, BatchCompletionDelegate callback, object state) @@ -68,11 +78,18 @@ namespace Grpc.Core.Internal public IOpCompletionCallback Extract(IntPtr key) { IOpCompletionCallback value = null; - lock (myLock) + bool lockTaken = false; + try { + spinLock.Enter(ref lockTaken); + value = dict[key]; dict.Remove(key); } + finally + { + if (lockTaken) spinLock.Exit(); + } environment.DebugStats.PendingBatchCompletions.Decrement(); return value; } -- cgit v1.2.3