From 2f064064ca4040e299462325714281844a4b33af Mon Sep 17 00:00:00 2001 From: GreemDev Date: Sun, 23 Mar 2025 04:14:31 -0500 Subject: [PATCH] HLE: Slightly speed up IpcService creation This is not a crazy speedup, we're talking fractions of a millisecond here (I had to measure in ticks; 10000000 ticks per second) - TIPC commands are opted-into for registration since they are only used for IUserInterface, but were still attempted to be initialized for every service which spent needless time. - Directly use GetType() instead of accessing all exported types, and Where()ing for equivalency to GetType(). This causes the logic for registration to be a lot faster. --- src/Ryujinx.HLE/HOS/Services/IpcService.cs | 43 ++++++++++++++----- .../HOS/Services/Sm/IUserInterface.cs | 2 +- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/Ryujinx.HLE/HOS/Services/IpcService.cs b/src/Ryujinx.HLE/HOS/Services/IpcService.cs index 1b95b6712..7b1739e8e 100644 --- a/src/Ryujinx.HLE/HOS/Services/IpcService.cs +++ b/src/Ryujinx.HLE/HOS/Services/IpcService.cs @@ -1,8 +1,10 @@ +using Gommon; using Ryujinx.Common.Logging; using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.HOS.Ipc; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -21,22 +23,43 @@ namespace Ryujinx.HLE.HOS.Services private int _selfId; private bool _isDomain; - public IpcService(ServerBase server = null) + public IpcService(ServerBase server = null, bool registerTipc = false) { - CmifCommands = GetType().Assembly.GetTypes() - .Where(type => type == GetType()) - .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)) + Stopwatch sw = Stopwatch.StartNew(); + + CmifCommands = GetType() + .GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public) .SelectMany(methodInfo => methodInfo.GetCustomAttributes() .Select(command => (command.Id, methodInfo))) .ToDictionary(command => command.Id, command => command.methodInfo); + + sw.Stop(); + + Logger.Notice.Print( + LogClass.Emulation, + $"{CmifCommands.Count} Cmif commands loaded in {sw.ElapsedTicks} ticks ({Stopwatch.Frequency} tps).", + GetType().AsPrettyString() + ); - TipcCommands = GetType().Assembly.GetTypes() - .Where(type => type == GetType()) - .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)) - .SelectMany(methodInfo => methodInfo.GetCustomAttributes() - .Select(command => (command.Id, methodInfo))) - .ToDictionary(command => command.Id, command => command.methodInfo); + if (registerTipc) + { + sw.Start(); + TipcCommands = GetType() + .GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public) + .SelectMany(methodInfo => methodInfo.GetCustomAttributes() + .Select(command => (command.Id, methodInfo))) + .ToDictionary(command => command.Id, command => command.methodInfo); + + sw.Stop(); + + Logger.Notice.Print( + LogClass.Emulation, + $"{TipcCommands.Count} Tipc commands loaded in {sw.ElapsedTicks} ticks ({Stopwatch.Frequency} tps).", + GetType().AsPrettyString() + ); + } + Server = server; _parent = this; diff --git a/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs b/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs index 6d03d8d05..af511af29 100644 --- a/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs +++ b/src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs @@ -21,7 +21,7 @@ namespace Ryujinx.HLE.HOS.Services.Sm private bool _isInitialized; - public IUserInterface(KernelContext context, SmRegistry registry) + public IUserInterface(KernelContext context, SmRegistry registry) : base(registerTipc: true) { _commonServer = new ServerBase(context, "CommonServer"); _registry = registry;