From 268a209889725861d2c84ae974c8439be7df5f2f Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sun, 3 Nov 2019 16:23:31 +0100 Subject: [PATCH] Implement fast metadata path for properties that have no metadata overrides. --- src/Avalonia.Base/AvaloniaProperty.cs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Base/AvaloniaProperty.cs b/src/Avalonia.Base/AvaloniaProperty.cs index 56ad241187..d4e416d229 100644 --- a/src/Avalonia.Base/AvaloniaProperty.cs +++ b/src/Avalonia.Base/AvaloniaProperty.cs @@ -28,6 +28,8 @@ namespace Avalonia private readonly Dictionary _metadata; private readonly Dictionary _metadataCache = new Dictionary(); + private bool _hasMetadataOverrides; + /// /// Initializes a new instance of the class. /// @@ -446,16 +448,28 @@ namespace Avalonia /// public PropertyMetadata GetMetadata(Type type) { - Contract.Requires(type != null); + if (!_hasMetadataOverrides) + { + return _defaultMetadata; + } - PropertyMetadata result; - Type currentType = type; + return GetMetadataWithOverrides(type); + } - if (_metadataCache.TryGetValue(type, out result)) + private PropertyMetadata GetMetadataWithOverrides(Type type) + { + if (type is null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (_metadataCache.TryGetValue(type, out PropertyMetadata result)) { return result; } + Type currentType = type; + while (currentType != null) { if (_metadata.TryGetValue(currentType, out result)) @@ -535,6 +549,8 @@ namespace Avalonia metadata.Merge(baseMetadata, this); _metadata.Add(type, metadata); _metadataCache.Clear(); + + _hasMetadataOverrides = true; } [DebuggerHidden]