Saturday, April 26, 2008

Parallelism, Concurrency and the free lunch

For anybody who is looking for ways of how to take advantage of the CPU capability to make their program run faster, I would suggest to look at the Parallel Framework Extension and download the PFX CTP to try it out.

I was just optimizing my program. On my dual core machine, the improved version of my program which use PFX give me an approximate of 18% performance improvement over the original program which use a conventional foreach loop. As my data set increase, the gain is up to about 35%.

Why this is a big deal?

As the silicon has reach its physics limit of getting more into the CPU chip, we cannot depend on Moore’s law to take care of performance for us anymore. The free lunch is pretty much over.

Instead, we can see emerging trend in parallelism and concurrency. People today are talking about multi core processors and how to make program run faster by utilizing all the cores they have in the computer; functional programming language is getting more attention; functional programming elements start to incorporated into mainstream imperative language (example is Linq); Software Transactional Memory is also getting more research effort.

I bet that concurrency is going to be one of the important topics in the next wave of programming paradigm.

Having say that, I dont mean concurrency is the ultimate to all problems. Concurrency is a tough subject and hard to get it right. You have to weight whether the overhead of locking is worth the benefits; You have to understand the side effects of state changes; You have to find a way to handle exception and etc. Concurrency is mean to be a way of tapping into the spare processing capacity, not a way to make slow code fast. You still have to understand where the bottleneck is in your code before you decide to settle on making things more concurrent. Use it with care.


Wednesday, April 09, 2008

DataBoy 1.0 released

I have just publish DataBoy version 1 in codeplex.

DataBoy is a small footprint and portable tool to perform simple query against SQL CE and SQL Server. It is used as an alternative query tool for SQL Management console.

The download link is here : DataBoy


Tuesday, April 08, 2008

Thought on WPF

I recall that some time I go I was having a conversation with my colleague about WPF. His complaint was that the XAML thing is really complicated, messy and difficult to comprehend. While I am supporting the blue badge camp, there is in fact some truth to his statements and this lead to an interesting discussion and thought.

WPF and XAML is kind of a revolutionary, and it requires a new way of thinking and accepting it. Although XAML should feel natural to developer and designer who are familiar to HTML, the expressiveness of XAML is extended way beyond of what we can used to imagine. Just like HTML, XAML is very good and simple in specifying the UI layout and content without needing to think through the object model. However, as the UI get more complex and the XAML growth, it become more difficult to manage a large chunk of XAML.

Second, the tool support for WPF is still pretty weak. Visual Studio 2008 and Expression Blend each has a WPF UI designer, but neither are really strong. Visual Studio 2008 are sufficient for creating simple WPF application but is not the right tool for creating complex, fancy and interaction rich application. Expression Blend fill in this gap but the shortcoming is it does not have intelisense support and the XAML editor does not support the same short cut key as in Visual Studio. Add on to this is the performance of the WPF designer in both tool sucks! The debug-ability of XAML application is also very weak. The best thing Visual Studio does now is just to throw an exception in its InitializeComponent() method whenever there is an error in the XAML. It is up to the developer to figure out what went wrong. To be fair, WPF is still a relatively new thing, and we should give the blue badge guy more time to get the tool right. Hopefully it won’t take long for this to happen.

WPF introduce a lot of new concepts such as property element, markup extension and so on. Developer who are not familiar with XML has even a harder time to understand how XAML is structured (I had a colleague who just managed to figure out what XML namespace is recently). Using markup extension in XAML really takes some time to figure out how it works because of the way it is expressed in attribute value string and the special syntax notation it had.

Enough rambling about WPF and XAML. XAML does have some good thing that we should praise about. Most notably, The expressiveness of XAML makes certain expression easier to understand than in imperative code. For example, let say I have a button on the form which width I want it to automatically adjust based on a value in a textbox that the user can type in. The most obvious way and simplest to do this is using data binding. Specifying in XAML will look like this :

<Button x:Name="btnOk" Content="Clickme"
Width="{Binding Path=Text, ElementName=txtWidth, Mode=Default}" />
<TextBox x:Name="txtWidth" Text="120"/>

To achieve the same effect in C#, the code will look like this:

Binding myBinding = null;
myBinding = new Binding("Text");
myBinding.ElementName = "txtWidth";
btnOk.SetBinding(Button.WidthProperty, myBinding);

Binding myBinding = null;
myBinding = new Binding("Text");
myBinding.ElementName = "txtWidth";
BindingOperations.SetBinding(btnOk, Button.WidthProperty, myBinding);

Neither of the C# code is any easier to understand than the XAML code. On top of that, it is not easy and obvious to figure out the right API and object model to do it in C# code. When I type btnOk.width in Visual Studio, intellisense show that width property is of type double, thus it seem that there can be nothing much that can be done about it. So, XAML win in this aspect.

The XML nature of XAML allow us to visualize better the layout hierrarchy of UI controls. The ability to define resources such as style and templates in XAML right at the appropriate context and being able to specify complex UI controls in the Content property in XAML add on to the clarity and simplicity of the UI definition than doing it at code level.

WPF and XAML is still weak is some areas especially the tooling support. But nonetheless, this should not hinder us from embracing a new technology. The technology landscape is constantly changing and we should always approach new technology with a new and open mindset.