WPF – what about performance?

Previously published on Today Software Magazine.

 

When it comes to .Net family of client applications, Windows Presentation Foundation is a name that is frequently found. WPF is no longer a novelty, being introduced from. Net Framework 3.0 but brings out graphical interface advantages that still make it competitive.

The temptation whenever there’s something new, is to make it the ideal solution for everything. There is an ongoing discussion when it comes to choosing a Microsoft client technology, but if you don’t use the advantages brought by WPF (data-binding, dynamic styling, media services, animations) for performance reasons a lot of developers still choose WinForms.

XAML world and Binding

Declarative module was successful in the web development and it has inspired a new language based on XML, called eXtensible Application Markup Language (XAML). XAML has many advantages, but what is worth mentioning is that it gets rid of the procedural code and allows the developer or site designer to describe how the controls look and interact.

The View in WPF contains a XAML and a code behind file. If we follow the MVVM pattern and there were no compromises in the design, the code-behind should be nothing more than what is generated automatically and the connection between data from View and ViewModel should be done trough Binding .

Binding is a powerful mechanism, it triggers necessary notifications to the UI when data representation in ViewModel changes.

To Blend, or not to Blend

Clearly one of the advantages that WPF brings is the extensibility when it comes to customization. If we are referring to design, as a step forward from WinForms, WPF provides developers the many possibilities and techniques to change the look and feel of the controls, and this underpins modern rich client applications centered on user interaction.

Microsoft comes forward with a design-oriented tool, which has its own build engine and is able to create WPF applications, independent of Visual Studio. Expression Blend separates roles and offers designers the opportunity to focus on the design and programmers on the code (presentation logic and back-end), providing the convenience on not blocking one another.

In Blend, the application is developed visually, simply drawing shapes, path-ups and controls directly on the board, and later describes the appearance and behavior.

In conclusion, if the application has been chosen to be developed in WPF, there is clearly a need for a complex interface and Blend brings a big help on this part.

MVVM

Model View ViewModel is a UI pattern, created by Microsoft as a specialization of the Presentation Model pattern (introduced by Martin Fowler). Based largely on MVC, MVVM is suitable to development environments where UI platform supports event-driven programming such as HTML, WPF, Silverlight.

The purpose of this pattern is to separate the graphical user interface (either markup or code) – View from business logic (backend) – Model. ViewModel is responsible for the connection between the two, giving details of the model in such a way as to be consumed by the view, for describing the same logic.

MVVM was designed to use data binding in WPF to better facilitate separation of UI, reducing most of the code behind of the View. The advantages are clearly visible, and it’s a good design pattern to follow in WPF applications.

In time, the critics did not fail to appear, and one comes from John Gossman, inventor MVVM, showing that it brings significant overhead if we have large operations at UI level, and in large applications with complex UI, fine tuning on the View becomes increasingly difficult to implement.

3rd party controls

In WPF, controls are the base of the UI structure, they are designed to achieve the desired graphical interface and could be reused. Over time there were providers who became famous, and perhaps imposed themselves by the set of WPF controls they offer.

3rd party controls on the one hand can be a challenge / opportunity for those who want to implement them, on the other hand bring great help to a team that wants to implement a quick solution to a problem.

When we do not want or have time to reinvent the wheel it seems a viable option to acquire an existing set of controls, which has proven over time to bring the solution you need. The major developers that deliver WPF controls are Telerik, Infragistics, DevExpress, which offer a very diverse suite of controls. The experience reached in the development of these controls is often difficult to match, and probably more expensive, and thus they persist in the market since WPF emerged.

One of the side advantages that a set of control brings is the benefit of technical support, with a fee of course. Besides abundant documentation available on the Internet, each provider has a technical department and this is a way to get clarifications to questions, concerns and possible bugs related controls.

Speaking of industrial applications, the performance of these controls is also important, as they are mostly black-box for consumers. There is a certain overhead if we speak of a consistent UI, and this can become a problem.

Perhaps the most „problematic” in terms of control is DataGrid’s performance. This is also because it takes more data to be displayed. Most controls offer ways for developers to make their product performant. What should be considered in this regard:

Virtualization: Many of the grid controls are virtualized, and if they aren’t already, you have a flag to set in this sense (can be made on Row / Column). Hint: do not put a DataGrid in a ScrollViewer, it basically loses virtualization.

  • Pagination: pagination can be done at UI level, with all the data already loaded, but you can try and load a dynamic data page to the View
  • Asynchronous loading: the binding can be used IsAsync = True, because in this case the data loading may take longer and the UI shouldn’t be blocked

Performance is one of the key issues to be considered when choosing a set of controls, because a mature application with a rich UI, but slow is not something that one would work with.

Performance

WPF is known for UI benefits they bring, but what about performance? I had the opportunity to work on several projects WPF, two stand out for long implementation time and magnitude of the product itself, where performance was vital.

WPF was not created to be efficient „by design”. Binding is the mechanism that underlines WPF but for each binding to be made, it requires memory. When it comes to complex screens with multiple controls, each subpart is linked to a data object, it is observed that there is a cost for all its advantages.

Another problem occurs in hierarchical data when the data context changes. Propagation is descending on all levels, and binding recovery takes time. This can become visible to the user, and an alternative is not always at hand. Unfortunately, you cannot see an advance of Microsoft to improve controls, and as evidence of this is that the best improvement it brought in this direction in .Net Framework 4.5 is virtualization of TreeView. Roadmap for WPF can be confirmed only briefly, so it is unclear what the future in this direction is, given that Silverlight, although promising at one point, was suddenly interrupted.

A parallel is often encountered between WPF and WinRT, and not for unfounded reasons. Both are based on XAML and similar API, but WinRT has been designed in a manner of performance. A WPF accumulated baggage left behind and has a native C + + API. From C# (or JavaScript) native components are called and this is felt in the reaction time.

In Windows 7 SDK there is a series of profiling application, which allow us to analyze at run-time how WPF applications behave and what optimizations can be made to enhance performance. In this series there are two components that are worth mentioning.

Perforator – analyzes how your application is rendering. It displays a set of graphs which allow to analyze in a very specific manner how each subpart of a screen is rendered and find potential problems. Charts provided by Perforator are: refresh rate analysis, software and hardware intermediate rendering changes, and video memory consumption.

Visual Profiler – presents performance issues in the context of how the layout was built over the application. Visual hierarchy is drilled down from high-level objects (such as buttons and TextBlock s) to the low-level (such as lines and ellipses). Rather than describing graphs showing performance problems, Visual Profiler describes problems using Visual representation of objects (in a similar way to how UISpy works).

As the software developer community is growing there is fierce competition between software solutions. I think it will eventually reduce to performance. This criteria will make the difference between a performant software and one used because it’s widely distributed, used solely because there is no alternative. Responsiveness of a program will be the satisfaction factor required, and the user begins to be educated and demand more and more in this direction.

References

en.wikipedia.org/wiki/Windows_Presentation_Foundation

en.wikipedia.org/wiki/Model_View_ViewModel

msdn.microsoft.com/en-us/library/cc296376.aspx

msdn.microsoft.com/en-us/library/188ht7d8(v=vs.80).aspx

en.wikipedia.org/wiki/Pareto_principle

msdn.microsoft.com/en-us/library/system.windows.data.binding.isasync.aspx