A user account is required in order to edit this wiki, but we've had to disable public user registrations due to spam.
To request an account, ask an autoconfirmed user on Chat (such as one of these permanent autoconfirmed members).
Navigator HW Concurrency: Difference between revisions
(Create navigator.cores proposal) |
(→Abstract: extraneous word) |
||
Line 9: | Line 9: | ||
As opposed to previous proposals, this does not rely on creating a completely new Worker API. Instead, this API aims to help parallel applications using the current Worker API with hardcoded threadpools increase performance by changing a single line of code. All that parallel applications need to do to make use of this API is to replace code like <code>threads = X</code> with <code>threads = navigator.cores || X</code>. | As opposed to previous proposals, this does not rely on creating a completely new Worker API. Instead, this API aims to help parallel applications using the current Worker API with hardcoded threadpools increase performance by changing a single line of code. All that parallel applications need to do to make use of this API is to replace code like <code>threads = X</code> with <code>threads = navigator.cores || X</code>. | ||
Currently, highly parallel algorithms must prompt the user for how many cores they have. Many users do not know this information, nor should they be expected to know | Currently, highly parallel algorithms must prompt the user for how many cores they have. Many users do not know this information, nor should they be expected to know. Giving users control over thread count can lead to massive slowdowns in situations where the user thinks the highest option is best. For example, this can result in 32 threads being run on a user's dual core laptop when a more appropriate threadpool size is 2 or 4 threads. | ||
== Use cases == | == Use cases == |
Revision as of 02:22, 1 May 2014
Proposed navigator.cores API for efficient Worker allocation in parallel applications
Abstract
This specification defines an API for reading the system's total number of user-accessible logical processors.
The intended use for the API is to help developers appropriately size their worker threadpools for the user's system in order to perform CPU-intensive parallel algorithms.
As opposed to previous proposals, this does not rely on creating a completely new Worker API. Instead, this API aims to help parallel applications using the current Worker API with hardcoded threadpools increase performance by changing a single line of code. All that parallel applications need to do to make use of this API is to replace code like threads = X
with threads = navigator.cores || X
.
Currently, highly parallel algorithms must prompt the user for how many cores they have. Many users do not know this information, nor should they be expected to know. Giving users control over thread count can lead to massive slowdowns in situations where the user thinks the highest option is best. For example, this can result in 32 threads being run on a user's dual core laptop when a more appropriate threadpool size is 2 or 4 threads.
Use cases
- Physics engines for WebGL games: Many physics engines are highly parallelizable, but currently there is no method to determine how many threads to use. Games with optional cosmetic physics can also use navigator.cores to detect systems with too few cores and disable cosmetic physics by default.
- Using xz (LZMA2) in JavaScript to compress data before saving to disk without having to prompt the user for worker thread count.
- Running realtime object/face/movement/etc. detection algorithms on webcam input or video file input, without prompting the user for a worker thread count.
- Image processing in online photo editors is highly parallelizable but often hardcoded to a specific worker count. For example, this recent blog post on image processing in JavaScript suggests hardcoding the worker count to 4. All the author has to do to is replace the 4 with
navigator.cores || 4
to increase performance in computers with more cores (such as their own computer).
- Multithreaded automatic OCR: A current attempt at automatic OCR is http://projectnaptha.com/ (single-threaded). If Project Naptha is ever going to use the multithreaded Ocrad mode, they must currently prompt the user for a core count. This defeats the purpose of a silent background processing script by interrupting the user with a prompt.
- Anything else highly parallizeable, such as raytracer webapps like http://tech.pusherhq.com/demo/raytracer_workers
API
WebIDL
[NoInterfaceObject, Exposed=Window,Worker] interface NavigatorCores { readonly attribute unsigned long cores; };
Privacy concerns
System core count is public information as it is already accessible with the polyfill in the appendix. If you believe core count is a privacy leak, then the only solution is to completely remove web worker threads from the specification, as a timing attack on threadpool performance will always be possible otherwise.
Appendix
A polyfill in JavaScript can be found at:
The polyfill works by running a timing attack on measured runtime of a worker threadpool that is resized according to a binary search until performance no longer increases with the number of threads.
Performance is O(log n) in the number of cores.