From 9193d7a378746c31f9c2354d1774cfd8663334d0 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 23 Jun 2021 17:21:08 -0400 Subject: [PATCH] Remove delegate allocations from FindAmbientValues These were calling an ArrayHelper.ForAll, passing in a method group as a delegate, causing each call to allocate the delegate. The CheckAmbient helper being called was only used here, so rather than caching a delegate, I just inlined the code. The other ArrayHelper methods were each only used in one place, also, so I just inlined those, too, and deleted the helper class. There is one potentially visible change here. If an invalid property was passed in to the public IAmbientProvider.Get*AmbientValues methods via the "properties" parameter, today it's throwing an ArgumentException using an internal name of "xamlProperty", and this change makes it so that the exception contains the actual parameter name "properties". --- .../Xaml/Context/ObjectWriterContext.cs | 37 ++++++++---------- .../System/Xaml/Context/XamlContext.cs | 8 +++- .../System/Xaml/MS/Impl/ArrayHelper.cs | 39 ------------------- .../System.Xaml/System/Xaml/XamlTypeName.cs | 2 + 4 files changed, 24 insertions(+), 62 deletions(-) delete mode 100644 src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/ArrayHelper.cs diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs index 6057278ab35..21af3d22cf9 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs @@ -205,22 +205,6 @@ internal IEnumerable ServiceProvider_GetAllAmbientValues(I return valueList; } - private static void CheckAmbient(XamlMember xamlMember) - { - if (!xamlMember.IsAmbient) - { - throw new ArgumentException(SR.Get(SRID.NotAmbientProperty, xamlMember.DeclaringType.Name, xamlMember.Name), nameof(xamlMember)); - } - } - - private static void CheckAmbient(XamlType xamlType) - { - if (!xamlType.IsAmbient) - { - throw new ArgumentException(SR.Get(SRID.NotAmbientType, xamlType.Name), nameof(xamlType)); - } - } - internal XamlObjectWriterSettings ServiceProvider_GetSettings() { if (_settings == null) @@ -326,8 +310,15 @@ private List FindAmbientValues(IEnumerable ceili XamlMember[] properties, bool stopAfterFirst) { - ArrayHelper.ForAll(properties, CheckAmbient); - List ceilingTypes = ArrayHelper.ToList(ceilingTypesEnumerable); + foreach (XamlMember xamlMember in properties) + { + if (!xamlMember.IsAmbient) + { + throw new ArgumentException(SR.Get(SRID.NotAmbientProperty, xamlMember.DeclaringType.Name, xamlMember.Name), nameof(properties)); + } + } + + List ceilingTypes = ceilingTypesEnumerable != null ? new List(ceilingTypesEnumerable) : null; List retList = new List(); @@ -436,13 +427,18 @@ private List FindAmbientValues(IEnumerable ceili private List FindAmbientValues(XamlType[] types, bool stopAfterFirst) { - ArrayHelper.ForAll(types, CheckAmbient); + foreach (XamlType xamlType in types) + { + if (!xamlType.IsAmbient) + { + throw new ArgumentException(SR.Get(SRID.NotAmbientType, xamlType.Name), nameof(types)); + } + } List retList = new List(); // Start the search for ambient properties with the parent frame. ObjectWriterFrame frame = _stack.PreviousFrame; - ObjectWriterFrame lowerFrame = _stack.CurrentFrame; while (frame.Depth >= 1) { @@ -463,7 +459,6 @@ private List FindAmbientValues(XamlType[] types, bool stopAfterFirst) } } - lowerFrame = frame; frame = (ObjectWriterFrame)frame.Previous; Debug.Assert(frame != null); } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlContext.cs index 431fa56ef5b..1f62a6cdfae 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlContext.cs @@ -268,8 +268,12 @@ internal XamlType GetXamlType(XamlTypeName typeName, bool returnUnknownTypesOnFa XamlType[] typeArgs = null; if (typeName.HasTypeArgs) { - typeArgs = ArrayHelper.ConvertArrayType( - typeName.TypeArguments, GetXamlTypeOrUnknown); + List typeNames = typeName.TypeArgumentsList; + typeArgs = new XamlType[typeNames.Count]; + for (int i = 0; i < typeArgs.Length; i++) + { + typeArgs[i] = GetXamlTypeOrUnknown(typeNames[i]); + } } xamlType = new XamlType(typeName.Namespace, typeName.Name, typeArgs, SchemaContext); } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/ArrayHelper.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/ArrayHelper.cs deleted file mode 100644 index 7ca0c95091e..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/MS/Impl/ArrayHelper.cs +++ /dev/null @@ -1,39 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; - -namespace System.Xaml -{ - internal static class ArrayHelper - { - internal static S[] ConvertArrayType(ICollection src, Func f) - { - if (src == null) - { - return null; - } - int len = src.Count, n = 0; - S[] dest = new S[len]; - foreach (R r in src) - { - dest[n++] = f(r); - } - return dest; - } - - internal static void ForAll(R[] src, Action f) - { - foreach (R r in src) - f(r); - } - - internal static List ToList(IEnumerable src) - { - return (src != null) - ? new List(src) - : null; - } - } -} diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs index 3aa8be55c17..2081c71bb50 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlTypeName.cs @@ -66,6 +66,8 @@ public IList TypeArguments } } + internal List TypeArgumentsList => _typeArguments; + public override string ToString() { return ToString(null);