Skip to content

Commit 65cc95c

Browse files
Improve .NET 9.0+ locking performance
1 parent fd6e3ad commit 65cc95c

File tree

14 files changed

+31
-16
lines changed

14 files changed

+31
-16
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Collections.Immutable;
5+
using System.Threading;
56

67
namespace Discord.Commands
78
{
@@ -11,7 +12,7 @@ internal class CommandMapNode
1112

1213
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
1314
private readonly string _name;
14-
private readonly object _lockObj = new object();
15+
private readonly Lock _lockObj = new();
1516
private ImmutableArray<CommandInfo> _commands;
1617

1718
public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0;

src/Discord.Net.Core/Discord.Net.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@
1919
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8">
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
22+
<PackageReference Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Include="Backport.System.Threading.Lock" Version="3.1.5">
23+
<PrivateAssets>all</PrivateAssets>
24+
<IncludeAssets>analyzers</IncludeAssets>
25+
</PackageReference>
2226
</ItemGroup>
2327
</Project>

src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Threading;
78
using System.Threading.Tasks;
89

910
using Model = Discord.API.Interaction;
@@ -32,7 +33,7 @@ public ulong CommandId
3233
/// </summary>
3334
internal new RestCommandBaseData Data { get; private set; }
3435

35-
private object _lock = new object();
36+
private readonly Lock _lock = new();
3637

3738
internal RestCommandBase(DiscordRestClient client, Model model)
3839
: base(client, model.Id)

src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Text;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using DataModel = Discord.API.MessageComponentInteractionData;
1011
using Model = Discord.API.Interaction;
@@ -24,7 +25,7 @@ public class RestMessageComponent : RestInteraction, IComponentInteraction, IDis
2425
/// <inheritdoc cref="IComponentInteraction.Message"/>
2526
public RestUserMessage Message { get; private set; }
2627

27-
private object _lock = new object();
28+
private readonly Lock _lock = new();
2829

2930
internal RestMessageComponent(BaseDiscordClient client, Model model)
3031
: base(client, model.Id)

src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Linq;
99
using System.Reflection;
10+
using System.Threading;
1011
using System.Threading.Tasks;
1112

1213
using DataModel = Discord.API.ModalInteractionData;
@@ -41,7 +42,7 @@ internal RestModal(DiscordRestClient client, ModelBase model)
4142
return entity;
4243
}
4344

44-
private object _lock = new object();
45+
private readonly Lock _lock = new();
4546

4647
/// <summary>
4748
/// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created

src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using DataModel = Discord.API.AutocompleteInteractionData;
89
using Model = Discord.API.Interaction;
@@ -19,7 +20,7 @@ public class RestAutocompleteInteraction : RestInteraction, IAutocompleteInterac
1920
/// </summary>
2021
public new RestAutocompleteInteractionData Data { get; }
2122

22-
private object _lock = new object();
23+
private readonly Lock _lock = new();
2324

2425
internal RestAutocompleteInteraction(DiscordRestClient client, Model model)
2526
: base(client, model.Id)

src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class RequestBucket
1717
{
1818
private const int MinimumSleepTimeMs = 750;
1919

20-
private readonly object _lock;
20+
private readonly Lock _lock;
2121
private readonly RequestQueue _queue;
2222
private int _semaphore;
2323
private DateTimeOffset? _resetTick;
@@ -32,7 +32,7 @@ public RequestBucket(RequestQueue queue, IRequest request, BucketId id)
3232
_queue = queue;
3333
Id = id;
3434

35-
_lock = new object();
35+
_lock = new();
3636

3737
if (request.Options.IsClientBucket)
3838
WindowCount = ClientBucket.Get(request.Options.BucketId).WindowCount;

src/Discord.Net.WebSocket/DiscordShardedClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class DiscordShardedClient : BaseSocketClient, IDiscordClient
2121
private ImmutableArray<StickerPack<SocketSticker>> _defaultStickers;
2222
private int _totalShards;
2323
private SemaphoreSlim[] _identifySemaphores;
24-
private object _semaphoreResetLock;
24+
private readonly Lock _semaphoreResetLock;
2525
private Task _semaphoreResetTask;
2626

2727
private bool _isDisposed;
@@ -80,7 +80,7 @@ private DiscordShardedClient(int[] ids, DiscordSocketConfig config, API.DiscordS
8080
if (ids != null && config.TotalShards == null)
8181
throw new ArgumentException($"Custom ids are not supported when {nameof(config.TotalShards)} is not specified.");
8282

83-
_semaphoreResetLock = new object();
83+
_semaphoreResetLock = new();
8484
_shardIdsToIndex = new Dictionary<int, int>();
8585
config.DisplayInitialLog = false;
8686
_baseConfig = config;

src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.IO;
88
using System.Linq;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011
using Model = Discord.API.Channel;
1112
using ThreadMember = Discord.API.ThreadMember;
@@ -110,8 +111,8 @@ public bool IsPrivateThread
110111

111112
private bool _usersDownloaded;
112113

113-
private readonly object _downloadLock = new object();
114-
private readonly object _ownerLock = new object();
114+
private readonly Lock _downloadLock = new();
115+
private readonly Lock _ownerLock = new();
115116

116117
private ulong _ownerId;
117118

src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using DataModel = Discord.API.MessageComponentInteractionData;
1011
using Model = Discord.API.Interaction;
@@ -24,7 +25,7 @@ public class SocketMessageComponent : SocketInteraction, IComponentInteraction,
2425
/// <inheritdoc cref="IComponentInteraction.Message"/>
2526
public SocketUserMessage Message { get; private set; }
2627

27-
private object _lock = new object();
28+
private readonly Lock _lock = new();
2829
public override bool HasResponded { get; internal set; } = false;
2930

3031
internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user)

0 commit comments

Comments
 (0)