Skip to content

Commit 43ed3ba

Browse files
Speed optimization for .NET 9.0+ through System.Threading.Lock
1 parent 2e420f0 commit 43ed3ba

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

BitFaster.Caching/Atomic/AsyncAtomicFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ private async ValueTask<V> CreateValueAsync<TFactory>(K key, TFactory valueFacto
137137

138138
private class Initializer
139139
{
140+
private readonly Lock locker = LockFactory.Create();
140141
private bool isInitialized;
141142
private Task<V>? valueTask;
142143

@@ -178,7 +179,7 @@ private Task<V> DoubleCheck(Task<V> value)
178179
return valueTask!;
179180
}
180181

181-
lock (this)
182+
lock (locker)
182183
{
183184
if (!isInitialized)
184185
{

BitFaster.Caching/Atomic/AtomicFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,14 @@ public override int GetHashCode()
159159
#pragma warning disable CA2002 // Do not lock on objects with weak identity
160160
private class Initializer
161161
{
162+
private readonly Lock locker = LockFactory.Create();
162163
private bool isInitialized;
163164
private V? value;
164165
private ExceptionDispatchInfo? exceptionDispatch;
165166

166167
public V CreateValue<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, V>
167168
{
168-
lock (this)
169+
lock (locker)
169170
{
170171
if (isInitialized)
171172
{

BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public void Dispose()
147147

148148
private class Initializer
149149
{
150+
private readonly Lock locker = LockFactory.Create();
150151
private bool isTaskInitialized;
151152
private bool isTaskCompleted;
152153
private bool isDisposeRequested;
@@ -197,7 +198,7 @@ private Task<Scoped<V>> DoubleCheck(Task<Scoped<V>> value)
197198
return task!;
198199
}
199200

200-
lock (this)
201+
lock (locker)
201202
{
202203
if (!isTaskInitialized)
203204
{

BitFaster.Caching/Atomic/ScopedAtomicFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ public void Dispose()
162162
#pragma warning disable CA2002 // Do not lock on objects with weak identity
163163
private class Initializer
164164
{
165+
private readonly Lock locker = LockFactory.Create();
165166
private bool isInitialized;
166167
private Scoped<V>? value;
167168

168169
public Scoped<V> CreateScope<TFactory>(K key, TFactory valueFactory) where TFactory : struct, IValueFactory<K, Scoped<V>>
169170
{
170-
lock (this)
171+
lock (locker)
171172
{
172173
if (isInitialized)
173174
{
@@ -183,7 +184,7 @@ public Scoped<V> CreateScope<TFactory>(K key, TFactory valueFactory) where TFact
183184

184185
public Scoped<V> TryCreateDisposedScope()
185186
{
186-
lock (this)
187+
lock (locker)
187188
{
188189
if (isInitialized)
189190
{

BitFaster.Caching/BitFaster.Caching.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0</TargetFrameworks>
5-
<LangVersion>11.0</LangVersion>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0;net9.0</TargetFrameworks>
5+
<LangVersion>latest</LangVersion>
66
<Authors>Alex Peck</Authors>
77
<Company />
88
<Product>BitFaster.Caching</Product>
@@ -54,7 +54,11 @@
5454

5555
<ItemGroup>
5656
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
57-
</ItemGroup>
57+
<PackageReference Include="Backport.System.Threading.Lock" Version="3.1.4" />
58+
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
59+
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
60+
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
61+
</ItemGroup>
5862

5963
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
6064
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed class ClassicLru<K, V> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IB
2424
private readonly int capacity;
2525
private readonly ConcurrentDictionary<K, LinkedListNode<LruItem>> dictionary;
2626
private readonly LinkedList<LruItem> linkedList = new();
27+
private readonly Lock locker = LockFactory.Create();
2728

2829
private readonly CacheMetrics metrics = new();
2930
private readonly CachePolicy policy;
@@ -120,7 +121,7 @@ private bool TryAdd(K key, V value)
120121
{
121122
LinkedListNode<LruItem>? first = null;
122123

123-
lock (this.linkedList)
124+
lock (this.locker)
124125
{
125126
if (linkedList.Count >= capacity)
126127
{
@@ -303,7 +304,7 @@ private void OnRemove(LinkedListNode<LruItem> node)
303304
// the List & Dictionary. Now thread A will try to move x to the end of the list.
304305
if (node.List != null)
305306
{
306-
lock (this.linkedList)
307+
lock (this.locker)
307308
{
308309
if (node.List != null)
309310
{
@@ -350,7 +351,7 @@ public void AddOrUpdate(K key, V value)
350351
{
351352
LinkedListNode<LruItem>? first = null;
352353

353-
lock (this.linkedList)
354+
lock (this.locker)
354355
{
355356
if (linkedList.Count >= capacity)
356357
{
@@ -410,7 +411,7 @@ public void Trim(int itemCount)
410411
{
411412
LinkedListNode<LruItem>? first = null;
412413

413-
lock (this.linkedList)
414+
lock (this.locker)
414415
{
415416
if (linkedList.Count > 0)
416417
{
@@ -440,7 +441,7 @@ private void LockAndMoveToEnd(LinkedListNode<LruItem> node)
440441
return;
441442
}
442443

443-
lock (this.linkedList)
444+
lock (this.locker)
444445
{
445446
if (node.List == null)
446447
{

0 commit comments

Comments
 (0)