Browse Source

Implement fast metadata path for properties that have no metadata overrides.

pull/3203/head
Dariusz Komosinski 7 years ago
parent
commit
268a209889
  1. 24
      src/Avalonia.Base/AvaloniaProperty.cs

24
src/Avalonia.Base/AvaloniaProperty.cs

@ -28,6 +28,8 @@ namespace Avalonia
private readonly Dictionary<Type, PropertyMetadata> _metadata; private readonly Dictionary<Type, PropertyMetadata> _metadata;
private readonly Dictionary<Type, PropertyMetadata> _metadataCache = new Dictionary<Type, PropertyMetadata>(); private readonly Dictionary<Type, PropertyMetadata> _metadataCache = new Dictionary<Type, PropertyMetadata>();
private bool _hasMetadataOverrides;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AvaloniaProperty"/> class. /// Initializes a new instance of the <see cref="AvaloniaProperty"/> class.
/// </summary> /// </summary>
@ -446,16 +448,28 @@ namespace Avalonia
/// ///
public PropertyMetadata GetMetadata(Type type) public PropertyMetadata GetMetadata(Type type)
{ {
Contract.Requires<ArgumentNullException>(type != null); if (!_hasMetadataOverrides)
{
return _defaultMetadata;
}
PropertyMetadata result; return GetMetadataWithOverrides(type);
Type currentType = 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; return result;
} }
Type currentType = type;
while (currentType != null) while (currentType != null)
{ {
if (_metadata.TryGetValue(currentType, out result)) if (_metadata.TryGetValue(currentType, out result))
@ -535,6 +549,8 @@ namespace Avalonia
metadata.Merge(baseMetadata, this); metadata.Merge(baseMetadata, this);
_metadata.Add(type, metadata); _metadata.Add(type, metadata);
_metadataCache.Clear(); _metadataCache.Clear();
_hasMetadataOverrides = true;
} }
[DebuggerHidden] [DebuggerHidden]

Loading…
Cancel
Save