Skip to content

Avoid unnecessary locking via MethodImplOptions.Synchronized #2225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 29 additions & 72 deletions src/NHibernate/Stat/StatisticsImpl.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

using NHibernate.Cache;
using NHibernate.Engine;
using NHibernate.Util;
using System.Linq;

namespace NHibernate.Stat
{
public class StatisticsImpl : IStatistics, IStatisticsImplementor
{
private object _syncRoot;
private readonly object _syncRoot = new object();

private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(StatisticsImpl));
private readonly ISessionFactoryImplementor sessionFactory;
Expand Down Expand Up @@ -81,17 +76,6 @@ public StatisticsImpl(ISessionFactoryImplementor sessionFactory)
this.sessionFactory = sessionFactory;
}

private object SyncRoot
{
get
{
if (_syncRoot == null)
Interlocked.CompareExchange(ref _syncRoot, new object(), null);

return _syncRoot;
}
}

#region IStatistics Members

public long EntityDeleteCount
Expand Down Expand Up @@ -305,10 +289,9 @@ public long OptimisticFailureCount
get { return optimisticFailureCount; }
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void Clear()
{
lock (SyncRoot)
lock (_syncRoot)
{
secondLevelCacheHitCount = 0;
secondLevelCacheMissCount = 0;
Expand Down Expand Up @@ -355,10 +338,9 @@ public void Clear()
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public EntityStatistics GetEntityStatistics(string entityName)
{
lock (SyncRoot)
lock (_syncRoot)
{
EntityStatistics es;
if (!entityStatistics.TryGetValue(entityName, out es))
Expand All @@ -370,10 +352,9 @@ public EntityStatistics GetEntityStatistics(string entityName)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public CollectionStatistics GetCollectionStatistics(string role)
{
lock (SyncRoot)
lock (_syncRoot)
{
CollectionStatistics cs;
if (!collectionStatistics.TryGetValue(role, out cs))
Expand All @@ -385,10 +366,9 @@ public CollectionStatistics GetCollectionStatistics(string role)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public SecondLevelCacheStatistics GetSecondLevelCacheStatistics(string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
SecondLevelCacheStatistics slcs;

Expand All @@ -406,10 +386,9 @@ public SecondLevelCacheStatistics GetSecondLevelCacheStatistics(string regionNam
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public QueryStatistics GetQueryStatistics(string queryString)
{
lock (SyncRoot)
lock (_syncRoot)
{
QueryStatistics qs;
if (!queryStatistics.TryGetValue(queryString, out qs))
Expand Down Expand Up @@ -460,10 +439,9 @@ public TimeSpan OperationThreshold
{
return operationThreshold;
}
[MethodImpl(MethodImplOptions.Synchronized)]
set
{
lock (SyncRoot)
lock (_syncRoot)
{
operationThreshold = value;
}
Expand All @@ -474,46 +452,41 @@ public TimeSpan OperationThreshold

#region IStatisticsImplementor Members

[MethodImpl(MethodImplOptions.Synchronized)]
public void OpenSession()
{
lock (SyncRoot)
lock (_syncRoot)
{
sessionOpenCount++;
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void CloseSession()
{
lock (SyncRoot)
lock (_syncRoot)
{
sessionCloseCount++;
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void Flush()
{
lock (SyncRoot)
lock (_syncRoot)
{
flushCount++;
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void Connect()
{
lock (SyncRoot)
lock (_syncRoot)
{
connectCount++;
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void LoadEntity(string entityName, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
entityLoadCount++;
GetEntityStatistics(entityName).loadCount++;
Expand All @@ -524,10 +497,9 @@ public void LoadEntity(string entityName, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void FetchEntity(string entityName, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
entityFetchCount++;
GetEntityStatistics(entityName).fetchCount++;
Expand All @@ -538,10 +510,9 @@ public void FetchEntity(string entityName, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void UpdateEntity(string entityName, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
entityUpdateCount++;
GetEntityStatistics(entityName).updateCount++;
Expand All @@ -552,10 +523,9 @@ public void UpdateEntity(string entityName, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void InsertEntity(string entityName, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
entityInsertCount++;
GetEntityStatistics(entityName).insertCount++;
Expand All @@ -566,10 +536,9 @@ public void InsertEntity(string entityName, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void DeleteEntity(string entityName, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
entityDeleteCount++;
GetEntityStatistics(entityName).deleteCount++;
Expand All @@ -580,10 +549,9 @@ public void DeleteEntity(string entityName, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void LoadCollection(string role, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
collectionLoadCount++;
GetCollectionStatistics(role).loadCount++;
Expand All @@ -594,10 +562,9 @@ public void LoadCollection(string role, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void FetchCollection(string role, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
collectionFetchCount++;
GetCollectionStatistics(role).fetchCount++;
Expand All @@ -608,10 +575,9 @@ public void FetchCollection(string role, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void UpdateCollection(string role, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
collectionUpdateCount++;
GetCollectionStatistics(role).updateCount++;
Expand All @@ -622,10 +588,9 @@ public void UpdateCollection(string role, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void RecreateCollection(string role, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
collectionRecreateCount++;
GetCollectionStatistics(role).recreateCount++;
Expand All @@ -636,24 +601,22 @@ public void RecreateCollection(string role, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void RemoveCollection(string role, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
collectionRemoveCount++;
GetCollectionStatistics(role).removeCount++;
}
if (operationThreshold < time)
{
LogOperation(OperationRecreateCollection, role, time);
LogOperation(OperationRemoveCollection, role, time);
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void SecondLevelCachePut(string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
SecondLevelCacheStatistics slc = GetSecondLevelCacheStatistics(regionName);
if (slc != null)
Expand All @@ -664,10 +627,9 @@ public void SecondLevelCachePut(string regionName)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void SecondLevelCacheHit(string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
SecondLevelCacheStatistics slc = GetSecondLevelCacheStatistics(regionName);
if (slc != null)
Expand All @@ -678,10 +640,9 @@ public void SecondLevelCacheHit(string regionName)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void SecondLevelCacheMiss(string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
SecondLevelCacheStatistics slc = GetSecondLevelCacheStatistics(regionName);
if (slc != null)
Expand All @@ -692,10 +653,9 @@ public void SecondLevelCacheMiss(string regionName)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void QueryExecuted(string hql, int rows, TimeSpan time)
{
lock (SyncRoot)
lock (_syncRoot)
{
queryExecutionCount++;
if (queryExecutionMaxTime < time)
Expand All @@ -715,10 +675,9 @@ public void QueryExecuted(string hql, int rows, TimeSpan time)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void QueryCacheHit(string hql, string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
queryCacheHitCount++;
if (hql != null)
Expand All @@ -734,10 +693,9 @@ public void QueryCacheHit(string hql, string regionName)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void QueryCacheMiss(string hql, string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
queryCacheMissCount++;
if (hql != null)
Expand All @@ -753,10 +711,9 @@ public void QueryCacheMiss(string hql, string regionName)
}
}

[MethodImpl(MethodImplOptions.Synchronized)]
public void QueryCachePut(string hql, string regionName)
{
lock (SyncRoot)
lock (_syncRoot)
{
queryCachePutCount++;
if (hql != null)
Expand Down
Loading