Skip to content

Commit

Permalink
Use inner dictionary in property registry.
Browse files Browse the repository at this point in the history
This speeds up perspex property lookups.
  • Loading branch information
grokys committed Mar 7, 2016
1 parent c0bab67 commit 3532da2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
14 changes: 9 additions & 5 deletions src/Perspex.Base/PerspexProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class PerspexProperty : IEquatable<PerspexProperty>
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;
Expand Down Expand Up @@ -61,7 +60,7 @@ protected PerspexProperty(
PropertyType = valueType;
OwnerType = ownerType;
Notifying = notifying;
_id = s_nextId++;
Id = s_nextId++;

_metadata.Add(ownerType, metadata);
_defaultMetadata = metadata;
Expand All @@ -85,7 +84,7 @@ protected PerspexProperty(PerspexProperty source, Type ownerType)
PropertyType = source.PropertyType;
OwnerType = ownerType;
Notifying = source.Notifying;
_id = source._id;
Id = source.Id;
_defaultMetadata = source._defaultMetadata;
}

Expand Down Expand Up @@ -164,6 +163,11 @@ protected PerspexProperty(PerspexProperty source, Type ownerType)
/// </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.
Expand Down Expand Up @@ -392,13 +396,13 @@ public override bool Equals(object obj)
/// <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>
Expand Down
54 changes: 27 additions & 27 deletions src/Perspex.Base/PerspexPropertyRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class PerspexPropertyRegistry
/// <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
Expand All @@ -39,11 +39,11 @@ public class PerspexPropertyRegistry
/// <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>();
Expand All @@ -65,13 +65,13 @@ public IEnumerable<PerspexProperty> GetRegistered(Type type)
// 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;
}
}

Expand Down Expand Up @@ -105,15 +105,15 @@ public PerspexProperty FindRegistered(Type type, PerspexProperty property)
{
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;
}
}

Expand Down Expand Up @@ -250,30 +250,30 @@ public void Register(Type type, PerspexProperty property)
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);
}
}
}
Expand Down

0 comments on commit 3532da2

Please sign in to comment.