All 7 entries tagged Csharp

No other Warwick Blogs use the tag Csharp on entries | View entries tagged Csharp at Technorati | There are no images tagged Csharp on this blog

August 05, 2006

BindingListView speedy sorting

I received some feedback about my BindingListView library saying it was very slow for sorting large lists. At first I thought 20,000 objects in a view was a bit extreme, but I tested with a DataView object and it worked OK. So I decided to hunt down the bottleneck in BindingListView sorting.
Using the CLR Profiler I determined it was reflection calls taking most of the execution time (no suprises there, reflection is very slow!)
When sorting the BLV used reflection to read the sorted property value from each object. The sorting 20,000 objects means and lot of calls to "GetValue", resulting in crippled performance.
The solution came from .NET 2.0's System.Reflection.Emit.DynamicMethod class. Using this class I was able to emit the IL (Microsoft .NET Intermediate Language) that directly accessed an object's property. (Of course the name of the property is supplied at runtime.)
I actually generate a few different methods depending on it the property is a value type or reference type. Since a value type needs to be boxed before we can compare using IComparable.
Here is one of the methods:
private static Comparison BuildRefTypeComparison(PropertyInfo pi, ListSortDirection direction)
{
MethodInfo getMethod = pi.GetGetMethod();
Debug.Assert(getMethod != null);

DynamicMethod dm = new DynamicMethod("Get" + pi.Name, typeof(int), new Type[] { typeof(T), typeof(T) }, typeof(T));
ILGenerator il = dm.GetILGenerator();

// Get the value of the first object's property.
il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Call, getMethod, null);

// Get the value of the second object's property.
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Call, getMethod, null);

// Cast the first value to IComparable and call CompareTo,
// passing the second value as the argument.
il.Emit(OpCodes.Castclass, typeof(IComparable));
il.EmitCall(OpCodes.Call, typeof(IComparable).GetMethod("CompareTo"), null);

// If descending then multiply comparison result by -1
// to reverse the ordering.
if (direction == ListSortDirection.Descending)
{
il.Emit(OpCodes.Ldc_I4_M1);
il.Emit(OpCodes.Mul);
}

// Return the result of the comparison.
il.Emit(OpCodes.Ret);

// Create the delegate pointing at the dynamic method.
return (Comparison)dm.CreateDelegate(typeof(Comparison));
}

Emitting IL by hand is not pretty (see my previous post for an idea to improve this problem). Using Reflector (or ILdasm) certainly is a must if, like me, you are an IL newbie. I wrote some simple example methods in C#, compiled them, then viewed in Reflector to see the IL they produced.

The end result in my speed test harness was BindingListView sort actually out performing the DataView!
Using 200,000 objects (10x more than the reported amount). Sorting on an Int32 property.
Old BindingListView took 146,823ms
New BindingListView took 16,564ms
DataView took 20,654ms

All the code is online at Source Forge


May 18, 2006

WMP Plug–in Library Source

Here's the source for my Windows Media Player plug–in library. You can use it to create your own plug–ins.

link

As usual, no warranties come with the code – do what you like with it. If you do make something cool, do let me know ;)


May 09, 2006

WMP Podcast Plug–In

IE7's feed subscription engine will download enclosures if you tell it too (it's a per–feed setting). However the IE feed browser does not have a direct way to see which items have enclosures. So I decided to create a plug–in for Windows Media Player. The plug–in reads the user's "common feed list" looking for downloaded audio enclosures. These are then listed in a simple grid directly within WMP. I can then simply click a file to start playing.
Screenshot:
WMP Plug-In

I wrote the plug–in in just a few hours using VS.NET 2005 with C#2.0! There are still enhancements required, but it does work great already.

Having the MS feed engine syncing all my feeds (podcasts, news, blogs, everything!) and using different applications to view aspects of them is better than trying to have one application do everything.

I may add support for searching and browsing podcast specific content (podcast alley, etc). No need to install iTunes at all! :D


November 17, 2005

Screen Cast #1 – Data Bound Tree View

Writing about web page http://www.equin.co.uk/screencasts/01-databoundtreeview.wmv

