Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Application Responsiveness


Common Sources Of Problems

The general cause of event-processing bottlenecks is executing unbounded or high-latency operations on the GUI thread. More often than not, this code lives in user-authored code to respond to UI events. Such code might perform a simple CPU-intensive activity whose algorithmic complexity depends on some variable, say the size of the input data. When testing your program in-house, it might function okay with small sample data sizes. However, when users decide to operate on more data than you anticipated, it might consume more compute time, leading to precisely the situation just described.

That said, most programs aren't CPU-bound. The total amount of work your code performs is a complex equation, depending on variables such as the CPU, memory, peripherals (such as disk), and the network. Clearly CPU clock speed plays a major role, but often isn't the dominant factor. The CPU's internal parallelism and the degree to which it can achieve superscalar execution on a particular piece of code depend heavily on the number of branch prediction misses and instruction stream fences. These are generally of more interest to compiler writers than application developers, but use of locks, volatile reads/writes, interlocked operations, and memory barriers can also impact this. Memory-intensive operations can also lead to variable delays, especially on multiprocessor machines in which some portions of the cache hierarchy are more expensive to access than others. High cache miss rates can be crippling on applications, bloating the cost of memory-intensive work by an order of magnitude. Too much thread-level parallelism in your application—or among all the machine's running processes—can place a higher burden on the OS thread scheduler, leading to context-switching overhead. Many of these factors are never dealt with directly in your code, but should be part of a rigorous stress-testing process to catch problems before software is released. If you encounter issues like this during testing, a good profiler can measure and track down the source.

Device I/O clearly incurs a higher cost than most CPU- and memory-based activities, usually by several orders of magnitude. Most programs today are more and more connected, and as a result must send and receive larger quantities of data via the disk and network. Doing this type of work on the UI thread is almost always a mistake. Network I/O consists of many steps, and the cost of each varies greatly depending on the state of the network peripheral, the network, the destination node, and hops in between. Your program generally has no control over these factors, so doing such things on the UI thread is a disaster waiting to happen.

GUIs often perform dramatically worse under system-wide memory pressure. This is because ordinary memory operations can suddenly turn into disk I/O due to page faulting. This dramatically changes the performance characteristics of simple memory accesses once your program has to compete with others for scarce machine resources. If simple memory accesses can be seen as "variable latency operations," you're probably wondering if there's anything you should do on the GUI thread. Generally speaking, any data- or compute-intensive tasks should be done on a separate thread, even if that incurs overhead for worker synchronization.

Finally, synchronization resulting from access to shared data structures is another variable latency operation. If a lock is contended, the thread attempting acquisition typically ends up blocking. It is only awoken again when the thread that owned the lock finishes its work and relinquishes the lock (and any that are given access to the lock first). Although the thread that owns that lock might not be a GUI thread, it might be performing I/O or waiting for an event. This essentially looks as if the GUI thread itself were performing such operations because it has to wait the same amount of time. Deadlocks are the worst type of lock contention, especially on the GUI thread.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.