Browse Source

Use inner dictionary in property registry.

This speeds up perspex property lookups.
pull/472/head
Steven Kirk 10 years ago
parent
commit
3532da2bb0
  1. 14
      src/Perspex.Base/PerspexProperty.cs
  2. 54
      src/Perspex.Base/PerspexPropertyRegistry.cs

14
src/Perspex.Base/PerspexProperty.cs

@ -22,7 +22,6 @@ namespace Perspex
public static readonly object UnsetValue = new Unset(); public static readonly object UnsetValue = new Unset();
private static int s_nextId = 1; private static int s_nextId = 1;
private readonly int _id;
private readonly Subject<PerspexPropertyChangedEventArgs> _initialized; private readonly Subject<PerspexPropertyChangedEventArgs> _initialized;
private readonly Subject<PerspexPropertyChangedEventArgs> _changed; private readonly Subject<PerspexPropertyChangedEventArgs> _changed;
private readonly PropertyMetadata _defaultMetadata; private readonly PropertyMetadata _defaultMetadata;
@ -61,7 +60,7 @@ namespace Perspex
PropertyType = valueType; PropertyType = valueType;
OwnerType = ownerType; OwnerType = ownerType;
Notifying = notifying; Notifying = notifying;
_id = s_nextId++; Id = s_nextId++;
_metadata.Add(ownerType, metadata); _metadata.Add(ownerType, metadata);
_defaultMetadata = metadata; _defaultMetadata = metadata;
@ -85,7 +84,7 @@ namespace Perspex
PropertyType = source.PropertyType; PropertyType = source.PropertyType;
OwnerType = ownerType; OwnerType = ownerType;
Notifying = source.Notifying; Notifying = source.Notifying;
_id = source._id; Id = source.Id;
_defaultMetadata = source._defaultMetadata; _defaultMetadata = source._defaultMetadata;
} }
@ -164,6 +163,11 @@ namespace Perspex
/// </remarks> /// </remarks>
public Action<IPerspexObject, bool> Notifying { get; } public Action<IPerspexObject, bool> Notifying { get; }
/// <summary>
/// Gets the integer ID that represents this property.
/// </summary>
internal int Id { get; }
/// <summary> /// <summary>
/// Provides access to a property's binding via the <see cref="PerspexObject"/> /// Provides access to a property's binding via the <see cref="PerspexObject"/>
/// indexer. /// indexer.
@ -392,13 +396,13 @@ namespace Perspex
/// <inheritdoc/> /// <inheritdoc/>
public bool Equals(PerspexProperty other) public bool Equals(PerspexProperty other)
{ {
return other != null && _id == other._id; return other != null && Id == other.Id;
} }
/// <inheritdoc/> /// <inheritdoc/>
public override int GetHashCode() public override int GetHashCode()
{ {
return _id; return Id;
} }
/// <summary> /// <summary>

54
src/Perspex.Base/PerspexPropertyRegistry.cs

@ -17,14 +17,14 @@ namespace Perspex
/// <summary> /// <summary>
/// The registered properties by type. /// The registered properties by type.
/// </summary> /// </summary>
private readonly Dictionary<Type, List<PerspexProperty>> _registered = private readonly Dictionary<Type, Dictionary<int, PerspexProperty>> _registered =
new Dictionary<Type, List<PerspexProperty>>(); new Dictionary<Type, Dictionary<int, PerspexProperty>>();
/// <summary> /// <summary>
/// The registered attached properties by owner type. /// The registered attached properties by owner type.
/// </summary> /// </summary>
private readonly Dictionary<Type, List<PerspexProperty>> _attached = private readonly Dictionary<Type, Dictionary<int, PerspexProperty>> _attached =
new Dictionary<Type, List<PerspexProperty>>(); new Dictionary<Type, Dictionary<int, PerspexProperty>>();
/// <summary> /// <summary>
/// Gets the <see cref="PerspexPropertyRegistry"/> instance /// Gets the <see cref="PerspexPropertyRegistry"/> instance
@ -39,11 +39,11 @@ namespace Perspex
/// <returns>A collection of <see cref="PerspexProperty"/> definitions.</returns> /// <returns>A collection of <see cref="PerspexProperty"/> definitions.</returns>
public IEnumerable<PerspexProperty> GetAttached(Type ownerType) public IEnumerable<PerspexProperty> GetAttached(Type ownerType)
{ {
List<PerspexProperty> list; Dictionary<int, PerspexProperty> inner;
if (_attached.TryGetValue(ownerType, out list)) if (_attached.TryGetValue(ownerType, out inner))
{ {
return list; return inner.Values;
} }
return Enumerable.Empty<PerspexProperty>(); return Enumerable.Empty<PerspexProperty>();
@ -65,13 +65,13 @@ namespace Perspex
// Ensure the type's static ctor has been run. // Ensure the type's static ctor has been run.
RuntimeHelpers.RunClassConstructor(type.TypeHandle); RuntimeHelpers.RunClassConstructor(type.TypeHandle);
List<PerspexProperty> list; Dictionary<int, PerspexProperty> inner;
if (_registered.TryGetValue(type, out list)) if (_registered.TryGetValue(type, out inner))
{ {
foreach (PerspexProperty p in list) foreach (var p in inner)
{ {
yield return p; yield return p.Value;
} }
} }
@ -105,15 +105,15 @@ namespace Perspex
{ {
while (type != null) while (type != null)
{ {
List<PerspexProperty> list; Dictionary<int, PerspexProperty> inner;
if (_registered.TryGetValue(type, out list)) if (_registered.TryGetValue(type, out inner))
{ {
var index = list.IndexOf(property); PerspexProperty result;
if (index != -1) if (inner.TryGetValue(property.Id, out result))
{ {
return list[index]; return result;
} }
} }
@ -250,30 +250,30 @@ namespace Perspex
Contract.Requires<ArgumentNullException>(type != null); Contract.Requires<ArgumentNullException>(type != null);
Contract.Requires<ArgumentNullException>(property != null); Contract.Requires<ArgumentNullException>(property != null);
List<PerspexProperty> list; Dictionary<int, PerspexProperty> inner;
if (!_registered.TryGetValue(type, out list)) if (!_registered.TryGetValue(type, out inner))
{ {
list = new List<PerspexProperty>(); inner = new Dictionary<int, PerspexProperty>();
_registered.Add(type, list); _registered.Add(type, inner);
} }
if (!list.Contains(property)) if (!inner.ContainsKey(property.Id))
{ {
list.Add(property); inner.Add(property.Id, property);
} }
if (property.IsAttached) if (property.IsAttached)
{ {
if (!_attached.TryGetValue(property.OwnerType, out list)) if (!_attached.TryGetValue(property.OwnerType, out inner))
{ {
list = new List<PerspexProperty>(); inner = new Dictionary<int, PerspexProperty>();
_attached.Add(property.OwnerType, list); _attached.Add(property.OwnerType, inner);
} }
if (!list.Contains(property)) if (!inner.ContainsKey(property.Id))
{ {
list.Add(property); inner.Add(property.Id, property);
} }
} }
} }

Loading…
Cancel
Save