On Sat, 07 Dec 2013 04:06:29 GMT, Sergei Nosov wrote:

On Fri, 06 Dec 2013 16:34:17 -0500, Etienne Cimon wrote:

If you need to run routines that take a lot of CPU cycles, you can call
yield() every now and then to make sure other requests aren't left out
while this operation runs.

Ok, that's pretty much what I've thought.

Only threads are exposed to
data races, but by default, vibe.d runs on one thread only.

I'm not really familiar with web server development, but isn't it sub-optimal? I would expect that the "main loop" would have a pool of threads (with capacity = number of CPU cores), also it'll have some queue of fibers and it will run assigning those to free threads. If we're running on a single thread - we're not utilizing more than 1 core. Is my understanding correct? If so, what is the proposed way to utilize more cores?

As long as everything is I/O bound, which can be considered a common case when DB queries and sizable pages are generated, a single thread is the most efficient choice. But an important point is that it greatly simplifies the threading model and doesn't require synchronization primitives to avoid low-level race conditions, which can give it an advantage compared to multi-threaded operation, even if the program is CPU bound. If this is not the case, there are two options to employ threads:

  1. Currently, to enable multi-threaded processing in general, vibe.core.core.enableWorkerThreads needs to be called, which creates a worker thread per CPU core (this won't be necessary anymore in a later version). Then HTTPServerOption.distribute needs to be added to HTTPServerSettings.options. Now all incoming TCP connections will be distributed among the worker threads evenly by the OS.

  2. A good alternative can be to do complex computations using runWorkerTask (which lets the task run in one of the worker threads) and let all I/O and request processing still happen in the main thread. This can often avoid explicit synchronization, while still fully exploiting the CPU's cores and thus simplifies the overall program architecture and may give a performance advantage.