From a094ca93bc28322daea51c2cc4dfca18c742f2aa Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Sat, 26 Sep 2026 21:29:07 +0200 Subject: [PATCH 1/5] Optimize GetRoles not to allocate and improve PartialGuildUser comparison --- NetCord/GuildUserRoles.cs | 88 +++++++++++++++++++++++++++ NetCord/PartialGuildUserExtensions.cs | 16 +---- NetCord/Rest/RestGuild.cs | 33 ++++++---- 3 files changed, 112 insertions(+), 25 deletions(-) create mode 100644 NetCord/GuildUserRoles.cs diff --git a/NetCord/GuildUserRoles.cs b/NetCord/GuildUserRoles.cs new file mode 100644 index 000000000..27b44ec75 --- /dev/null +++ b/NetCord/GuildUserRoles.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace NetCord; + +public readonly struct GuildUserRoles : IEnumerable +{ + private readonly IReadOnlyList _roleIds; + private readonly IReadOnlyDictionary _roles; + + internal GuildUserRoles(IReadOnlyList roleIds, IReadOnlyDictionary roles) + { + _roleIds = roleIds; + _roles = roles; + } + + public Enumerator GetEnumerator() => new(_roleIds, _roles); + + IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_roleIds, _roles); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + public struct Enumerator : IEnumerator + { + private readonly IReadOnlyList _roleIds; + private readonly IReadOnlyDictionary _roles; + private int _position; + + internal Enumerator(IReadOnlyList roleIds, IReadOnlyDictionary roles) + { + _roleIds = roleIds; + _roles = roles; + } + + [AllowNull] + public Role Current + { + readonly get + { + if (field is not { } current) + ThrowInvalidOperation(); + + return current; + + [DoesNotReturn] + [StackTraceHidden] + static void ThrowInvalidOperation() + { + throw new InvalidOperationException("Enumerator is positioned before the first element of the collection or after the last element."); + } + } + private set; + } + + readonly object IEnumerator.Current => Current; + + public bool MoveNext() + { + int count = _roleIds.Count; + + while (_position < count) + { + var roleId = _roleIds[_position++]; + + if (_roles.TryGetValue(roleId, out var role)) + { + Current = role; + return true; + } + } + + Current = null; + return false; + } + + void IEnumerator.Reset() + { + _position = 0; + Current = null; + } + + readonly void IDisposable.Dispose() + { + } + } +} + diff --git a/NetCord/PartialGuildUserExtensions.cs b/NetCord/PartialGuildUserExtensions.cs index 3b76c70bd..1daa4adc8 100644 --- a/NetCord/PartialGuildUserExtensions.cs +++ b/NetCord/PartialGuildUserExtensions.cs @@ -9,26 +9,16 @@ namespace NetCord; public static class PartialGuildUserExtensions { /// - /// Returns an object belonging to the by acquiring it from the specificied . + /// Returns the roles belonging to the by acquiring it from the specificied . /// /// /// IDs of roles that do not exist in the will be ignored. /// /// The to acquire roles for. /// The to acquire the roles from. - public static IEnumerable GetRoles(this PartialGuildUser user, RestGuild guild) + public static GuildUserRoles GetRoles(this PartialGuildUser user, RestGuild guild) { - var roles = guild.Roles; - - var roleIds = user.RoleIds; - - int count = roleIds.Count; - - for (int i = 0; i < count; i++) - { - if (roles.TryGetValue(roleIds[i], out var role)) - yield return role; - } + return new(user.RoleIds, guild.Roles); } /// diff --git a/NetCord/Rest/RestGuild.cs b/NetCord/Rest/RestGuild.cs index 985e2bcbc..cb4e3de0a 100644 --- a/NetCord/Rest/RestGuild.cs +++ b/NetCord/Rest/RestGuild.cs @@ -43,28 +43,37 @@ public int Compare(PartialGuildUser? x, PartialGuildUser? y) if (y is null) return 1; - var (xId, yId) = (x.Id, y.Id); - - if (xId == yId) - return 0; - var ownerId = OwnerId; - if (xId == ownerId) - return 1; + if (x.Id == ownerId) + return y.Id == ownerId ? 0 : 1; - if (yId == ownerId) + if (y.Id == ownerId) return -1; - if (x.RoleIds.Count is 0) - return -y.RoleIds.Count; + using var xRoleEnumerator = x.GetRoles(this).GetEnumerator(); + + if (!xRoleEnumerator.MoveNext()) + { + using var yRoleEnumerator = y.GetRoles(this).GetEnumerator(); + + return yRoleEnumerator.MoveNext() ? -1 : 0; + } + + var xPosition = xRoleEnumerator.Current.Position; - var xHighestPosition = x.GetRoles(this).Max(r => r.Position); + while (xRoleEnumerator.MoveNext()) + { + var currentPosition = xRoleEnumerator.Current.Position; + + if (currentPosition > xPosition) + xPosition = currentPosition; + } int result = 1; foreach (var role in y.GetRoles(this)) { - var comparisonResult = xHighestPosition.CompareTo(role.Position); + var comparisonResult = xPosition.CompareTo(role.Position); if (comparisonResult < 0) return -1; From 0ae036f8a87267158232b452358ed1e3ccbe9b63 Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Sat, 26 Sep 2026 21:57:54 +0200 Subject: [PATCH 2/5] Refactor --- NetCord/Rest/RestGuild.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NetCord/Rest/RestGuild.cs b/NetCord/Rest/RestGuild.cs index cb4e3de0a..901394810 100644 --- a/NetCord/Rest/RestGuild.cs +++ b/NetCord/Rest/RestGuild.cs @@ -44,6 +44,7 @@ public int Compare(PartialGuildUser? x, PartialGuildUser? y) return 1; var ownerId = OwnerId; + if (x.Id == ownerId) return y.Id == ownerId ? 0 : 1; @@ -73,7 +74,7 @@ public int Compare(PartialGuildUser? x, PartialGuildUser? y) foreach (var role in y.GetRoles(this)) { - var comparisonResult = xPosition.CompareTo(role.Position); + int comparisonResult = xPosition.CompareTo(role.Position); if (comparisonResult < 0) return -1; From 373f85b679e4261e4df60dd0931b7c92b5ff4e6a Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Sat, 26 Sep 2026 22:01:43 +0200 Subject: [PATCH 3/5] Improve xml doc --- NetCord/PartialGuildUserExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetCord/PartialGuildUserExtensions.cs b/NetCord/PartialGuildUserExtensions.cs index 1daa4adc8..7e052cf10 100644 --- a/NetCord/PartialGuildUserExtensions.cs +++ b/NetCord/PartialGuildUserExtensions.cs @@ -9,7 +9,7 @@ namespace NetCord; public static class PartialGuildUserExtensions { /// - /// Returns the roles belonging to the by acquiring it from the specificied . + /// Returns roles of the by acquiring them from the specificied . /// /// /// IDs of roles that do not exist in the will be ignored. From 1562de3378b9cde82f265fda4268231a49091588 Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Sat, 26 Sep 2026 22:04:52 +0200 Subject: [PATCH 4/5] Remove superfluous new line --- NetCord/GuildUserRoles.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/NetCord/GuildUserRoles.cs b/NetCord/GuildUserRoles.cs index 27b44ec75..9012c45fa 100644 --- a/NetCord/GuildUserRoles.cs +++ b/NetCord/GuildUserRoles.cs @@ -85,4 +85,3 @@ readonly void IDisposable.Dispose() } } } - From 289802844d8d4d82e2e194e7a7db0506467acb5b Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Sat, 26 Sep 2026 22:06:36 +0200 Subject: [PATCH 5/5] Simplify IEnumerable.GetEnumerator --- NetCord/GuildUserRoles.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetCord/GuildUserRoles.cs b/NetCord/GuildUserRoles.cs index 9012c45fa..847f50eed 100644 --- a/NetCord/GuildUserRoles.cs +++ b/NetCord/GuildUserRoles.cs @@ -17,7 +17,7 @@ internal GuildUserRoles(IReadOnlyList roleIds, IReadOnlyDictionary new(_roleIds, _roles); - IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_roleIds, _roles); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();