Move C heap allocations for RVALUE object data into GC heap#4391
Merged
Conversation
shyouhei
approved these changes
Apr 26, 2021
eightbitraptor
force-pushed
the
mvh-rvargc
branch
4 times, most recently
from
April 29, 2021 12:09
bb21c14 to
9c36370
Compare
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.
Introduction
This work supersedes the work in PR: 4107 and Redmine: 17570. We've reimplemented the feature to make the diff smaller, easier to maintain and less intrusive to existing data structures.
We're working at Shopify to restructure Ruby memory management in order to allow objects to occupy more than one heap slot. This will allow previously heap allocated data to be stored next to its associated
RVALUEslot in a contiguous memory region.We believe that this will simplify the internals of the GC by:
malloccalls for heap allocated objects by deferring them until the object is promoted to an old object. When objects no longer need to callmalloc, the transient heap can be removed.We believe that there will be performance improvements across most Ruby codebases as a result of these simplifications. Objects will also have improved data locality, resulting in improved hardware cache performance.
Summary of changes
This is a rewrite of a feature initially proposed in PR #4107.
The referenced PR adds the core implementation and API in order to store arbitrary length data inside contiguous free slots on the heap. It also includes a reference implementation for
T_CLASSobjects, that would usually allocate therb_classext_tstruct on the system heap. The current API is:RVARGC_NEWOBJ_OF- A reimplementation of theNEWOBJ_OFmacro that takes an additional parameterpayload_length, the length of the payload data to store in bytes.rb_rvargc_payload_data_ptr- avoid *to the start of the region where the extra data can be allocated.We've introduced a new type
T_PAYLOADand astruct RPayloadthat contains a singleVALUE flags. We use theFL_USERbits to store the number of payload slots so that we can stride over the payload body in most places where heap walking is required (as these slots can now contain user defined data they will not have accurateflagsand so most type checks will be incorrect).When
RVARGC_NEWOBJ_OFis called with a payload size, we calculate the number of slots required to store theRVALUE, anRPayloadand the payload data itself. We then first search the ractorsnewobj_cachefor a region of the required size, remove the slots from the freelist and initialize them.Then a pointer to the first allocatable byte in the payload body section can be found using
rb_rvargc_payload_data_ptr.These changes can be enabled using the compile time flag
USE_RVARGC=1.USE_RVARGCis disabled. Allocation ofRVALUEs in a single slot behaves almost identically to before this change (see Benchmarking data.Features (and challenges)
T_PAYLOADis fully integrated with the existing GC. The entire payload region will be treated as one single slot for marking, sweeping and generational purposes. In contrast with our previous attempt this means we no longer need to disable incremental marking, nor do we need to use an extra bitmap attached to a heap_page.T_CLASSand its payload region are pinned, so compaction will not move them. This has impacted the effectiveness of compaction, but unlike our previous PR, doesn't require us to disable compaction completely.USE_RVARGCis enabled. This is due to our (currently) naive approach to free region allocation.Next steps
With this merged. We have several different directions we intend to investigate
T_PAYLOADobject entirely.The end game for this work is to be remove the requirement for an
RVALUEto be exactly 40 bytes wide. This is obviously a long game, of which this PR takes the first steps.Benchmarking
We used Railsbench to compare the performance of master with our branch, with
USE_RVARGC=0And the same comparison using Optcarrot: