Skip to content

Performance Rule #1

Roland Pheasant edited this page Oct 20, 2020 · 1 revision

When loading data in dynamic data, always load initial items in one hit.

Suppose we have:

var myCache = new SourceCache<T, int>(t => t.Id);
var myItems = await myServiceLoad();

Under no circumstances do this:

foreach (var item in myItems)
    myCache.AddOrUpdate(item)

instead, do this:

myCache.AddOrUpdate(myItems)

This is because dynamic data uses change set notifications to process changes. In the first example one change set is created for each item which is added, whereas in the second improved example a single change notification is made. The issue with adding one at a time is amplified by every subsequent operator having to also process items one at the time.