Make the C extension Ractor-safe by removing msgpack_rmem_* slab allocator#392
Open
ianks wants to merge 1 commit into
Open
Make the C extension Ractor-safe by removing msgpack_rmem_* slab allocator#392ianks wants to merge 1 commit into
msgpack_rmem_* slab allocator#392ianks wants to merge 1 commit into
Conversation
msgpack_rmem_* slab allocator
28616a5 to
bb0785b
Compare
…or_safe) The page-recycling slab was a process-global msgpack_rmem_t mutated through an unsynchronized bitmask, so parallel Ractors (or threads) raced on it. Drop the slab and serve rmem pages straight from xmalloc/xfree (jemalloc and friends recycle these fine), then declare rb_ext_ractor_safe(true). The declaration is guarded by HAVE_RB_EXT_RACTOR_SAFE so the extension still builds on Ruby < 3.0. Add spec/cruby/ractor_spec.rb covering pack/unpack via Factory and Packer/Unpacker from non-main Ractors, including a concurrent multi-Ractor stress over the page-allocation path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes the extension safe to use from a non-main Ractor. The notable change here is the removal of the
msgpack_rmem_*routines, which serve as a mechanism for efficiently providing chunks of memory for decoding work.Today the page-recycling slab is a process-global
msgpack_rmem_tmutated through an unsynchronized bitmask, so parallel Ractors (or threads) race on it.The fix drops the global slab and serves pages with
xmalloc/xfree, since modern mallocs (i.e. jemalloc) are good at recycling anyway.Details / Research
Single-threaded, jemalloc 5.3, decode-heavy workload, so treat these as directional.
Throughput
No measurable impact at the request level. On a realistic HTTP-request replay (paired by recording, QoS-pinned, ~9.4k samples/variant) the warm path is a wash and allocations are identical:
Dropping the recycling costs ~3% throughput (and more GC churn) on an isolated decode microbench, but msgpack is well under 2% of a real request and the warm path doesn't decode at all, so it doesn't surface end-to-end.
RSS Impact
Memory usage seems fine as well, with the bare-xmalloc (this PR) having a higher peak as decay time increases (this is expected, and not a problem).
dirty_decay_ms:10000(default)dirty_decay_ms:1000closes #390