Skip to content

Commit 8a8c3f2

Browse files
bahusoidhazzik
authored andcommitted
Avoid unnecessary locking via MethodImplOptions.Synchronized (nhibernate#2225)
* Avoid unnecessary locking in SoftLimitMRUCache * Avoid unnecessary locking in SimpleMRUCache.cs * Avoid unnecessary locking in StatisticsImpl
1 parent 48b3bb9 commit 8a8c3f2

File tree

3 files changed

+40
-118
lines changed

3 files changed

+40
-118
lines changed

src/NHibernate/Stat/StatisticsImpl.cs

+29-72
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Runtime.CompilerServices;
43
using System.Text;
5-
using System.Threading;
6-
7-
using NHibernate.Cache;
84
using NHibernate.Engine;
9-
using NHibernate.Util;
105
using System.Linq;
116

127
namespace NHibernate.Stat
138
{
149
public class StatisticsImpl : IStatistics, IStatisticsImplementor
1510
{
16-
private object _syncRoot;
11+
private readonly object _syncRoot = new object();
1712

1813
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(StatisticsImpl));
1914
private readonly ISessionFactoryImplementor sessionFactory;
@@ -81,17 +76,6 @@ public StatisticsImpl(ISessionFactoryImplementor sessionFactory)
8176
this.sessionFactory = sessionFactory;
8277
}
8378

84-
private object SyncRoot
85-
{
86-
get
87-
{
88-
if (_syncRoot == null)
89-
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
90-
91-
return _syncRoot;
92-
}
93-
}
94-
9579
#region IStatistics Members
9680

9781
public long EntityDeleteCount
@@ -305,10 +289,9 @@ public long OptimisticFailureCount
305289
get { return optimisticFailureCount; }
306290
}
307291

308-
[MethodImpl(MethodImplOptions.Synchronized)]
309292
public void Clear()
310293
{
311-
lock (SyncRoot)
294+
lock (_syncRoot)
312295
{
313296
secondLevelCacheHitCount = 0;
314297
secondLevelCacheMissCount = 0;
@@ -355,10 +338,9 @@ public void Clear()
355338
}
356339
}
357340

358-
[MethodImpl(MethodImplOptions.Synchronized)]
359341
public EntityStatistics GetEntityStatistics(string entityName)
360342
{
361-
lock (SyncRoot)
343+
lock (_syncRoot)
362344
{
363345
EntityStatistics es;
364346
if (!entityStatistics.TryGetValue(entityName, out es))
@@ -370,10 +352,9 @@ public EntityStatistics GetEntityStatistics(string entityName)
370352
}
371353
}
372354

373-
[MethodImpl(MethodImplOptions.Synchronized)]
374355
public CollectionStatistics GetCollectionStatistics(string role)
375356
{
376-
lock (SyncRoot)
357+
lock (_syncRoot)
377358
{
378359
CollectionStatistics cs;
379360
if (!collectionStatistics.TryGetValue(role, out cs))
@@ -385,10 +366,9 @@ public CollectionStatistics GetCollectionStatistics(string role)
385366
}
386367
}
387368

