JuliaLang/libuv — pull request #19 by simonster (2014-02-05 03:54:38+00:00)
Title: Use clone() to launch processes on Linux

Fixes JuliaLang/julia#3536 (some benchmarks there) and probably JuliaLang/julia#5385.

On Linux, `fork` can be very slow with processes with high memory use due to the overhead to copy page tables. This PR uses `clone` with the `CLONE_VM` option to create a new process that shares memory with the current process but uses a different stack. This is extremely fast by comparison to `fork`, but it requires some care with respect to global state.

`clone` must be passed a pointer to the new process's stack. I am currently passing a small portion of our current stack, since the child process does not need much stack space. It might be safer to use `posix_memalign` to allocate the new process's stack on the heap, since it is less likely that the parent's memory could be corrupted if `uv_spawn` returns before the child exits or calls `execvp`/`execvpe`, but that is not supposed to happen. There seems to be a measurable performance difference, but my benchmarks are not necessarily indicative of real-world use.