-
Notifications
You must be signed in to change notification settings - Fork 47
Add buffer pool for socket buffers to reduce memory for idle connections #1549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Claude finished @carlhoerberg's task —— View job PR Code Review - Buffer Pool ImplementationTodo List:
Issues Found1. Race Condition in Buffer Pool (
|
2b2a721 to
ac9c6c8
Compare
Introduce a buffer pool for IO::Buffered read/write buffers to save memory
when handling many concurrent connections. Most connections are idle most
of the time, so releasing buffers back to a shared pool when not actively
in use allows memory to be reused across connections.
- Add BufferPool class: thread-safe pool with acquire/release operations
- Override IO::Buffered methods to use pooled buffers:
- Read buffers: wait for socket readability before acquiring buffer,
release immediately when all buffered data is consumed
- Write buffers: released after flush
- Initialize pools at startup using configured socket_buffer_size
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <[email protected]>
ac9c6c8 to
25893c8
Compare
Reports per-fiber stack usage and summary statistics for read_loop fibers to help diagnose memory usage. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
Refactors BoolChannel to use atomic state and activate/deactivate mechanisms instead of a dedicated fiber per instance, reducing memory overhead for idle connections. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
|
Dropping memory usage for 10.000 connections, each with one channel, from 637MiB to 413MiB, a 35% reduction. |
Replace the spawned send_loop fiber with atomic operations and direct channel state manipulation. This eliminates a fiber allocation that could fail mid-constructor, which would leave orphaned fibers accessing uninitialized instance variables and cause segfaults. This change mitigates the issue in #1595 where BoolChannel.new could fail after spawning deliver_loop but before initializing all BoolChannel fields. Changes extracted from PR #1549. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
## Summary This PR extracts the BoolChannel changes from #1549 - Eliminate the spawned fiber in `BoolChannel` constructor by replacing it with atomic operations and direct channel state manipulation - This mitigates the segfault issue in #1595 where `BoolChannel.new` could fail after spawning a fiber but before initializing all instance variables, leaving orphaned fibers that access uninitialized memory - Also mitigates #1595 just by creating less fibers, resulting in fewer memory maps ## Changes - Replace `spawn(name: "BoolChannel#send_loop")` with atomic state management using `Atomic(Bool)` - Introduce `StateChannel` class that extends `Channel(Nil)` with `activate`/`deactivate` methods - Use `EndlessQueue` to implement channels that signal state without blocking on send - Remove the `send_loop` fiber entirely, reducing fiber allocations by one per BoolChannel instance Co-authored-by: Carl Hörberg <[email protected]>
This PR extracts the BoolChannel changes from #1549 - Eliminate the spawned fiber in `BoolChannel` constructor by replacing it with atomic operations and direct channel state manipulation - This mitigates the segfault issue in #1595 where `BoolChannel.new` could fail after spawning a fiber but before initializing all instance variables, leaving orphaned fibers that access uninitialized memory - Also mitigates #1595 just by creating less fibers, resulting in fewer memory maps - Replace `spawn(name: "BoolChannel#send_loop")` with atomic state management using `Atomic(Bool)` - Introduce `StateChannel` class that extends `Channel(Nil)` with `activate`/`deactivate` methods - Use `EndlessQueue` to implement channels that signal state without blocking on send - Remove the `send_loop` fiber entirely, reducing fiber allocations by one per BoolChannel instance Co-authored-by: Carl Hörberg <[email protected]>
Summary
IO::Bufferedread/write buffers to save memory when handling many concurrent connectionsTest plan
🤖 Generated with Claude Code