From f1bdace2cdd3beaf5bc7d8cf7aca840a74e92bfd Mon Sep 17 00:00:00 2001 From: lindexi Date: Mon, 18 Dec 2023 20:21:14 +0800 Subject: [PATCH 1/2] Minor Optimize Performance by Reducing Calls to `AppDomain.CurrentDomain.FriendlyName` I have optimized the performance by reducing the number of times `AppDomain.CurrentDomain.FriendlyName` property is accessed. Accessing the FriendlyName property is not cheap and results in some additional performance overhead. By assigning it to a local variable first, I have managed to reduce multiple property accesses, thereby slightly improving performance without altering the existing logic. ``` public sealed partial class AppDomain : MarshalByRefObject { public string FriendlyName { get { Assembly? assembly = Assembly.GetEntryAssembly(); return assembly != null ? assembly.GetName().Name! : "DefaultDomain"; } } } ``` --- .../src/Shared/MS/Win32/HwndWrapper.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs index c6672b0fede..cfe6cda6638 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs @@ -95,10 +95,11 @@ public HwndWrapper( // Register will fail if the string gets over 255 in length. // So limit each part to a reasonable amount. string appName; - if(null != AppDomain.CurrentDomain.FriendlyName && 128 <= AppDomain.CurrentDomain.FriendlyName.Length) - appName = AppDomain.CurrentDomain.FriendlyName.Substring(0, 128); + var currentDomainFriendlyName = AppDomain.CurrentDomain.FriendlyName; + if (null != currentDomainFriendlyName && 128 <= currentDomainFriendlyName.Length) + appName = currentDomainFriendlyName[..128]; else - appName = AppDomain.CurrentDomain.FriendlyName; + appName = currentDomainFriendlyName; string threadName; if(null != Thread.CurrentThread.Name && 64 <= Thread.CurrentThread.Name.Length) From 1a9db9bf2caff0bfdb3d3bdc9caf5a6ce2eaf710 Mon Sep 17 00:00:00 2001 From: lindexi Date: Thu, 19 Sep 2024 09:51:57 +0800 Subject: [PATCH 2/2] Using the special string type --- src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs index cfe6cda6638..d8f01a0d49c 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs @@ -95,7 +95,7 @@ public HwndWrapper( // Register will fail if the string gets over 255 in length. // So limit each part to a reasonable amount. string appName; - var currentDomainFriendlyName = AppDomain.CurrentDomain.FriendlyName; + string currentDomainFriendlyName = AppDomain.CurrentDomain.FriendlyName; if (null != currentDomainFriendlyName && 128 <= currentDomainFriendlyName.Length) appName = currentDomainFriendlyName[..128]; else