Browse Source

Switched AvaloniaPropertyRegistry._properties from Dictionary to List

mstr2 6 years ago
parent
commit
5cbe89e9d6

+ 1 - 1
src/Avalonia.Base/AvaloniaProperty.cs

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

+ 5 - 5
src/Avalonia.Base/AvaloniaPropertyRegistry.cs

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

+ 20 - 27
src/Avalonia.Base/ValueStore.cs

@@ -238,7 +238,7 @@ namespace Avalonia
 
         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
                 // 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[9].PropertyId >= propertyId) return (9, _entries[9].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
             {
@@ -266,36 +262,33 @@ namespace Avalonia
                 int high = _entries.Length;
                 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)
-                            return (pivot, true);
+                    if (propertyId == id)
+                        return (pivot, true);
 
-                        if (propertyId <= id)
-                            high = pivot;
-                        else
-                            low = pivot + 1;
-                    }
+                    if (propertyId <= id)
+                        high = pivot;
+                    else
+                        low = pivot + 1;
+                }
 
-                    do
-                    {
-                        id = _entries[low].PropertyId;
+                do
+                {
+                    id = _entries[low].PropertyId;
 
-                        if (id == propertyId)
-                            return (low, true);
+                    if (id == propertyId)
+                        return (low, true);
 
-                        if (id > propertyId)
-                            break;
+                    if (id > propertyId)
+                        break;
 
-                        ++low;
-                    }
-                    while (low < high);
+                    ++low;
                 }
+                while (low < high);
             }
 
             return (0, false);