I decided that trying to write about the projects I've been working on would take too much time and not really get across the ideas as easily as just showing you! Therefore I decided to have a go at a "screen cast".

This first video demos the basics of my new Data Bound Tree View control. I'm still playing around with recording settings, etc, so hopefully the quality will improve for the next one.

Any comments, suggestions are welcome.


November 08, 2005

Recursive Data Bound TreeView

Follow-up to Data Bound Tree View from codeMonkey.Weblog();

I decided that my data bound treeview control should support recursive data structures. For example an OPML file consists of a set of "outline" elements and each outline can have child outlines inside. Therefore I've added a method to the control called "AddRecursiveLevel". So the tree will then recurse over the data structure until all leaves have been reached. The recursive level cannot have a CurrencyManager to update because that would require changing the data source of it (which can't be done easily). It does not make much sense to have an idea of "currency" when dealing with arbitrary depth tree data structure. The solution is to add a SelectedDataItem property to the control. Code that uses the treeview control can data bind that property to the datasource property of a BindingSource component. Other UI controls can then bind to that BindingSource. That way the user can drill down to a node in the tree view and then edit data using controls bound to the selected item.

There is still a fair bit of work to do in terms of enforcing constraints and documentation. I'm very busy getting ready for my trip to the USA on Thursday. Hopefully when I return I will have some more free time to work on the control.


October 27, 2005

Data Bound Tree View

I just wanted to mark this moment time as when I got my DataBoundTreeView control working! I'm going to put up a full post later explaining what it actually is. I am however very pleased with it! :D

October 22, 2005

AggregateBindingListView

Writing about web page http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=ccbb6b50-6c55-4291-b191-f5c5f80ba122

It took an afternoon of coding (and about a day or so of background thought process from the past month), but I finally created a working data-bindable aggregate object list view class. It grew out of the BindingListView project . The extension is that now multiple source lists of items can be merged into a single view. This view can then be sorted, filtered and searched.
An example of where this is useful would be an RSS feed reader application. Here you would want to show a single feed's list of items and also allow the user to view all items from all feeds. The user should be able to sort the items and search, etc, even when viewing all items from all feeds. The AggregateBindingListView does just that!

The plain old BindingListView is infact a degenerate case of the aggregate class, i.e. has just one source list of items. I have therefore now made it simply inherit from AggregateBindingListView and add an additional constructor that takes the list to view.

One special point for people using the ABLV class, the "NewItemsList" property must be set before items can be added using UI controls like the DataGrid. That way the ABLV knows which list to add the new item to. Otherwise adding new items in that way is disabled. (BindingListView automatically sets that property in the constructor, so no changes needed there.)

I'm very happy with the code and have put together a simple RSS reader application to test it. More testing is required, since the concept of merging multiple lists and keeping things like additions, edits, deletes in sync is by no means straight forward. I also need to complete/update the XML documentation. A further addition to the library would be a class to parse SQL style filter predicates. (We already have a SQL sort property implemented.) This is a fair amount of work, but would allow things like:
view.ApplyFilter("Name LIKE 'A*' and Age >= 18")
Currently only coded filters (classes that implement IFilter) exist.

Any interested coders are welcome to join the GotDotNet workspace linked above.


Google Ads

Search this blog

Most recent comments

  • I scratched my eye while i was holding some kind of plastic packaging.. Anyways the corner of the pl… by Ercan on this entry
  • About a year ago my contacts that i was wearing, i guess were fautly, because shortly after they wer… by Jon on this entry
  • I got shower gel in my eye 4 and a half days ago, and becasue i rubbed my eyes a lot, i have scratch… by Chris on this entry
  • This website may help http://www.webmd.com/eye–health/tc/Eye–Injuries–Home–Treatment by S on this entry
  • I somehow got dirt, or debris in my eye. The terrible pain sent me in a tailspin. I was afraid of sa… by Bobbi on this entry

Tags

October 2021

Mo Tu We Th Fr Sa Su
Sep |  Today  |
            1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Galleries

Blog archive

Loading…
RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder
© MMXXI