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

54
src/Perspex.Base/PerspexPropertyRegistry.cs

@ -17,14 +17,14 @@ namespace Perspex
/// <summary>
/// The registered properties by type.
/// </summary>
private readonly Dictionary<Type, List<PerspexProperty>> _registered =
new Dictionary<Type, List<PerspexProperty>>();
private readonly Dictionary<Type, Dictionary<int, PerspexProperty>> _registered =
new Dictionary<Type, Dictionary<int, PerspexProperty>>();
/// <summary>
/// The registered attached properties by owner type.
/// </summary>
private readonly Dictionary<Type, List<PerspexProperty>> _attached =
new Dictionary<Type, List<PerspexProperty>>();
private readonly Dictionary<Type, Dictionary<int, PerspexProperty>> _attached =
new Dictionary<Type, Dictionary<int, PerspexProperty>>();
/// <summary>
/// Gets the <see cref="PerspexPropertyRegistry"/> instance
@ -39,11 +39,11 @@ namespace Perspex
/// <returns>A collection of <see cref="PerspexProperty"/> definitions.</returns>
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>();
@ -65,13 +65,13 @@ namespace Perspex
// Ensure the type's static ctor has been run.
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)
{
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>(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>();
_registered.Add(type, list);
inner = new Dictionary<int, PerspexProperty>();
_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 (!_attached.TryGetValue(property.OwnerType, out list))
if (!_attached.TryGetValue(property.OwnerType, out inner))
{
list = new List<PerspexProperty>();
_attached.Add(property.OwnerType, list);
inner = new Dictionary<int, PerspexProperty>();
_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