www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Fiber implementation questions

reply Ilya Yaroshenko <ilyayaroshenko gmail.com> writes:
Hello,

I am thinking about faster event loop that vibe.d has (does not 
matter what driver are used). I want to implement (at least DIP) 
advanced Fiber yielding loop that can allow to eliminate context 
switching for all kind of event waiters.

Q1: Is it save to detach Fiber context from one thread and attach 
it to another thread?

Q2: core.thread.Fiber implementation looks like GC does not scan 
function stacks for yielded contexts. Why?

Best regards,
Ilya
Jun 20 2016
next sibling parent =?UTF-8?Q?S=c3=b6nke_Ludwig?= <sludwig outerproduct.org> writes:
Am 20.06.2016 um 17:43 schrieb Ilya Yaroshenko:
 Hello,

 I am thinking about faster event loop that vibe.d has (does not matter
 what driver are used). I want to implement (at least DIP) advanced Fiber
 yielding loop that can allow to eliminate context switching for all kind
 of event waiters.
I'm currently working on a successor of vibe.d's core module [1], which is more or less an internal rewrite to yield maximum performance. The vibe.d side definitely plays a part there, but most of the improvements are based on a rewritten event loop abstraction [2], which gives roughly a 3x speedup over both, libevent and libasync*, for a simple HTTP server request benchmark. This event loop abstraction is currently just a proof of concept and still experimental, though.
 Q1: Is it save to detach Fiber context from one thread and attach it to
 another thread?
It will at least always break TLS variables in one way or another. [1]: https://github.com/vibe-d/vibe-core [2]: https://github.com/vibe-d/eventcore * May have changed in the past weeks/months
Jun 20 2016
prev sibling next sibling parent Dicebot <public dicebot.lv> writes:
On 06/20/2016 06:43 PM, Ilya Yaroshenko wrote:
 Hello,
 
 I am thinking about faster event loop that vibe.d has (does not matter
 what driver are used). I want to implement (at least DIP) advanced Fiber
 yielding loop that can allow to eliminate context switching for all kind
 of event waiters.
 
 Q1: Is it save to detach Fiber context from one thread and attach it to
 another thread?
No, not without special (expensive) runtime and possibly compiler support. Primarily because TLS.
 Q2: core.thread.Fiber implementation looks like GC does not scan
 function stacks for yielded contexts. Why?
Don't know. It indeed sounds wrong though I have never experienced premature collection with fibers so the matter maybe more complicated than it seems.
Jun 20 2016
prev sibling parent reply deadalnix <deadalnix gmail.com> writes:
On Monday, 20 June 2016 at 15:43:27 UTC, Ilya Yaroshenko wrote:
 Hello,

 I am thinking about faster event loop that vibe.d has (does not 
 matter what driver are used). I want to implement (at least 
 DIP) advanced Fiber yielding loop that can allow to eliminate 
 context switching for all kind of event waiters.

 Q1: Is it save to detach Fiber context from one thread and 
 attach it to another thread?
No, and there are no way to make it safe without significant penalty for ALL code (not just code using fibers) and breaking the type system.
 Q2: core.thread.Fiber implementation looks like GC does not 
 scan function stacks for yielded contexts. Why?
Doesn't it ?
Jun 20 2016
next sibling parent Ilya Yaroshenko <ilyayaroshenko gmail.com> writes:
On Monday, 20 June 2016 at 20:17:04 UTC, deadalnix wrote:
 On Monday, 20 June 2016 at 15:43:27 UTC, Ilya Yaroshenko wrote:
 Q2: core.thread.Fiber implementation looks like GC does not 
 scan function stacks for yielded contexts. Why?
Doesn't it ?
I thought the code should be added to the GC roots.
Jun 21 2016
prev sibling parent reply Dicebot <public dicebot.lv> writes:
On 06/20/2016 11:17 PM, deadalnix wrote:
 Q2: core.thread.Fiber implementation looks like GC does not scan
 function stacks for yielded contexts. Why?
Doesn't it ?
https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4328 allocates fiber stack using mmap / malloc / VirtualAlloc and doesn't seem to register that memory in GC. But that may be indirect result of https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4439 call, which is why I am not sure.
Jun 21 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/21/16 6:14 AM, Dicebot wrote:
 On 06/20/2016 11:17 PM, deadalnix wrote:
 Q2: core.thread.Fiber implementation looks like GC does not scan
 function stacks for yielded contexts. Why?
