Skip to content

DependencyPropertyDetailsCollection wastes most of its DependencyPropertyDetails[] bucket per stored property #24754

Closed

Description

@MartinZikmund

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:

  1. Create any DependencyObject-derived type and register enough properties on other types beforehand to push the global DependencyProperty id 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).
  2. Set any single dependency property on a fresh instance.
  3. Reflect into the instance's private _properties field (DependencyObject._properties, type DependencyPropertyDetailsCollection), then into its private _entries field.
  4. Observe _entries.Length == 16 while 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 value

Workaround 🛠️

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

Suggested fixes, in increasing order of effort:

  • Shrink BucketSize from 16 to 4 (trades a proportionally longer/more frequently resized short[] offsets array - already cheap since the over-rent fix above - for a 4x denser DependencyPropertyDetails[]).
  • Give each DependencyProperty a per-owner-type dense ordinal alongside its global UniqueId, 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

  1. added
    kind/bug
    good first issue
    triage/untriaged
    project/core-tools 🛠️
    platform/all
    difficulty/starter 🚀
    area/performance 📈
    on Sep 25, 2026
  2. Qais-Dalahmeh commented on Oct 2, 2026

    @Qais-Dalahmeh

    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

    BucketSize controls two costs that move in opposite directions:

    • _entries array (DependencyPropertyDetails?[]): BucketSize * 8 bytes per bucket touched. Shrinking BucketSize shrinks this.
    • _entryOffsets array (short[], indexed by UniqueId >> shift): since a smaller BucketSize means a bigger bucket index for the same UniqueId, shrinking BucketSize grows this array — and it's rented from ArrayPool<short>.Shared, which rounds up to the next power of two, so the growth isn't smooth.

    First, to check what UniqueId values 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 639 DependencyObject types 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 _entryOffsets is sized by the highest UniqueId it 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.bytes together for BucketSize 16 (current) / 8 / 4, at UniqueId values of 500, 2000 and 8000, at property counts of 1, 2, 5 and 10:

    UniqueId count 16 (current) 8 4
    2000 1 384 576 1056
    2000 2 512 640 1088
    2000 5 1152 1344 2208
    2000 10 1792 1664 2368
    8000 1 1152 2112 4128
    8000 2 1280 2176 4160
    8000 5 1664 2368 4256
    8000 10 2304 2688 4416

    (all values in bytes; bold = smallest)

    At realistic UniqueId magnitude, shrinking BucketSize makes total memory worse in almost every case I measured — including the exact scenario this issue opens with (one property set on an object whose UniqueId is already in the thousands). It only starts to win once an object sets ~10+ properties, and even then the margin is small. (At lower UniqueIds, 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 UniqueId 2000 with the current BucketSize of 16, a single set property costs 128 bytes of _entries but 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 * count and the offsets cost as roughly 2 * UniqueId / BucketSize, the bucket size that minimizes their sum works out to:

    BucketSize_optimal ≈ 0.5 * sqrt(UniqueId / count)
    

    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 the UniqueId magnitudes 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 _entryOffsets cost 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, high UniqueId). 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.

  3. self-assigned this
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/performance 📈difficulty/starter 🚀good first issuekind/bugplatform/allproject/core-tools 🛠️triage/untriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions