Skip to content

RFC 2 : Delay Fiber stack association#15404

Closed
ysbaddaden wants to merge 7 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-delay-fiber-stack-association
Closed

RFC 2 : Delay Fiber stack association#15404
ysbaddaden wants to merge 7 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-delay-fiber-stack-association

Conversation

@ysbaddaden
Copy link
Collaborator

Fiber#initialize now takes an explicit stack. The public API doesn't change, and we can still create a Fiber without an explicit stack, taken from the current scheduler/context's stack pool.

Fiber::ExecutionContext#spawn takes advantage of this to take a stack from the execution context's stack pool, instead of the current context's stack pool. Cross context spawns thus won't take a stack from context A but release the stack in context B (where it actually ran) which would prevent stack recycling.

This also permits to create lots of Fiber instances in specs using a minimal fake stack, instead of requesting 8MB of virtual memory for each fiber, despite the fibers never running... which is leaking memory because we only release the stacks when the fiber terminates.

Follow up to #15345 and #15350

`Fiber::ExecutionContext#spawn` takes advantage of this to take a stack
from the execution context's stack pool, instead of the current
context's stack pool. Cross context spawns thus won't take a stack from
context A but release the stack in context B (where it actually ran)
which would prevent stack recycling.

This also permits to create lots of Fiber instances in specs using a
minimal fake stack, instead of requesting 8MB of virtual memory for each
fiber, despite the fibers never running... which is leaking memory
because we only release the stacks when the fiber terminates.
@ysbaddaden
Copy link
Collaborator Author

Allright: it eventually passed on mingw64 too.

@ysbaddaden ysbaddaden closed this Feb 4, 2025
@ysbaddaden ysbaddaden deleted the feature/execution-context-delay-fiber-stack-association branch February 4, 2025 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant