Web Workers: A Draft Recommendation to Allow Parallelism in Scripts

Most modern Web pages run scripts. So far, there are many limitations to allow these scripts to take advantage of multicore microprocessors. However, Web Workers is a draft specification that defines an API to allow Web application authors to spawn background workers running scripts in parallel.

There is a German proverb that says "Wer A sagt, muss auch B sagen". In for a penny, in for a pound. If you start something, you have to finish it. If you say A, then you also have to say B.

The WHATWG (Web Hypertext Application Technology Working Group) community has a draft recommendation for Web Workers. This is a work in progress. Besides, the W3C Web Apps WG is also producing this specification. Therefore, we should expect full support for Web Workers in HTML 5.

This specification defines an API for running scripts in the background independently of any user interface scripts. This way, developers can create more responsive Web pages. The long-running scripts can work in the background, in an independent thread and the UI scripts can run on another thread (the main UI thread). The browser is responsible of assigning threads to each Worker instance. A well-designed browser should run each worker in an independent thread, to favor parallelism. As happens with heavyweight threading, Worker instances have a high start-up cost and a high per-instance memory footprint.

The specification allows message-passing as the coordination mechanism. Thus, it allows developers to create multithreaded scripting, capable of exploiting modern multicore microprocessors.

The new API is really very simple. However, it is necessary to understand many multicore, message-passing and threading concepts before trying to use it. The scripts don't use direct threading, however, the Web browsers implementing this API will create independent threads for each new Worker instance. Developers can work with event listeners and direct channels. The direct channels really simplify the communications between Worker instances.

Using Web Workers, the user interface should't be blocked anymore. There's going to be a new way of designing and developing scripts for Web pages. Now, the scripts are also going to be able to take advantage of multicore microprocessors.

Do you have to wait for HTML 5? No, you don't. You can begin testing Web Workers in two Web browsers using JavaScript. FireFox 3.5 and Safari 4 already include support for Web Workers (draft). Therefore, if you are planning to create more responsive and faster scripts for your Web pages, you can begin working right now. Nonetheless, you have to take into account that it is a work in progress. For this reason, the specification might change over time.

There is a very interesting unofficial Web Worker sample test. You can run it using FireFox 3.5 (or greater) and Safari 4 (or greater). It was developed by Dennis Forbes, modifying an original "single-threaded" SunSpider JavaScript Benchmark.

You can select a Repeat Count value and the number of desired Web Workers. As aforementioned, Worker instances have a high start-up cost, therefore it is very important to use 10 as the minimum Repeat Count value. The test performs many heterogeneous algorithms using JavaScript and as many Web Workers as specified.

The following screenshots show the test running with different numbers of Worker instances (from 1 to 4) using a quad-core microprocessor. You can check the graph shown in the Windows Task manager.

Running the test with 1 Worker instance.

Running the test with 2 Worker instances.

Running the test with 3 Worker instances.

Running the test with 4 Worker instances.

Developers will be able to create scalable scripts using Web Workers. Multicore programming is also important for Web developers.

Real World Parallelism Webinar Series
  • November 17, 2009
    Visual Effects for Animation - presented by DreamWorks Animation
    Speaker: Ron Henderson (Bio)

    Ron Henderson manages the FX Tools group at DreamWorks Animation, where he is responsible for developing physical simulation and procedural modeling tools. These systems have been used for key visual effects in recent films such as Kung Fu Panda and Monsters vs. Aliens (March 2009).

    Prior to joining DreamWorks in 2002 he was a senior scientist at Caltech with a joint appointment to the Applied Math and Aeronautics departments, where he worked on efficient techniques for the direct numerical simulation of fluid turbulence.

    Abstract:
    In this webinar, Ron Henderson will show examples of visual effects, from hair and feathers to smoke and fire, from a variety of DreamWorks Animation feature films. He will discuss in general terms the kinds of techniques used to achieve particular visual effects. Finally, Henderson will show a detailed breakdown of the dam-breaking scene from Madagascar: Escape 2 Africa, demonstrating how different elements of key frame animation, simulation, and rendering are combined in a real production shot.

  • December 1, 2009
    A Quick and Easy Way to Parallelize a Legacy Codebase with Intel® Threading Building Blocks (TBBs)
    Speaker: Bernard Laberge, Avid, Senior Principal Engineer (Bio)

    Bernard Laberge is a senior principal engineer in the video editors division at Avid. During his seven years with the company he has been actively involved in the replacement of the legacy video processing engines used by Avid editors with a common hardware-abstracted, component-based video processing engine currently running on the CPU with SIMD optimized code, GPU, and dedicated hardware.

    Abstract:
    Learn how to overcome the limitations of a thread-based scheduler, including dealing with the absence of recursive parallelism support and the inefficient handling of unbalanced processing load. Bernard Laberge addresses how Avid resolved the expensive refactoring of their thread-based scheduler into a task-based solution by choosing Intel® Threading Building Blocks (TBBs). He explores how Avid was able to easily integrate the Intel TBBs into their video editor applications and more than 5 million lines of code.

  • December 15, 2009
    How to Use Intel® Parallel Studio to Streamline Code Development in a Multicore Environment
    Speaker: Matt Dunbar, Director for Performance Technology, SIMULIA (Bio)

    Matt Dunbar is the director for performance technology at SIMULIA. Since joining the company in 1993, he has worked on parallelization of the Abaqus suite of products, initially for shared memory architectures and more recently for distributed memory architectures. Dunbar has also been intimately involved in selecting both the hardware and software tools used in the development of the Abaqus product line.

    Abstract:
    Resolve elusive, costly multithreading errors quickly and efficiently with Intel® Parallel Studio. While many coding problems that lead to bugs in software applications are typically straightforward logic errors, errors in managing memory and in multithreading code can sometimes take weeks to months to diagnose and fix. Matt Dunbar explores how and why taking advantage of multicore processors through multithreaded code is critical for compute-intensive applications. While spotlighting his work on SIMULIA's Abaqus finite element solver, Dunbar addresses the need for multicore execution and shares his experiences using Intel Parallel Studio to streamline code development in a multicore environment.