-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Runtime worker threads #1459
Description
The runtime is currently single threaded, which means all transactions/extrinsics must be executed in series. This is rather suboptimal. Instead, worker "threads" should be allowed in the runtime environment.
Since this is a consensus computation and the need for determinism is absolute, there should be a number of restrictions on worker threads:
-
They should be pure functions. This implies that as long as there is at least one worker thread, storage may not be altered (though it may be read). Furthermore, there should be no other kinds of data-sharing between threads, even within
Sync
safeguards likeMutex
orAtomic
. Separate memory slabs should be used if needed. -
Worker threads may only be created from the main thread and have a specific identifier that is deterministic based upon the order in which they were created by the main thread.
If, while there is at least one worker thread, storage is written to, then there are two options that we should do one of:
- panic; or
- have
set_storage
buffer the alteration in a (thread-local) hash map, which ultimately takes effect once all worker threads are finished and in the order that worker threads were created (and thus deterministic).
Once this exists, a new issue should be created to introduce extrinsic parallelism whereby the block author prefixes the extrinsic set with an extrinsic to specify a dependency graph stating which extrinsics are mutually independent and can be executed in parallel. The runtime executive
can then use this information together with a number of worker threads in order to safely parallelise extrinsic queue execution.