From b9da6a15d73187ef661be9ae5dc114da5107c757 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Wed, 19 Nov 2025 21:57:15 -0600 Subject: [PATCH] Optimize IpcCommand source gen --- .../IpcCommandGenerator.cs | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Ryujinx.HLE.Generators/IpcCommandGenerator.cs b/src/Ryujinx.HLE.Generators/IpcCommandGenerator.cs index 47918c5cf..b201220fe 100644 --- a/src/Ryujinx.HLE.Generators/IpcCommandGenerator.cs +++ b/src/Ryujinx.HLE.Generators/IpcCommandGenerator.cs @@ -1,5 +1,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; +using System; using System.Linq; using System.Collections.Immutable; using System.Threading; @@ -9,20 +10,60 @@ namespace Ryujinx.HLE.Generators [Generator] public class IpcCommandGenerator : IIncrementalGenerator { - private sealed class CommandData + private sealed class CommandData : IEquatable { public required string Namespace { get; init; } public required string TypeName { get; init; } public required string MethodName { get; init; } public required ImmutableArray CommandIds { get; init; } + + public bool Equals(CommandData other) + { + return Namespace == other.Namespace && TypeName == other.TypeName && MethodName == other.MethodName && CommandIds.SequenceEqual(other.CommandIds); + } + + public override bool Equals(object obj) + => obj is CommandData other && Equals(other); + + public override int GetHashCode() + { + unchecked + { + var hashCode = (Namespace != null ? Namespace.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (TypeName != null ? TypeName.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (MethodName != null ? MethodName.GetHashCode() : 0); + return hashCode; + } + } } - private sealed class ServiceData + private sealed class ServiceData : IEquatable { public required string Namespace { get; init; } public required string TypeName { get; init; } public required ImmutableArray CmifCommands { get; init; } public required ImmutableArray TipcCommands { get; init; } + + + public bool Equals(ServiceData other) + { + return Namespace == other.Namespace && TypeName == other.TypeName && CmifCommands.SequenceEqual(other.CmifCommands) && TipcCommands.SequenceEqual(other.TipcCommands); + } + + public override bool Equals(object obj) + { + return obj is ServiceData other && Equals(other); + } + + public override int GetHashCode() + { + unchecked + { + var hashCode = (Namespace != null ? Namespace.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (TypeName != null ? TypeName.GetHashCode() : 0); + return hashCode; + } + } } public void Initialize(IncrementalGeneratorInitializationContext context) @@ -82,10 +123,17 @@ namespace Ryujinx.HLE.Generators generator.EnterScope($"namespace {data.Namespace}"); generator.EnterScope($"partial class {data.TypeName}"); - - GenerateCommandMethod("Cmif", data.CmifCommands); - GenerateCommandMethod("Tipc", data.TipcCommands); - + + if (!data.CmifCommands.IsEmpty) + { + GenerateCommandMethod("Cmif", data.CmifCommands); + } + + if (!data.TipcCommands.IsEmpty) + { + GenerateCommandMethod("Tipc", data.TipcCommands); + } + generator.LeaveScope(); generator.LeaveScope();