Doesn't it ?
https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4328 allocates fiber stack using mmap / malloc / VirtualAlloc and doesn't seem to register that memory in GC. But that may be indirect result of https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4439 call, which is why I am not sure.
It appears that Fibers interact with the GC differently: https://github.com/dlang/druntime/blob/master/src/core/thread.d#L1611 -Steve
Jun 21 2016
parent reply Dicebot <public dicebot.lv> writes:
On Tuesday, 21 June 2016 at 12:18:37 UTC, Steven Schveighoffer 
wrote:
 On 6/21/16 6:14 AM, Dicebot wrote:
 On 06/20/2016 11:17 PM, deadalnix wrote:
 Q2: core.thread.Fiber implementation looks like GC does not 
 scan
 function stacks for yielded contexts. Why?
Doesn't it ?
https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4328 allocates fiber stack using mmap / malloc / VirtualAlloc and doesn't seem to register that memory in GC. But that may be indirect result of https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4439 call, which is why I am not sure.
It appears that Fibers interact with the GC differently: https://github.com/dlang/druntime/blob/master/src/core/thread.d#L1611 -Steve
I suspected something like this, it explains a lot. Do you know why this approach was chosen instead of simply registering allocated memory for GC scan? Does GC has special approach for stack scanning which differs from normal memory?
Jun 21 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/21/16 11:43 AM, Dicebot wrote:
 On Tuesday, 21 June 2016 at 12:18:37 UTC, Steven Schveighoffer wrote:
 On 6/21/16 6:14 AM, Dicebot wrote:
 On 06/20/2016 11:17 PM, deadalnix wrote:
 Q2: core.thread.Fiber implementation looks like GC does not scan
 function stacks for yielded contexts. Why?
Doesn't it ?
https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4328 allocates fiber stack using mmap / malloc / VirtualAlloc and doesn't seem to register that memory in GC. But that may be indirect result of https://github.com/dlang/druntime/blob/master/src/core/thread.d#L4439 call, which is why I am not sure.
It appears that Fibers interact with the GC differently: https://github.com/dlang/druntime/blob/master/src/core/thread.d#L1611
I suspected something like this, it explains a lot. Do you know why this approach was chosen instead of simply registering allocated memory for GC scan? Does GC has special approach for stack scanning which differs from normal memory?
There must be a reason, I think maybe Martin or Sean would know more. -Steve
Jun 21 2016
parent reply David Nadlinger <code klickverbot.at> writes:
On Tuesday, 21 June 2016 at 16:12:13 UTC, Steven Schveighoffer 
wrote:
 On 6/21/16 11:43 AM, Dicebot wrote:
 On Tuesday, 21 June 2016 at 12:18:37 UTC, Steven Schveighoffer 
 wrote:
 On 6/21/16 6:14 AM, Dicebot wrote:
httpthub.com/dlang/druntime/blob/master/src/core/thread.d#L1611
I suspected something like this, it explains a lot. Do you know why this approach was chosen instead of simply registering allocated memory for GC scan? Does GC has special approach for stack scanning which differs from normal memory?
There must be a reason, I think maybe Martin or Sean would know more.
You don't want to be scanning the unused part of the stack region. -David
Jun 21 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/21/16 3:03 PM, David Nadlinger wrote:
 On Tuesday, 21 June 2016 at 16:12:13 UTC, Steven Schveighoffer wrote:
 On 6/21/16 11:43 AM, Dicebot wrote:
 On Tuesday, 21 June 2016 at 12:18:37 UTC, Steven Schveighoffer wrote:
 On 6/21/16 6:14 AM, Dicebot wrote:
 httpthub.com/dlang/druntime/blob/master/src/core/thread.d#L1611
I suspected something like this, it explains a lot. Do you know why this approach was chosen instead of simply registering allocated memory for GC scan? Does GC has special approach for stack scanning which differs from normal memory?
There must be a reason, I think maybe Martin or Sean would know more.
You don't want to be scanning the unused part of the stack region. -David
OK, that's... pretty obvious, thanks :) -Steve
Jun 21 2016