perf: use System.Threading.Lock for .NET 9+ - #35
Merged
Conversation
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.
There was a problem hiding this comment.
Pull request overview
该 PR 在 NewLife.NovaDb 多个热点路径中,将 lock(object)/Monitor 的锁对象在 .NET 9+ 条件下替换为 System.Threading.Lock,以期降低无竞争场景下的锁开销,并同步调整 Benchmark 项目以跑在更高版本框架上验证收益。
Changes:
- 在 .NET 9+(
NET9_0_OR_GREATER)下,将大量私有/静态锁字段从Object切换为System.Threading.Lock。 - 覆盖 WAL、事务、存储、引擎、集群、服务端控制器、队列等多个模块的锁字段声明。
- Benchmark 项目 TargetFramework 从
net8.0调整为net10.0。
Reviewed changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| NewLife.NovaDb/WAL/WalWriter.cs | WAL 写入器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/WAL/WalCheckpointer.cs | WAL 检查点管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/WAL/BinlogWriter.cs | Binlog 写入器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Tx/TransactionManager.cs | 事务管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Tx/Transaction.cs | 事务对象锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Storage/PageCache.cs | 页缓存锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Storage/MmfPager.cs | MMF Pager 锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Storage/DatabaseManager.cs | 数据库管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Server/NovaController.cs | 服务器端事务字典静态锁在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Server/FluxController.cs | 共享消费组集合静态锁在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Server/AuthManager.cs | 认证管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Queues/NovaQueue.cs | 队列内部多把锁在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/SkipList.cs | SkipList 内部锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/ShardManager.cs | ShardManager 锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/NovaTable.cs | NovaTable 锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/MaterializedViewManager.cs | 物化视图管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/KV/KvStore.cs | KV 写入串行化锁在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/HotIndexManager.cs | 热点索引管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/Flux/FluxEngine.cs | FluxEngine 锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/Flux/ConsumerGroup.cs | ConsumerGroup 锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Engine/ColdIndexDirectory.cs | 冷索引目录锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Core/SlowQueryLog.cs | 慢查询日志锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Cluster/ReplicationManager.cs | 复制管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Cluster/ReplicaClient.cs | 副本客户端锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Cluster/FailoverManager.cs | 故障切换管理器锁字段在 NET9+ 下改用 System.Threading.Lock |
| NewLife.NovaDb/Client/EmbeddedDatabase.cs | EmbeddedDatabase 内部锁在 NET9+ 下改用 System.Threading.Lock |
| Benchmark/Benchmark.csproj | Benchmark 目标框架调整为 net10.0 |
Comment on lines
+22
to
+26
| #if NET9_0_OR_GREATER | ||
| private readonly System.Threading.Lock _lock = new(); | ||
| #else | ||
| private readonly Object _lock = new(); | ||
| #endif |
There was a problem hiding this comment.
这个文件里有多处重复的 #if NET9_0_OR_GREATER 锁字段声明(_lock/_delayLock/_deadLetterLock/_signalLock)。建议用文件级类型别名(例如 using LockType = ...)或封装一个统一的锁类型来减少条件编译块的重复,后续维护(新增锁、重命名等)不容易漏改。
Member
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.