From 9c83502ed8bbb29ef9a7e4f5d8a96f3df77ca820 Mon Sep 17 00:00:00 2001 From: Takoroka <137927095+Takoroka@users.noreply.github.com> Date: Sun, 1 Nov 2026 05:36:58 -0500 Subject: fiber: leave guest thread stacks uninitialized, drop the unused rewind stack Each fiber zero-filled two 2 MiB std::arrays, the stack and a rewind stack nothing ever switched to, so every guest thread held 4 MiB of RAM however little stack it used: ~750 MiB for title 1's ~190 threads. The stack is now allocated without being touched, so the host backs only the pages a thread uses. (cherry picked from commit f5e7d0f2eed854a0f821f8a04253f04cf77db575) --- src/common/fiber.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/common/fiber.cpp') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 0e7f28c5b9..613ac97e22 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -25,8 +25,10 @@ struct Fiber::FiberImpl { FiberImpl() {} u32 canary_1 = CANARY_VALUE; - std::array stack{}; - std::array rewind_stack{}; + // Left uninitialized: the host gives a stack's pages memory only as they are used. Zero-filled + // std::arrays (and a second stack nothing switched to) made every fiber, one per guest thread, + // hold 4 MiB: ~750 MiB for title 1's ~190 threads. + std::unique_ptr stack; u32 canary_2 = CANARY_VALUE; boost::context::detail::fcontext_t context{}; @@ -38,7 +40,6 @@ struct Fiber::FiberImpl { std::shared_ptr previous_fiber; u8* stack_limit = nullptr; - u8* rewind_stack_limit = nullptr; bool is_thread_fiber = false; bool released = false; }; @@ -49,10 +50,10 @@ void Fiber::SetRewindPoint(std::function&& rewind_func) { Fiber::Fiber(std::function&& entry_point_func) : impl{std::make_unique()} { impl->entry_point = std::move(entry_point_func); - impl->stack_limit = impl->stack.data(); - impl->rewind_stack_limit = impl->rewind_stack.data(); + impl->stack.reset(new u8[DEFAULT_STACK_SIZE]); + impl->stack_limit = impl->stack.get(); u8* stack_base = impl->stack_limit + DEFAULT_STACK_SIZE; - impl->context = boost::context::detail::make_fcontext(stack_base, impl->stack.size(), [](boost::context::detail::transfer_t transfer) -> void { + impl->context = boost::context::detail::make_fcontext(stack_base, DEFAULT_STACK_SIZE, [](boost::context::detail::transfer_t transfer) -> void { auto* fiber = static_cast(transfer.data); ASSERT(fiber && fiber->impl && fiber->impl->previous_fiber && fiber->impl->previous_fiber->impl); ASSERT(fiber->impl->canary_1 == CANARY_VALUE); -- cgit v1.2.3