.NET 4 Tasks offers much better support for task cancellation, unlike QueueUserWorkItem tasks can be cancelled before commencing, and the Task library offers a standard way for running tasks to detect and report cancellation. I recently recorded a screencast that demonstrated the new Task API including the cancellation support. This blog post isn’t so much about the cancellation mechanics but some guidance on how best to use it if you don’t want to change the throughput of your task.
Below is some code that calculates pi, it is expecting to be run inside a task and is supporting the notion of cancellation.
So all well and good until you benchmark it and compare it to the version with no cancellation support and it runs almost twice as slow. The reason being that the cost of detecting cancellation is high in relation to the work being done. The important aspect to cancellation is being able to respond in a meaningful time for the client, at present we are being way over aggressive in checking.
One option would be to only check every N iterations..
This took a third less time than the previous more aggressive version. However the if block’s effect on the pipeline and the additional maths is still an additional cost over the version that had no cancellation support.
So a third approach is called for this time refactoring the algorithm to use two loops instead of one, were the check for cancellation is done once per iteration of the outerloop, this results in little additional cost.
Here are the timings are got from the various approaches
NoAbortableCalculatePi = 3.14159264958921 took 00:00:03.7357873
AbortableCalculatePi = 3.14159264958921 took 00:00:09.6137173
BetterAbortableCalculatePi = 3.14159264958921 took 00:00:06.3826212
OptimisedAbortableCalculatePi = 3.14159265758921 took 00:00:03.6883268
As the figures show there is virtually no difference between the first and last run, but a considerable difference when cancellation is inserted into the core of the computation.
So to sum up whilst cancellation support is good the frequency you check for could have an impact on the overall performance of your algorithm. Cancellation is something we want to support but in general users probably don’t need it so we need to strike the right balance between throughput and responding to cancellation in an appropriate timeframe.