388-
[MethodImpl(MethodImplOptions.Synchronized)]
389369
public SecondLevelCacheStatistics GetSecondLevelCacheStatistics(string regionName)
390370
{
391-
lock (SyncRoot)
371+
lock (_syncRoot)
392372
{
393373
SecondLevelCacheStatistics slcs;
394374

@@ -406,10 +386,9 @@ public SecondLevelCacheStatistics GetSecondLevelCacheStatistics(string regionNam
406386
}
407387
}
408388

409-
[MethodImpl(MethodImplOptions.Synchronized)]
410389
public QueryStatistics GetQueryStatistics(string queryString)
411390
{
412-
lock (SyncRoot)
391+
lock (_syncRoot)
413392
{
414393
QueryStatistics qs;
415394
if (!queryStatistics.TryGetValue(queryString, out qs))
@@ -460,10 +439,9 @@ public TimeSpan OperationThreshold
460439
{
461440
return operationThreshold;
462441
}
463-
[MethodImpl(MethodImplOptions.Synchronized)]
464442
set
465443
{
466-
lock (SyncRoot)
444+
lock (_syncRoot)
467445
{
468446
operationThreshold = value;
469447
}
@@ -474,46 +452,41 @@ public TimeSpan OperationThreshold
474452

475453
#region IStatisticsImplementor Members
476454

477-
[MethodImpl(MethodImplOptions.Synchronized)]
478455
public void OpenSession()
479456
{
480-
lock (SyncRoot)
457+
lock (_syncRoot)
481458
{
482459
sessionOpenCount++;
483460
}
484461
}
485462

486-
[MethodImpl(MethodImplOptions.Synchronized)]
487463
public void CloseSession()
488464
{
489-
lock (SyncRoot)
465+
lock (_syncRoot)
490466
{
491467
sessionCloseCount++;
492468
}
493469
}
494470

495-
[MethodImpl(MethodImplOptions.Synchronized)]
496471
public void Flush()
497472
{
498-
lock (SyncRoot)
473+
lock (_syncRoot)
499474
{
500475
flushCount++;
501476
}
502477
}
503478

504-
[MethodImpl(MethodImplOptions.Synchronized)]
505479
public void Connect()
506480
{
507-
lock (SyncRoot)
481+
lock (_syncRoot)
508482
{
509483
connectCount++;
510484
}
511485
}
512486

513-
[MethodImpl(MethodImplOptions.Synchronized)]
514487
public void LoadEntity(string entityName, TimeSpan time)
515488
{
516-
lock (SyncRoot)
489+
lock (_syncRoot)
517490
{
518491
entityLoadCount++;
519492
GetEntityStatistics(entityName).loadCount++;
@@ -524,10 +497,9 @@ public void LoadEntity(string entityName, TimeSpan time)
524497
}
525498
}
526499

527-
[MethodImpl(MethodImplOptions.Synchronized)]
528500
public void FetchEntity(string entityName, TimeSpan time)
529501
{
530-
lock (SyncRoot)
502+
lock (_syncRoot)
531503
{
532504
entityFetchCount++;
533505
GetEntityStatistics(entityName).fetchCount++;
@@ -538,10 +510,9 @@ public void FetchEntity(string entityName, TimeSpan time)
538510
}
539511
}
540512

541-
[MethodImpl(MethodImplOptions.Synchronized)]
542513
public void UpdateEntity(string entityName, TimeSpan time)
543514
{
544-
lock (SyncRoot)
515+
lock (_syncRoot)
545516
{
546517
entityUpdateCount++;
547518
GetEntityStatistics(entityName).updateCount++;
@@ -552,10 +523,9 @@ public void UpdateEntity(string entityName, TimeSpan time)
552523
}
553524
}
554525

555-
[MethodImpl(MethodImplOptions.Synchronized)]
556526
public void InsertEntity(string entityName, TimeSpan time)
557527
{
558-
lock (SyncRoot)
528+
lock (_syncRoot)
559529
{
560530
entityInsertCount++;
561531
GetEntityStatistics(entityName).insertCount++;
@@ -566,10 +536,9 @@ public void InsertEntity(string entityName, TimeSpan time)
566536
}
567537
}
568538

569-
[MethodImpl(MethodImplOptions.Synchronized)]
570539
public void DeleteEntity(string entityName, TimeSpan time)
571540
{
572-
lock (SyncRoot)
541+
lock (_syncRoot)
573542
{
574543
entityDeleteCount++;
575544
GetEntityStatistics(entityName).deleteCount++;
@@ -580,10 +549,9 @@ public void DeleteEntity(string entityName, TimeSpan time)
580549
}
581550
}
582551

583-
[MethodImpl(MethodImplOptions.Synchronized)]
584552
public void LoadCollection(string role, TimeSpan time)
585553
{
586-
lock (SyncRoot)
554+
lock (_syncRoot)
587555
{
588556
collectionLoadCount++;
589557
GetCollectionStatistics(role).loadCount++;
@@ -594,10 +562,9 @@ public void LoadCollection(string role, TimeSpan time)
594562
}
595563
}
596564

597-
[MethodImpl(MethodImplOptions.Synchronized)]
598565
public void FetchCollection(string role, TimeSpan time)
599566
{
600-
lock (SyncRoot)
567+
lock (_syncRoot)
601568
{
602569
collectionFetchCount++;
603570
GetCollectionStatistics(role).fetchCount++;
@@ -608,10 +575,9 @@ public void FetchCollection(string role, TimeSpan time)
608575
}
609576
}
610577

611-
[MethodImpl(MethodImplOptions.Synchronized)]
612578
public void UpdateCollection(string role, TimeSpan time)
613579
{
614-
lock (SyncRoot)
580+
lock (_syncRoot)
615581
{
616582
collectionUpdateCount++;
617583
GetCollectionStatistics(role).updateCount++;
@@ -622,10 +588,9 @@ public void UpdateCollection(string role, TimeSpan time)
622588
}
623589
}
624590

625-
[MethodImpl(MethodImplOptions.Synchronized)]
626591
public void RecreateCollection(string role, TimeSpan time)
627592
{
628-
lock (SyncRoot)
593+
lock (_syncRoot)
629594
{
630595
collectionRecreateCount++;
631596
GetCollectionStatistics(role).recreateCount++;
@@ -636,24 +601,22 @@ public void RecreateCollection(string role, TimeSpan time)
636601
}
637602
}
638603

639-
[MethodImpl(MethodImplOptions.Synchronized)]
640604
public void RemoveCollection(string role, TimeSpan time)
641605
{
642-
lock (SyncRoot)
606+
lock (_syncRoot)
643607
{
644608
collectionRemoveCount++;
645609
GetCollectionStatistics(role).removeCount++;
646610
}
647611
if (operationThreshold < time)
648612
{
649-
LogOperation(OperationRecreateCollection, role, time);
613+
LogOperation(OperationRemoveCollection, role, time);
650614
}
651615
}
652616

653-
[MethodImpl(MethodImplOptions.Synchronized)]
654617
public void SecondLevelCachePut(string regionName)
655618
{
656-
lock (SyncRoot)
619+
lock (_syncRoot)
657620
{
658621
SecondLevelCacheStatistics slc = GetSecondLevelCacheStatistics(regionName);
659622
if (slc != null)
@@ -664,10 +627,9 @@ public void SecondLevelCachePut(string regionName)
664627
}
665628
}
666629

667-
[MethodImpl(MethodImplOptions.Synchronized)]
668630
public void SecondLevelCacheHit(string regionName)
669631
{
670-
lock (SyncRoot)
632+
lock (_syncRoot)
671633
{
672634
SecondLevelCacheStatistics slc = GetSecondLevelCacheStatistics(regionName);
673635
if (slc != null)
@@ -678,10 +640,9 @@ public void SecondLevelCacheHit(string regionName)
678640
}
679641
}
680642

681-
[MethodImpl(MethodImplOptions.Synchronized)]
682643
public void SecondLevelCacheMiss(string regionName)
683644
{
684-
lock (SyncRoot)
645+
lock (_syncRoot)
685646
{
686647
SecondLevelCacheStatistics slc = GetSecondLevelCacheStatistics(regionName);
687648
if (slc != null)
@@ -692,10 +653,9 @@ public void SecondLevelCacheMiss(string regionName)
692653
}
693654
}
694655

695-
[MethodImpl(MethodImplOptions.Synchronized)]
696656
public void QueryExecuted(string hql, int rows, TimeSpan time)
697657
{
698-
lock (SyncRoot)
658+
lock (_syncRoot)
699659
{
700660
queryExecutionCount++;
701661
if (queryExecutionMaxTime < time)
@@ -715,10 +675,9 @@ public void QueryExecuted(string hql, int rows, TimeSpan time)
715675
}
716676
}
717677

718-
[MethodImpl(MethodImplOptions.Synchronized)]
719678
public void QueryCacheHit(string hql, string regionName)
720679
{
721-
lock (SyncRoot)
680+
lock (_syncRoot)
722681
{
723682
queryCacheHitCount++;
724683
if (hql != null)
@@ -734,10 +693,9 @@ public void QueryCacheHit(string hql, string regionName)
734693
}
735694
}
736695

737-
[MethodImpl(MethodImplOptions.Synchronized)]
738696
public void QueryCacheMiss(string hql, string regionName)
739697
{
740-
lock (SyncRoot)
698+
lock (_syncRoot)
741699
{
742700
queryCacheMissCount++;
743701
if (hql != null)
@@ -753,10 +711,9 @@ public void QueryCacheMiss(string hql, string regionName)
753711
}
754712
}
755713

756-
[MethodImpl(MethodImplOptions.Synchronized)]
757714
public void QueryCachePut(string hql, string regionName)
758715
{
759-
lock (SyncRoot)
716+
lock (_syncRoot)
760717
{
761718
queryCachePutCount++;
762719
if (hql != null)

0 commit comments

Comments
 (0)