From 83dfe0ebaa542da7c78d81c855c770efbed2c59a Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Mon, 28 Oct 2024 01:59:19 +0100 Subject: [PATCH 1/3] Replace ArrayList with List for perf --- .../MS/Internal/Helper.cs | 7 ++-- .../System/Windows/FrameworkElement.cs | 42 +++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs index 1c2731685ad..64e8a3ed469 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs @@ -65,10 +65,9 @@ public FindResourceHelper(object name) // Find a data template (or table template) resource - internal static object FindTemplateResourceFromAppOrSystem(DependencyObject target, ArrayList keys, int exactMatch, ref int bestMatch) + internal static object FindTemplateResourceFromAppOrSystem(DependencyObject target, List keys, int exactMatch, ref int bestMatch) { object resource = null; - int k; // Comment out below three lines code. // For now, we will always get the resource from Application level @@ -86,7 +85,7 @@ internal static object FindTemplateResourceFromAppOrSystem(DependencyObject targ if (app != null) { // If the element is rooted to a Window and App exists, defer to App. - for (k = 0; k < bestMatch; ++k) + for (int k = 0; k < bestMatch; k++) { object appResource = Application.Current.FindResourceInternal(keys[k]); if (appResource != null) @@ -105,7 +104,7 @@ internal static object FindTemplateResourceFromAppOrSystem(DependencyObject targ if (bestMatch >= exactMatch) { // Try the system resource collection. - for (k = 0; k < bestMatch; ++k) + for (int k = 0; k < bestMatch; k++) { object sysResource = SystemResources.FindResourceInternal(keys[k]); if (sysResource != null) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs index fd657bb3c9d..dea9b4df2c9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs @@ -1409,18 +1409,18 @@ internal static object FindTemplateResourceInternal(DependencyObject target, obj return null; } - Type type; - object dataType = ContentPresenter.DataTypeForItem(item, target, out type); - - ArrayList keys = new ArrayList(); + object dataType = ContentPresenter.DataTypeForItem(item, target, out Type type); // construct the list of acceptable keys, in priority order - int exactMatch = -1; // number of entries that count as an exact match - // add compound keys for the dataType and all its base types + List keys = new(); + + // number of entries that count as an exact match + int exactMatch = -1; + while (dataType != null) { - object key = null; + TemplateKey key = null; if (templateType == typeof(ItemContainerTemplate)) key = new ItemContainerTemplateKey(dataType); else if (templateType == typeof(DataTemplate)) @@ -1436,7 +1436,7 @@ internal static object FindTemplateResourceInternal(DependencyObject target, obj if (type != null) { type = type.BaseType; - if (type == typeof(Object)) // don't search for Object - perf + if (type == typeof(object)) // don't search for Object - perf type = null; } @@ -1461,14 +1461,14 @@ internal static object FindTemplateResourceInternal(DependencyObject target, obj } // Search the parent chain for a [Data|Table]Template in a ResourceDictionary. - private static object FindTemplateResourceInTree(DependencyObject target, ArrayList keys, int exactMatch, ref int bestMatch) + private static object FindTemplateResourceInTree(DependencyObject target, List keys, int exactMatch, ref int bestMatch) { Debug.Assert(target != null, "Don't call FindTemplateResource with a null target object"); ResourceDictionary table; object resource = null; - FrameworkObject fo = new FrameworkObject(target); + FrameworkObject fo = new(target); Debug.Assert(fo.IsValid, "Don't call FindTemplateResource with a target object that is neither a FrameworkElement nor a FrameworkContentElement"); while (fo.IsValid) @@ -1482,9 +1482,9 @@ private static object FindTemplateResourceInTree(DependencyObject target, ArrayL // Fetch the ResourceDictionary // for the given target element table = GetInstanceResourceDictionary(fo.FE, fo.FCE); - if( table != null ) + if (table != null) { - candidate = FindBestMatchInResourceDictionary( table, keys, exactMatch, ref bestMatch ); + candidate = FindBestMatchInResourceDictionary(table, keys, exactMatch, ref bestMatch); if (candidate != null) { resource = candidate; @@ -1501,9 +1501,9 @@ private static object FindTemplateResourceInTree(DependencyObject target, ArrayL // ------------------------------------------- table = GetStyleResourceDictionary(fo.FE, fo.FCE); - if( table != null ) + if (table != null) { - candidate = FindBestMatchInResourceDictionary( table, keys, exactMatch, ref bestMatch ); + candidate = FindBestMatchInResourceDictionary(table, keys, exactMatch, ref bestMatch); if (candidate != null) { resource = candidate; @@ -1520,9 +1520,9 @@ private static object FindTemplateResourceInTree(DependencyObject target, ArrayL // ------------------------------------------- table = GetThemeStyleResourceDictionary(fo.FE, fo.FCE); - if( table != null ) + if (table != null) { - candidate = FindBestMatchInResourceDictionary( table, keys, exactMatch, ref bestMatch ); + candidate = FindBestMatchInResourceDictionary(table, keys, exactMatch, ref bestMatch); if (candidate != null) { resource = candidate; @@ -1539,9 +1539,9 @@ private static object FindTemplateResourceInTree(DependencyObject target, ArrayL // ------------------------------------------- table = GetTemplateResourceDictionary(fo.FE, fo.FCE); - if( table != null ) + if (table != null) { - candidate = FindBestMatchInResourceDictionary( table, keys, exactMatch, ref bestMatch ); + candidate = FindBestMatchInResourceDictionary(table, keys, exactMatch, ref bestMatch); if (candidate != null) { resource = candidate; @@ -1580,16 +1580,14 @@ private static object FindTemplateResourceInTree(DependencyObject target, ArrayL // Given a ResourceDictionary and a set of keys, try to find the best // match in the resource dictionary. - private static object FindBestMatchInResourceDictionary( - ResourceDictionary table, ArrayList keys, int exactMatch, ref int bestMatch) + private static object FindBestMatchInResourceDictionary(ResourceDictionary table, List keys, int exactMatch, ref int bestMatch) { object resource = null; - int k; // Search target element's ResourceDictionary for the resource if (table != null) { - for (k = 0; k < bestMatch; ++k) + for (int k = 0; k < bestMatch; k++) { object candidate = table[keys[k]]; if (candidate != null) From 7d7a88b41c9e46ce2e0401466d154facda2e5e89 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Mon, 28 Oct 2024 02:02:44 +0100 Subject: [PATCH 2/3] Use some discards and remove unnecessary assigments --- .../System/Windows/FrameworkElement.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs index dea9b4df2c9..da8c4f4788b 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs @@ -3133,17 +3133,11 @@ internal bool InvalidateAutomationAncestorsCoreHelper(Stack br return continueInvalidation; } - internal static bool InvalidateAutomationIntermediateElements( - DependencyObject mergePoint, - DependencyObject modelTreeNode) + internal static bool InvalidateAutomationIntermediateElements(DependencyObject mergePoint, DependencyObject modelTreeNode) { - UIElement e = null; - ContentElement ce = null; - UIElement3D e3d = null; - while (modelTreeNode != null && modelTreeNode != mergePoint) { - if (!UIElementHelper.InvalidateAutomationPeer(modelTreeNode, out e, out ce, out e3d)) + if (!UIElementHelper.InvalidateAutomationPeer(modelTreeNode, out _, out _, out _)) { return false; } From 67b3c5f00b22c13da20af5afd8c53d4b30111bac Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Wed, 2 Apr 2025 12:47:01 +0200 Subject: [PATCH 3/3] Avoid unnecessary null-check --- .../System/Windows/FrameworkElement.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs index da8c4f4788b..2cdfb397a1f 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs @@ -1404,7 +1404,7 @@ internal static object FindResourceInTree( internal static object FindTemplateResourceInternal(DependencyObject target, object item, Type templateType) { // Data styling doesn't apply to UIElement (bug 1007133). - if (item == null || (item is UIElement)) + if (item is null or UIElement) { return null; } @@ -1420,14 +1420,10 @@ internal static object FindTemplateResourceInternal(DependencyObject target, obj while (dataType != null) { - TemplateKey key = null; if (templateType == typeof(ItemContainerTemplate)) - key = new ItemContainerTemplateKey(dataType); + keys.Add(new ItemContainerTemplateKey(dataType)); else if (templateType == typeof(DataTemplate)) - key = new DataTemplateKey(dataType); - - if (key != null) - keys.Add(key); + keys.Add(new DataTemplateKey(dataType)); // all keys added for the given item type itself count as an exact match if (exactMatch == -1)