Skip to content

Commit 13b4aa6

Browse files
VAllensnnhy
authored andcommitted
perf: use System.Threading.Lock for .NET 9+
Replace lock(object)/Monitor with System.Threading.Lock for .NET 9 and above to reduce lock overhead in uncontended scenarios. Benchmark results show improvements in lightweight operations (Get/Exists/TTL) with no regression in write-heavy paths.
1 parent a0563a6 commit 13b4aa6

27 files changed

Lines changed: 117 additions & 1 deletion

Benchmark/Benchmark.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<OutputPath>..\Bin\Benchmark</OutputPath>
77
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
88
<ImplicitUsings>enable</ImplicitUsings>

NewLife.NovaDb/Client/EmbeddedDatabase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ internal sealed class EmbeddedDatabase
1616
private readonly DbOptions _options;
1717
private readonly ConcurrentDictionary<String, KvStore> _kvStores = new(StringComparer.OrdinalIgnoreCase);
1818
private FluxEngine? _fluxEngine;
19+
#if NET9_0_OR_GREATER
20+
private readonly System.Threading.Lock _fluxLock = new();
21+
#else
1922
private readonly Object _fluxLock = new();
23+
#endif
2024

2125
/// <summary>数据库路径</summary>
2226
public String DbPath => _dbPath;

NewLife.NovaDb/Cluster/FailoverManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ namespace NewLife.NovaDb.Cluster;
1212
public class FailoverManager
1313
{
1414
private readonly ReplicationManager _replication;
15+
#if NET9_0_OR_GREATER
16+
private readonly System.Threading.Lock _lock = new();
17+
#else
1518
private readonly Object _lock = new();
19+
#endif
1620

1721
/// <summary>允许切换的最大复制延迟(LSN 差值)。超过此值拒绝切换,避免数据丢失。默认 100</summary>
1822
public UInt64 MaxAllowedLag { get; set; } = 100;

NewLife.NovaDb/Cluster/ReplicaClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public class ReplicaClient : IDisposable
2020
private UInt64 _lastAppliedLsn;
2121
private readonly List<WalRecord> _appliedRecords = [];
2222
private readonly Action<UInt64, Byte[]>? _applyCallback;
23+
#if NET9_0_OR_GREATER
24+
private readonly System.Threading.Lock _lock = new();
25+
#else
2326
private readonly Object _lock = new();
27+
#endif
2428
private Boolean _isConnected;
2529
private Boolean _disposed;
2630

NewLife.NovaDb/Cluster/ReplicationManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public class ReplicationManager : IDisposable
2020
private readonly Dictionary<String, NodeInfo> _slaves = [];
2121
private readonly Dictionary<String, UInt64> _slavePositions = [];
2222
private readonly Dictionary<String, ApiClient> _slaveClients = [];
23+
#if NET9_0_OR_GREATER
24+
private readonly System.Threading.Lock _lock = new();
25+
#else
2326
private readonly Object _lock = new();
27+
#endif
2428
private Boolean _disposed;
2529

2630
private readonly List<WalRecord> _replicationBuffer = [];

NewLife.NovaDb/Core/SlowQueryLog.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public IReadOnlyList<SlowQueryEntry> RecentEntries
3939
#region 字段
4040
private Int64 _totalCount;
4141
private readonly LinkedList<SlowQueryEntry> _entries = new();
42+
#if NET9_0_OR_GREATER
43+
private readonly System.Threading.Lock _lock = new();
44+
#else
4245
private readonly Object _lock = new();
46+
#endif
4347
#endregion
4448

4549
#region 日志

NewLife.NovaDb/Engine/ColdIndexDirectory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public class ColdIndexDirectory
2020
{
2121
private readonly Int32 _anchorInterval;
2222
private readonly List<ColdDirectoryEntry> _anchors = [];
23+
#if NET9_0_OR_GREATER
24+
private readonly System.Threading.Lock _lock = new();
25+
#else
2326
private readonly Object _lock = new();
27+
#endif
2428

2529
/// <summary>锚点间隔(行数),默认 1000 行一个锚点</summary>
2630
public Int32 AnchorInterval => _anchorInterval;

NewLife.NovaDb/Engine/Flux/ConsumerGroup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ public class ConsumerGroup
2626
public MessageId? LastDeliveredId { get; set; }
2727

2828
private readonly Dictionary<String, PendingEntry> _pendingEntries = [];
29+
#if NET9_0_OR_GREATER
30+
private readonly System.Threading.Lock _lock = new();
31+
#else
2932
private readonly Object _lock = new();
33+
#endif
3034

3135
/// <summary>创建消费组</summary>
3236
/// <param name="name">消费组名称</param>

NewLife.NovaDb/Engine/Flux/FluxEngine.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ public partial class FluxEngine : IDisposable
88
private readonly String _basePath;
99
private readonly DbOptions _options;
1010
private readonly SortedDictionary<String, List<FluxEntry>> _partitions = new(StringComparer.Ordinal);
11+
#if NET9_0_OR_GREATER
12+
private readonly System.Threading.Lock _lock = new();
13+
#else
1114
private readonly Object _lock = new();
15+
#endif
1216
private Boolean _disposed;
1317

1418
/// <summary>创建 FluxEngine 实例</summary>

NewLife.NovaDb/Engine/HotIndexManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public class HotIndexManager
4848
{
4949
private readonly HotSegmentConfig _config;
5050
private readonly SkipList<ComparableObject, IndexSegment> _hotSegments;
51+
#if NET9_0_OR_GREATER
52+
private readonly System.Threading.Lock _lock = new();
53+
#else
5154
private readonly Object _lock = new();
55+
#endif
5256
private DateTime _lastHeatCheck = DateTime.UtcNow;
5357

5458
/// <summary>配置</summary>

0 commit comments

Comments
 (0)