Browse Source

Switched AvaloniaPropertyRegistry._properties from Dictionary to List

pull/2209/head
mstr2 8 years ago
parent
commit
5cbe89e9d6
  1. 2
      src/Avalonia.Base/AvaloniaProperty.cs
  2. 10
      src/Avalonia.Base/AvaloniaPropertyRegistry.cs
  3. 47
      src/Avalonia.Base/ValueStore.cs

2
src/Avalonia.Base/AvaloniaProperty.cs

@ -21,7 +21,7 @@ namespace Avalonia
/// </summary> /// </summary>
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;
private readonly Subject<AvaloniaPropertyChangedEventArgs> _initialized; private readonly Subject<AvaloniaPropertyChangedEventArgs> _initialized;
private readonly Subject<AvaloniaPropertyChangedEventArgs> _changed; private readonly Subject<AvaloniaPropertyChangedEventArgs> _changed;
private readonly PropertyMetadata _defaultMetadata; private readonly PropertyMetadata _defaultMetadata;

10
src/Avalonia.Base/AvaloniaPropertyRegistry.cs

@ -13,8 +13,8 @@ namespace Avalonia
/// </summary> /// </summary>
public class AvaloniaPropertyRegistry public class AvaloniaPropertyRegistry
{ {
private readonly Dictionary<int, AvaloniaProperty> _allProperties = private readonly IList<AvaloniaProperty> _properties =
new Dictionary<int, AvaloniaProperty>(); new List<AvaloniaProperty>();
private readonly Dictionary<Type, Dictionary<int, AvaloniaProperty>> _registered = private readonly Dictionary<Type, Dictionary<int, AvaloniaProperty>> _registered =
new Dictionary<Type, Dictionary<int, AvaloniaProperty>>(); new Dictionary<Type, Dictionary<int, AvaloniaProperty>>();
private readonly Dictionary<Type, Dictionary<int, AvaloniaProperty>> _attached = private readonly Dictionary<Type, Dictionary<int, AvaloniaProperty>> _attached =
@ -157,7 +157,7 @@ namespace Avalonia
/// <returns>The registered property or null if no matching property found.</returns> /// <returns>The registered property or null if no matching property found.</returns>
internal AvaloniaProperty FindRegistered(int id) internal AvaloniaProperty FindRegistered(int id)
{ {
return _allProperties.TryGetValue(id, out var value) ? value : null; return id < _properties.Count ? _properties[id] : null;
} }
/// <summary> /// <summary>
@ -215,7 +215,7 @@ namespace Avalonia
inner.Add(property.Id, property); inner.Add(property.Id, property);
} }
_allProperties[property.Id] = property; _properties.Add(property);
_registeredCache.Clear(); _registeredCache.Clear();
} }
@ -251,7 +251,7 @@ namespace Avalonia
inner.Add(property.Id, property); inner.Add(property.Id, property);
} }
_allProperties[property.Id] = property; _properties.Add(property);
_attachedCache.Clear(); _attachedCache.Clear();
} }
} }

47
src/Avalonia.Base/ValueStore.cs

@ -238,7 +238,7 @@ namespace Avalonia
private (int, bool) TryFindEntry(int propertyId) private (int, bool) TryFindEntry(int propertyId)
{ {
if (_entries.Length <= 16) if (_entries.Length <= 12)
{ {
// For small lists, we use an optimized linear search. Since the last item in the list // For small lists, we use an optimized linear search. Since the last item in the list
// is always int.MaxValue, we can skip a conditional branch in each iteration. // is always int.MaxValue, we can skip a conditional branch in each iteration.
@ -255,10 +255,6 @@ namespace Avalonia
if (_entries[8].PropertyId >= propertyId) return (8, _entries[8].PropertyId == propertyId); if (_entries[8].PropertyId >= propertyId) return (8, _entries[8].PropertyId == propertyId);
if (_entries[9].PropertyId >= propertyId) return (9, _entries[9].PropertyId == propertyId); if (_entries[9].PropertyId >= propertyId) return (9, _entries[9].PropertyId == propertyId);
if (_entries[10].PropertyId >= propertyId) return (10, _entries[10].PropertyId == propertyId); if (_entries[10].PropertyId >= propertyId) return (10, _entries[10].PropertyId == propertyId);
if (_entries[11].PropertyId >= propertyId) return (11, _entries[11].PropertyId == propertyId);
if (_entries[12].PropertyId >= propertyId) return (12, _entries[12].PropertyId == propertyId);
if (_entries[13].PropertyId >= propertyId) return (13, _entries[13].PropertyId == propertyId);
if (_entries[14].PropertyId >= propertyId) return (14, _entries[14].PropertyId == propertyId);
} }
else else
{ {
@ -266,36 +262,33 @@ namespace Avalonia
int high = _entries.Length; int high = _entries.Length;
int id; int id;
if (high > 0) while (high - low > 3)
{ {
while (high - low > 3) int pivot = (high + low) / 2;
{ id = _entries[pivot].PropertyId;
int pivot = (high + low) / 2;
id = _entries[pivot].PropertyId;
if (propertyId == id) if (propertyId == id)
return (pivot, true); return (pivot, true);
if (propertyId <= id) if (propertyId <= id)
high = pivot; high = pivot;
else else
low = pivot + 1; low = pivot + 1;
} }
do do
{ {
id = _entries[low].PropertyId; id = _entries[low].PropertyId;
if (id == propertyId) if (id == propertyId)
return (low, true); return (low, true);
if (id > propertyId) if (id > propertyId)
break; break;
++low; ++low;
}
while (low < high);
} }
while (low < high);
} }
return (0, false); return (0, false);

Loading…
Cancel
Save