Skip to content

Commit f19253d

Browse files
authored
Minor Performance Optimization by Reducing Calls to AppDomain.CurrentDomain.FriendlyName (#8567)
* 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"; } } } ``` * Using the special string type
1 parent 8f308f4 commit f19253d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ public HwndWrapper(
9595
// Register will fail if the string gets over 255 in length.
9696
// So limit each part to a reasonable amount.
9797
string appName;
98-
if(null != AppDomain.CurrentDomain.FriendlyName && 128 <= AppDomain.CurrentDomain.FriendlyName.Length)
99-
appName = AppDomain.CurrentDomain.FriendlyName.Substring(0, 128);
98+
string currentDomainFriendlyName = AppDomain.CurrentDomain.FriendlyName;
99+
if (null != currentDomainFriendlyName && 128 <= currentDomainFriendlyName.Length)
100+
appName = currentDomainFriendlyName[..128];
100101
else
101-
appName = AppDomain.CurrentDomain.FriendlyName;
102+
appName = currentDomainFriendlyName;
102103

103104
string threadName;
104105
if(null != Thread.CurrentThread.Name && 64 <= Thread.CurrentThread.Name.Length)

0 commit comments

Comments
 (0)