From 0bb3f89dc13d61cc87859a2ce20d325fcbd95d0b Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Sun, 11 Aug 2024 19:46:33 +0200 Subject: [PATCH] Performance improvement for .NET 9.0+ --- .../DefaultZookeeperBus.cs | 6 +- .../EasyCaching.Bus.Zookeeper.csproj | 2 +- .../DistributedLock/DistributedLock.cs | 2 +- .../DistributedLock/MemoryLock.cs | 2 +- .../DistributedLock/RefCounter.cs | 59 ------------------- src/EasyCaching.Core/EasyCaching.Core.csproj | 11 +++- 6 files changed, 16 insertions(+), 66 deletions(-) delete mode 100644 src/EasyCaching.Core/DistributedLock/RefCounter.cs diff --git a/bus/EasyCaching.Bus.Zookeeper/DefaultZookeeperBus.cs b/bus/EasyCaching.Bus.Zookeeper/DefaultZookeeperBus.cs index e314bf79..c7cb7999 100644 --- a/bus/EasyCaching.Bus.Zookeeper/DefaultZookeeperBus.cs +++ b/bus/EasyCaching.Bus.Zookeeper/DefaultZookeeperBus.cs @@ -41,7 +41,7 @@ public class DefaultZookeeperBus : EasyCachingAbstractBus /// /// lock /// - private readonly object _zkEventLock = new object(); + private readonly Lock _zkEventLock = new Lock(); /// /// The serializer. @@ -214,7 +214,7 @@ private async Task SubscribeDataChange(WatchedEvent @event) /// private async Task ReZkConnect() { - if (!Monitor.TryEnter(_zkEventLock, _zkBusOptions.ConnectionTimeout)) + if (!_zkEventLock.TryEnter(_zkBusOptions.ConnectionTimeout)) return; try { @@ -234,7 +234,7 @@ private async Task ReZkConnect() } finally { - Monitor.Exit(_zkEventLock); + _zkEventLock.Exit(); } } diff --git a/bus/EasyCaching.Bus.Zookeeper/EasyCaching.Bus.Zookeeper.csproj b/bus/EasyCaching.Bus.Zookeeper/EasyCaching.Bus.Zookeeper.csproj index 5d754db8..8459c1e8 100644 --- a/bus/EasyCaching.Bus.Zookeeper/EasyCaching.Bus.Zookeeper.csproj +++ b/bus/EasyCaching.Bus.Zookeeper/EasyCaching.Bus.Zookeeper.csproj @@ -3,7 +3,7 @@ - netstandard2.0;net6.0 + netstandard2.0;net6.0;net9.0 ncc;Catcher Wong ncc;Catcher Wong $(EasyCachingZookeeperBusPackageVersion) diff --git a/src/EasyCaching.Core/DistributedLock/DistributedLock.cs b/src/EasyCaching.Core/DistributedLock/DistributedLock.cs index 4ae267c0..ec93bd05 100644 --- a/src/EasyCaching.Core/DistributedLock/DistributedLock.cs +++ b/src/EasyCaching.Core/DistributedLock/DistributedLock.cs @@ -9,7 +9,7 @@ namespace EasyCaching.Core.DistributedLock public class DistributedLock : MemoryLock { private readonly IDistributedLockProvider _provider; - private readonly object _syncObj = new object(); + private readonly Lock _syncObj = new Lock(); private readonly DistributedLockOptions _options; private readonly ILogger _logger; diff --git a/src/EasyCaching.Core/DistributedLock/MemoryLock.cs b/src/EasyCaching.Core/DistributedLock/MemoryLock.cs index c1d01f57..ac263e2a 100644 --- a/src/EasyCaching.Core/DistributedLock/MemoryLock.cs +++ b/src/EasyCaching.Core/DistributedLock/MemoryLock.cs @@ -16,7 +16,7 @@ public class MemoryLock : IDistributedLock public string Key { get; } - private readonly object _syncObj = new object(); + private readonly Lock _syncObj = new Lock(); public MemoryLock(string key) => Key = key; diff --git a/src/EasyCaching.Core/DistributedLock/RefCounter.cs b/src/EasyCaching.Core/DistributedLock/RefCounter.cs deleted file mode 100644 index d5f8c189..00000000 --- a/src/EasyCaching.Core/DistributedLock/RefCounter.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Threading; - -namespace EasyCaching.Core.DistributedLock -{ - internal class RefCounter - { - private int _refCount = 1; - - public RefCounter(T value) => Value = value; - - public T Value { get; } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Increment() => Interlocked.Increment(ref _refCount); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Decrement() => Interlocked.Decrement(ref _refCount); - } - - internal class RefCounterPool where TValue : class - { - private readonly IDictionary> _dictionary; - - public RefCounterPool() => _dictionary = new Dictionary>(); - - public TValue GetOrAdd(TKey key, Func valueFactory) - { - if (valueFactory == null) throw new ArgumentNullException(nameof(valueFactory)); - - RefCounter item; - lock (_dictionary) - { - if (!_dictionary.TryGetValue(key, out item)) - return (_dictionary[key] = new RefCounter(valueFactory(key))).Value; - } - - item.Increment(); - - return item.Value; - } - - public TValue TryRemove(TKey key) - { - RefCounter item; - - lock (_dictionary) - { - if (!_dictionary.TryGetValue(key, out item) || item.Decrement() > 0) return null; - - _dictionary.Remove(key); - } - - return item.Value; - } - } -} diff --git a/src/EasyCaching.Core/EasyCaching.Core.csproj b/src/EasyCaching.Core/EasyCaching.Core.csproj index 34d1a4d7..378653a2 100644 --- a/src/EasyCaching.Core/EasyCaching.Core.csproj +++ b/src/EasyCaching.Core/EasyCaching.Core.csproj @@ -2,7 +2,8 @@ - netstandard2.0;net6.0 + netstandard2.0;net6.0;net9.0 + Preview ncc;Catcher Wong ncc;Catcher Wong $(EasyCachingCorePackageVersion) @@ -36,12 +37,20 @@ + + + + + + + +