<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dapfor.Net Grid Blog &#187; BindingList</title>
	<atom:link href="http://www.blog.dapfor.com/tag/bindinglist/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.dapfor.com</link>
	<description>.Net Grid and MFC Grid</description>
	<lastBuildDate>Fri, 09 Sep 2016 05:40:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Why BindingList is slow with objects implementing INotifyPropertyChanged interface</title>
		<link>http://www.blog.dapfor.com/why-bindinglist-is-slow-with-objects-implementing-inotifypropertychanged-interface</link>
		<comments>http://www.blog.dapfor.com/why-bindinglist-is-slow-with-objects-implementing-inotifypropertychanged-interface#comments</comments>
		<pubDate>Thu, 29 Mar 2012 18:59:31 +0000</pubDate>
		<dc:creator>dapadm</dc:creator>
				<category><![CDATA[Data binding]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[BindingList]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.blog.dapfor.com/?p=139</guid>
		<description><![CDATA[A BindingList&#60;T&#62; is the main tool for binding objects to grids. It is a container that may notify subscribers when data is added or removed. For this purpose it provides public ListChanged event that specifies collection changes with ListChangedType. Besides changing the collection itself (adding, removing, etc) the binding list notifies of data object changes. [...]]]></description>
			<content:encoded><![CDATA[<p>A <strong>BindingList&lt;T&gt;</strong> is the main tool for binding objects to grids. It is a container that may notify subscribers when data is added or removed. For this purpose it provides public ListChanged event that specifies collection changes with ListChangedType. Besides changing the collection itself (adding, removing, etc) the binding list notifies of data object changes. In other words, when data is added to collection <strong>BindingList&lt;T&gt;</strong> checks whether it implements <strong>INotifyPropertyChanged</strong> interface. If it is implemented, the collection subscribes to each object changes and forwards notifications as <strong>IBindingList.ListChanged</strong> events with <strong>ListChangedType.ItemChanged</strong> type and with specified PropertyDescriptor that is searched by reflection. </p>
<p>It’s worth mentioning that handler of <strong>INotifyPropertyChanged</strong> notification is very poorly implemented. Low performance reflection is used for searching PropertyDescriptor and internal cache is very poorly organized thus significantly reducing performance. Specifically when the <strong>BindingList&lt;T&gt;</strong> receives notifications of new object, it compares this object with the previous object that was used for notification. If they are not identical, the binding list does a terrible thing – it runs through all objects to get the index of the notifying object. This seriously impacts performance when there are a lot of elements. When the number of elements is about 100 000, handling of a single notification takes about 1 ms, which is unacceptable for most applications. </p>
<p>In actual applications <strong>ListChangedType.ItemChanged</strong> event with <strong>ListChangedType.ItemChanged</strong> flag and set PropertyDescriptor is handled quite rarely. However, it is hard to disable <strong>INotifyPropertyChanged</strong> handling in a <strong>BindingList&lt;T&gt;</strong> via simple means. The only acceptable method of disabling handling is to use reflection. The thing is that the binding list in its constructor checks whether the data type implements <strong>INotifyPropertyChanged</strong> interface and sets <strong>raiseItemChangedEvents</strong> private variable to true if it does. Otherwise it is set to false. This variable influences only subscriptions to <strong>INotifyPropertyChanged</strong> interface. There is no direct access to this variable and it can be modified only via reflection. An example of code for this is provided below. </p>
<pre>
class CustomBindingList&lt;T&gt; : BindingList&lt;T&gt;
{
    public CustomBindingList()
    {
        FieldInfo fi = typeof (BindingList&lt;T&gt;).GetField("raiseItemChangedEvents",
                               BindingFlags.Instance | BindingFlags.NonPublic);
        if (fi != null)
        {
            fi.SetValue(this, false);
        }
    }
}
</pre>
<p><br/></p>
<p>The above implements fully functional binding list with disabled handling of <strong>INotifуPropertyChanged</strong> interface events. At the same time the binding list notifies subscribers of adding or removing data just like in standard implementation of this container. </p>
<p>Some figures: In standard binding list implementation handling of 1000 notifications in 100 000 container took more than 600 msec. In optimized version this time – 0 since the container no longer subscribes to data objects. Besides that, no subscription enables memory saving. </p>
<p>This article concerns only binding list performance aspects. Another important aspect of working with binding lists <strong>INotifyPropertyChanged</strong> is their use in <a href="http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-binding">advanced binding</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dapfor.com/why-bindinglist-is-slow-with-objects-implementing-inotifypropertychanged-interface/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
