Repository navigation
Description
Current behavior 🐛
DependencyPropertyDetailsCollection stores each DependencyObject's set properties in a DependencyPropertyDetails?[] array. It buckets properties by property.UniqueId >> 4 (16 properties per bucket) and grows the array one full 16-slot bucket at a time, in src/Uno.UI/UI/Xaml/DependencyPropertyDetailsCollection.cs:
private const int BucketSize = 16;
...
var bucketIndex = property.UniqueId >> 4;
var bucketRemainder = property.UniqueId & 15;
...
if (offset == -1)
{
entryOffsets[bucketIndex] = offset = (short)entries.Length;
var newEntries = _pool.Rent(entries.Length + BucketSize); // always grows by 16 slots
...
}UniqueId comes from a single process-wide counter (DependencyProperty.cs, internal int UniqueId => _uniqueId;) shared by every DependencyProperty ever registered by the app, Uno.UI, and any libraries/themes loaded. Two properties on the same object almost never share a 16-wide slice of that global id space, so touching a single property on an object typically allocates a full 16-slot, 128-byte array to hold that one value - about 6% fill rate. Every additional property that lands in a different bucket costs another full 16-slot array.
Measured directly (reflecting into the private _entries field) on current master: setting one property on a plain DependencyObject late enough that other properties have already been registered (i.e. any realistic app after theme/control static constructors have run) allocates:
entries.Length=16 used=1 fill=6.2% bytes=128 (8 bytes/ref) for 1 value(s)
This is paid by every DependencyObject that ever sets a property - so before first frame (theme dictionaries, control templates, visual states) and again on every template materialization.
Note: this collection also used to over-allocate its companion short[] offsets array by 16x (_offsetsPool.Rent((bucketIndex * BucketSize) + 1) instead of Rent(bucketIndex + 1)) - that part is already fixed on master (perf: Stop over-renting the property offsets array), with regression coverage in Given_DependencyPropertyDetailsCollection.When_Property_Has_High_UniqueId_Then_Offsets_Are_Not_Over_Rented. This issue is only about the remaining DependencyPropertyDetails?[] entries array, which still grows in fixed 16-slot buckets keyed by the global id.
Expected behavior 🎯
The DependencyPropertyDetails?[] array should scale with the number of properties an object actually uses, not with how sparsely those properties are distributed across the process-wide UniqueId space. A single set property shouldn't need a 128-byte backing array.
How to reproduce it (as minimally and precisely as possible) 🔬
This is an internal allocation issue in the property-value storage, not something that needs a visual repro - it's reproducible with reflection against the private collection fields:
- Create any
DependencyObject-derived type and register enough properties on other types beforehand to push the globalDependencyPropertyid counter past 16 (in a real app, Uno.UI's own controls/themes already register ~2000+ properties, so this always happens before user code runs). - Set any single dependency property on a fresh instance.
- Reflect into the instance's private
_propertiesfield (DependencyObject._properties, typeDependencyPropertyDetailsCollection), then into its private_entriesfield. - Observe
_entries.Length == 16while only 1 slot is non-null.
Minimal MSTest-style snippet (drops into Uno.UI.UnitTests, next to the existing Given_DependencyPropertyDetailsCollection tests which already pad the global id past 16 via RegisterPaddedProperties):
PaddedObject sut = new();
sut.SetValue(_highIdProperty, 42); // _highIdProperty registered after 256 padding properties
var properties = typeof(DependencyObject)
.GetField("_properties", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(sut)!;
var entries = (Array)properties.GetType()
.GetField("_entries", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(properties)!;
var used = 0;
for (var i = 0; i < entries.Length; i++)
{
if (entries.GetValue(i) != null) used++;
}
// entries.Length == 16, used == 1 => 6.2% fill, 128 bytes for one valueWorkaround 🛠️
None needed from application code - this only affects internal memory overhead, not correctness.
Renderer 🎨
- Skia
Affected platforms 📱💻🖥️
- All platforms 🌍
Uno.Sdk version (and other relevant versions) 📦
master @ ffcff329eb3120c88cebf214629ca8a810f7880c, net10.0/net11.0.
IDE version 🧑💻
N/A (reproduced via unit test, not IDE-specific)
Anything else we need to know? 💬
Root cause: src/Uno.UI/UI/Xaml/DependencyPropertyDetailsCollection.cs
BucketSize = 16- bucket indexing and fixed-size growth
- keyed off the process-wide
DependencyProperty.UniqueId
Suggested fixes, in increasing order of effort:
- Shrink
BucketSizefrom 16 to 4 (trades a proportionally longer/more frequently resizedshort[]offsets array - already cheap since the over-rent fix above - for a 4x denserDependencyPropertyDetails[]). - Give each
DependencyPropertya per-owner-type dense ordinal alongside its globalUniqueId, and bucket on that instead, so an object's bucket space reflects only the properties its own type declares.
A related, much larger open PR (#18093, "Experiment with different approach for DP storage") replaces this whole bucketed scheme with a sorted dense array + binary search, which would also eliminate this waste as a side effect, but it hasn't seen activity since 2024 and reworks far more than this specific problem.
A smaller, standalone fix for just the bucket-size/fill-rate problem described here would be a good, self-contained starter task.
Activity
Qais-Dalahmeh commentedon Oct 2, 2026
I looked into implementing the first suggested fix here (shrinking
BucketSize), and wanted to share what I found before opening a PR, because the numbers don't support that fix once the full picture is measured.What I measured
BucketSizecontrols two costs that move in opposite directions:_entriesarray (DependencyPropertyDetails?[]):BucketSize * 8 bytesper bucket touched. ShrinkingBucketSizeshrinks this._entryOffsetsarray (short[], indexed byUniqueId >> shift): since a smallerBucketSizemeans a bigger bucket index for the sameUniqueId, shrinkingBucketSizegrows this array — and it's rented fromArrayPool<short>.Shared, which rounds up to the next power of two, so the growth isn't smooth.First, to check what
UniqueIdvalues are realistic: in an isolated test process (counter starting at -1), running the static constructors of 12 common controls (Button,TextBlock,TextBox,Grid,StackPanel,Border,Image,ListView,ScrollViewer,CheckBox,ComboBox,ContentPresenter) and their base types registers 352 properties, and running them for all 639DependencyObjecttypes in Uno.UI registers 2015. So this issue's "~2000+" estimate holds for Uno.UI alone, before any toolkit, theme or app-defined properties. Each object's_entryOffsetsis sized by the highestUniqueIdit touches, so control-specific properties registered later in that order are what drive its size.I then implemented the shrink exactly as suggested (parameterized via a single shift constant, no other changes) and measured
entries.bytes + offsets.bytestogether forBucketSize16 (current) / 8 / 4, atUniqueIdvalues of 500, 2000 and 8000, at property counts of 1, 2, 5 and 10:(all values in bytes; bold = smallest)
At realistic
UniqueIdmagnitude, shrinkingBucketSizemakes total memory worse in almost every case I measured — including the exact scenario this issue opens with (one property set on an object whoseUniqueIdis already in the thousands). It only starts to win once an object sets ~10+ properties, and even then the margin is small. (At lowerUniqueIds, roughly below 512, 8 ties or beats 16 — so objects that only touch early-registered core properties wouldn't regress, but they aren't where the cost is.)A related observation: at
UniqueId2000 with the currentBucketSizeof 16, a single set property costs 128 bytes of_entriesbut 256 bytes of_entryOffsets. So at realistic scale, the larger per-object cost today is actually the offsets array, not the entries array this issue focuses on.Why
Treating the entries cost as
8 * BucketSize * countand the offsets cost as roughly2 * UniqueId / BucketSize, the bucket size that minimizes their sum works out to:For this issue's own stated scenario (1 property,
UniqueId≈ 2000), that's ≈22 — larger than the current 16, not smaller. The write-up's assumption that the offsets array is "already cheap" holds at theUniqueIdmagnitudes a quick local test registers (low hundreds), but not at the magnitude a real app actually reaches before user code runs, which is exactly the scenario motivating this issue.Where that leaves things
The straightforward "shrink
BucketSize" fix doesn't hold up once the_entryOffsetscost is measured at realistic scale — it's not just "a smaller win than hoped," it's a net regression for the most common shape (few properties, highUniqueId). I don't think this is a good first PR as scoped. A fix that actually helps would need to decouple the offsets array's granularity from the entries array's granularity (so shrinking one doesn't grow the other), which is a meaningfully bigger change than this issue's first suggestion — possibly closer to the per-owner-type ordinal idea mentioned as the second option, or to #18093.Happy to share the small test harness I used to produce these numbers if it's useful for whoever picks this up next.
perf: Size DP storage to the properties set
perf: Size DP storage to the properties set
perf: Size DP storage to the properties set
perf: Size DP storage to the properties set
perf: Size DP storage to the properties set