Skip to content

Commit 7096c7f

Browse files
committed
Avoid unnecessary locking in SoftLimitMRUCache
1 parent 48b3bb9 commit 7096c7f

File tree

2 files changed

+11
-46
lines changed

2 files changed

+11
-46
lines changed

src/NHibernate/Util/SimpleMRUCache.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.Serialization;
4-
using System.Threading;
53

64
namespace NHibernate.Util
75
{
@@ -19,7 +17,7 @@ public class SimpleMRUCache : IDeserializationCallback
1917
{
2018
private const int DefaultStrongRefCount = 128;
2119

22-
private object _syncRoot;
20+
private readonly object _syncRoot = new object();
2321

2422
private readonly int strongReferenceCount;
2523

@@ -35,17 +33,6 @@ public SimpleMRUCache(int strongReferenceCount)
3533
cache = new LRUMap(strongReferenceCount);
3634
}
3735

38-
private object SyncRoot
39-
{
40-
get
41-
{
42-
if (_syncRoot == null)
43-
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
44-
45-
return _syncRoot;
46-
}
47-
}
48-
4936
#region IDeserializationCallback Members
5037

5138
void IDeserializationCallback.OnDeserialization(object sender)
@@ -57,41 +44,37 @@ void IDeserializationCallback.OnDeserialization(object sender)
5744

5845
public object this[object key]
5946
{
60-
[MethodImpl(MethodImplOptions.Synchronized)]
6147
get
6248
{
63-
lock (SyncRoot)
49+
lock (_syncRoot)
6450
{
6551
return cache[key];
6652
}
6753
}
6854
}
6955

70-
[MethodImpl(MethodImplOptions.Synchronized)]
7156
public void Put(object key, object value)
7257
{
73-
lock (SyncRoot)
58+
lock (_syncRoot)
7459
{
7560
cache.Add(key, value);
7661
}
7762
}
7863

7964
public int Count
8065
{
81-
[MethodImpl(MethodImplOptions.Synchronized)]
8266
get
8367
{
84-
lock (SyncRoot)
68+
lock (_syncRoot)
8569
{
8670
return cache.Count;
8771
}
8872
}
8973
}
9074

91-
[MethodImpl(MethodImplOptions.Synchronized)]
9275
public void Clear()
9376
{
94-
lock (SyncRoot)
77+
lock (_syncRoot)
9578
{
9679
cache.Clear();
9780
}

src/NHibernate/Util/SoftLimitMRUCache.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections;
3-
using System.Runtime.CompilerServices;
43
using System.Runtime.Serialization;
5-
using System.Threading;
64

75
namespace NHibernate.Util
86
{
@@ -25,7 +23,7 @@ namespace NHibernate.Util
2523
public class SoftLimitMRUCache : IDeserializationCallback
2624
{
2725
private const int DefaultStrongRefCount = 128;
28-
private object _syncRoot;
26+
private readonly object _syncRoot = new object();
2927

3028
private readonly int strongReferenceCount;
3129

@@ -50,17 +48,6 @@ public SoftLimitMRUCache(int strongReferenceCount)
5048
public SoftLimitMRUCache()
5149
: this(DefaultStrongRefCount) {}
5250

53-
private object SyncRoot
54-
{
55-
get
56-
{
57-
if (_syncRoot == null)
58-
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
59-
60-
return _syncRoot;
61-
}
62-
}
63-
6451
#region IDeserializationCallback Members
6552

6653
void IDeserializationCallback.OnDeserialization(object sender)
@@ -72,10 +59,9 @@ void IDeserializationCallback.OnDeserialization(object sender)
7259

7360
public object this[object key]
7461
{
75-
[MethodImpl(MethodImplOptions.Synchronized)]
7662
get
7763
{
78-
lock (SyncRoot)
64+
lock (_syncRoot)
7965
{
8066
object result = softReferenceCache[key];
8167
if (result != null)
@@ -87,10 +73,9 @@ public object this[object key]
8773
}
8874
}
8975

90-
[MethodImpl(MethodImplOptions.Synchronized)]
9176
public void Put(object key, object value)
9277
{
93-
lock (SyncRoot)
78+
lock (_syncRoot)
9479
{
9580
softReferenceCache[key] = value;
9681
strongReferenceCache[key] = value;
@@ -99,10 +84,9 @@ public void Put(object key, object value)
9984

10085
public int Count
10186
{
102-
[MethodImpl(MethodImplOptions.Synchronized)]
10387
get
10488
{
105-
lock (SyncRoot)
89+
lock (_syncRoot)
10690
{
10791
return strongReferenceCache.Count;
10892
}
@@ -111,20 +95,18 @@ public int Count
11195

11296
public int SoftCount
11397
{
114-
[MethodImpl(MethodImplOptions.Synchronized)]
11598
get
11699
{
117-
lock (SyncRoot)
100+
lock (_syncRoot)
118101
{
119102
return softReferenceCache.Count;
120103
}
121104
}
122105
}
123106

124-
[MethodImpl(MethodImplOptions.Synchronized)]
125107
public void Clear()
126108
{
127-
lock (SyncRoot)
109+
lock (_syncRoot)
128110
{
129111
strongReferenceCache.Clear();
130112
softReferenceCache.Clear();

0 commit comments

Comments
 (0)