From 13190154e75bf51497815b9d21782d78d4704ba2 Mon Sep 17 00:00:00 2001 From: martincostello Date: Tue, 16 Sep 2025 16:42:55 +0100 Subject: [PATCH] fix: change StringEnum back to record Change `StringEnum` back to a sealed record to see if it resolves the performance regression reported in #766. --- src/Octokit.Webhooks/Extensions/StringEnum.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Octokit.Webhooks/Extensions/StringEnum.cs b/src/Octokit.Webhooks/Extensions/StringEnum.cs index 397fd873..9e6a2b72 100644 --- a/src/Octokit.Webhooks/Extensions/StringEnum.cs +++ b/src/Octokit.Webhooks/Extensions/StringEnum.cs @@ -8,7 +8,7 @@ namespace Octokit.Webhooks.Extensions; using JetBrains.Annotations; [PublicAPI] -public readonly struct StringEnum : IEquatable> +public sealed record StringEnum : IEquatable> where TEnum : struct, Enum { private readonly TEnum? parsedValue; @@ -49,23 +49,6 @@ public StringEnum(TEnum parsedValue) public static implicit operator StringEnum(TEnum parsedValue) => new(parsedValue); - public static bool operator ==(StringEnum? left, StringEnum? right) - { - if (left is null && right is null) - { - return true; - } - - if (left is null || right is null) - { - return false; - } - - return left.Value.Equals(right.Value); - } - - public static bool operator !=(StringEnum? left, StringEnum? right) => !(left == right); - public bool TryParse([NotNullWhen(true)] out TEnum? value) { if (this.isValidEnum && this.parsedValue is not null) @@ -78,12 +61,12 @@ public bool TryParse([NotNullWhen(true)] out TEnum? value) return false; } - public override bool Equals(object? obj) => obj is StringEnum other && this.Equals(other); - - public bool Equals(StringEnum other) + public bool Equals(StringEnum? other) { var canParseThis = this.TryParse(out var thisValue); - var canParseOther = other.TryParse(out var otherValue); + + TEnum? otherValue = null; + var canParseOther = other != null && other.TryParse(out otherValue); return canParseThis switch { @@ -91,7 +74,7 @@ public bool Equals(StringEnum other) true when canParseOther => thisValue.Equals(otherValue), // If neither can be parsed, compare string values - false when !canParseOther => this.StringValue.Equals(other.StringValue, StringComparison.OrdinalIgnoreCase), + false when !canParseOther => this.StringValue.Equals(other?.StringValue, StringComparison.OrdinalIgnoreCase), // If one can parse and one cannot, they're not equal _ => false,