Skip to content

Commit 39ecaf4

Browse files
committed
Improve .NET 9.0+ locking performance (#30)
1 parent b1f07c2 commit 39ecaf4

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/Kook.Net.Commands/Map/CommandMapNode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ internal class CommandMapNode
99

1010
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
1111
private readonly string _name;
12+
#if NET9_0_OR_GREATER
13+
private readonly Lock _lockObj = new();
14+
#else
1215
private readonly object _lockObj = new();
16+
#endif
1317
private ImmutableArray<CommandInfo> _commands;
1418

1519
// ReSharper disable InconsistentlySynchronizedField

src/Kook.Net.Core/Utils/AsyncEvent.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ namespace Kook;
55
internal class AsyncEvent<T>
66
where T : class
77
{
8+
#if NET9_0_OR_GREATER
9+
private readonly Lock _subLock = new();
10+
#else
811
private readonly object _subLock = new();
12+
#endif
913
internal ImmutableArray<T> _subscriptions = [];
1014

1115
// ReSharper disable once InconsistentlySynchronizedField

src/Kook.Net.Rest/Net/Queue/RequestBucket.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ internal class RequestBucket
1111
{
1212
private const int MinimumSleepTimeMs = 750;
1313

14-
private readonly object _lock;
14+
#if NET9_0_OR_GREATER
15+
private readonly Lock _lock = new();
16+
#else
17+
private readonly object _lock = new();
18+
#endif
1519
private readonly RequestQueue _queue;
1620
private int _semaphore;
1721
private DateTimeOffset? _resetTick;
@@ -32,8 +36,6 @@ public RequestBucket(RequestQueue queue, IRequest request, BucketId id)
3236
_queue = queue;
3337
Id = id;
3438

35-
_lock = new object();
36-
3739
if (request.Options.IsClientBucket && request.Options.BucketId != null)
3840
WindowCount = ClientBucket.Get(request.Options.BucketId).WindowCount;
3941
else if (request.Options.IsGatewayBucket && request.Options.BucketId != null)

src/Kook.Net.WebSocket/Entities/Users/SocketGlobalUser.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ namespace Kook.WebSocket;
66
[DebuggerDisplay("{DebuggerDisplay,nq}")]
77
internal sealed class SocketGlobalUser : SocketUser
88
{
9+
#if NET9_0_OR_GREATER
10+
private readonly Lock _lockObj = new();
11+
#else
912
private readonly object _lockObj = new();
13+
#endif
1014
private ushort _references;
1115

1216
/// <inheritdoc />

0 commit comments

Comments
 (0)