Repository navigation
Description
Current memory usage
On railsbench, with M1 Mac and stats build of ruby@0949cd7, yjit_alloc_size is 9.0MB (code_region_size is 6.9MB).
Heap profiling result
Using yjit-dhat branch, the profiling result was:
- 3.1MB (33.9%):
Blockbygen_single_block - 2.2MB (23.9%):
Branchbymake_branch_entry - 1.9MB (21.5%):
Vecbyget_or_create_version_list, etc. - 0.7MB (7.6%):
BranchStubbyset_branch_target - 0.5MB (5.8%):
HashMapbyassume_method_lookup_stable, etc. - 0.2MB (2.3%):
IseqPayloadbyget_or_create_iseq_payload - 0.2MB (2.1%):
BranchTargetbyset_branch_target
Struct counts
Using yjit-mem-stats branch, the breakdown of structs was:
# of IseqPayload: 2190
# of Block: 15296
# of Branch: 26968
# of BranchTarget: 28161
# of BranchTarget::Stub: 11836
# of BranchTarget::Block: 16325
size of IseqPayload: 96
size of Block: 176
size of Branch: 56
size of BranchTarget: 16
size of BranchStub: 58
size of Context: 38
Note that #s might be missing some unreachable structs on the heap, but then it might also mean they are currently leaking.
Known ideas
The following ideas are ordered based on the estimated impact (the earlier, the more impactful).
1. Eliminate extra blocks/branches created by defer_compilation
Related: #462
defer_count was 6883. If we just replace the previous block with a combined block, we could reduce Blocks and Branches by 6883, which would save more memory than any other idea.
2. Hash consing for Context
The idea was proposed by Maxime.
Context could be a series of "cons" where each cons is a pair of a diff and a pointer to the previous Context, which roots from the default Context. If you replace Context (38 bytes) in Block and BranchStub with a Box of cons (8 bytes), it would save 30 bytes from 15296 blocks and 11836 branch stubs, which could give 0.81MB reduction at most (9% of all).
Background
I already tried deduplicating contexts, but the hash table used for deduplication ended up being large, even with shrink_to_fit. The reduction was too limited despite the complexity.
However, thanks to the patch, I was able to easily collect the distribution of duplicated contexts https://gist.github.com/k0kubun/c1a5854e011db90d7e6efd73a6eda3b3. Many duplicated contexts share the same local_types, temp_types, and temp_mapping, so we could save those 32 bytes if we optimize the data structure for it.
3. Replace CodePtr with write_pos
If we allow only up to 4GB in --yjit-exec-mem-size, Option<CodePtr> could be changed to Option<NonZeroU32> by storing a write_pos instead of a pointer. (well, write_pos needs to be interpreted as a 1-origin value then 😅)
4. Combine targets and shape in Branch
In Branch, we could combine targets and shape into a single enum. Each variant should have either Box<[BranchTarget; 2]> or Box<BranchTarget>. Not a lot of reduction, but it’d still save 8 byte for every case.
Notes
For each Vec, we might be able to save some space by eliminating the capacity if it's no longer modified, e.g using into_boxed_slice.
Activity
k0kubun commentedon Feb 9, 2023
An experiment for the second point ruby#7279
k0kubun commentedon Feb 16, 2023
I tried deduplicating
ContextwithHashMap<Context, Rc<Context>>again k0kubun@5f80d41, but again, it didn't give a significant difference (changed only 0.2%):before
after
live_context_size: The size used for storing a referenceRc<Context>, which is part ofBlockandBranchStublive_context_count: The number of context referencescontext_table_size: The estimated size of the deduplication HashMapcontext_table_count: The number of entries in the deduplication HashMapIn this change,
size_of::<Block>()is reduced from 176 to 144 (-32), andsize_of::<BranchStub>()is reduced from 58 to 26 (-26).Because
compiled_block_countcould count freed blocks as well, I preparedlive_block_countand it was 15,340. So there were 11682 branch stubs.32 * 15340 + 26 * 11682is794612. This is offset by the HashMap size442368andContexts referenced byRc, which would consume 6035 * 38 =229330. So it's supposed to give a122914decrease in memory usage, however, it only gave a17736decrease. Allocating individual 38 bytes on heap wouldn't be that storage-efficient, so the number is not that surprising.I think we could also try having a vector of unique Contexts per iseq and store an index to it instead of
Rc.maximecb commentedon Feb 16, 2023
Thank you for putting together this experiment.
If I understand correctly, before, there were
26,874context objects. With the table, there are6,035entries in the table. That's the number of deduplicated contexts. So there is about a 5x reduction in the number of contexts.Maybe the problem is that for
HashMap<Context, Rc<Context>>, there's a Context stored once in the key, and another one allocated on the heap for the value. Then, theHashMapalso has some storage overhead (~2x) because hash maps can't be too full, otherwise it creates too many collisions.Feel free to run that experiment if you want to. I think the potential issue there is that we would need to look for existing contexts in the hash map, which might scale poorly for larger ISEQs because it's effectively a linear O(n) search?
This is not an easy problem to solve hmm.
k0kubun commentedon Feb 16, 2023
Yeah, I figured out how to address this and was about to comment on that, actually. It was as easy as just changing it to
HashMap<Rc<Context>, Rc<Context>>, and everything seems to be working as intended.Updated numbers:
A
282248decrease seems like a good-enough number for this deduplication idea. I've just rebased my Packed Context branch from latest master and measured it again, andyjit_alloc_sizewas8,902,985, i.e. only a267216decrease.So the deduplication idea seems fairly efficient. The only concern on the deduplication idea is GC, but maybe it's fine to lazily sweep them on
code_gc(using Rc's strong count)?k0kubun commentedon Feb 17, 2023
It turns out that you could get a key from
HashSet(I thought you could only querycontains), so it's compatible with that idea (and a nicer construct for readability). Now it's a336052decrease.I think this is good enough for now. I'll try making a PR with this idea and Rc-based GC on
code_gc. (edit: ruby#7332)[-]YJIT: Save memory used by Rust[/-][+]YJIT: Profile and report memory used by Rust[/+][-]YJIT: Profile and report memory used by Rust[/-][+]YJIT: Profile and report memory used by YJIT Rust[/+]k0kubun commentedon Mar 1, 2023
We have open issues or pull requests for everything discussed in this issue, so I'll close this. I'll probably profile the same thing again after Alan mergers his Rc removal.
maximecb commentedon Aug 31, 2023
Alan and I rebased the yjit-dhat branch on master and viewed the profile for railsbench using the DHAT viewer: https://nnethercote.github.io/dh_view/dh_view.html
Filter option: at t-gmax (bytes)
The good news is that the yjit alloc size is smaller than previously, however the code size has increased to:
The blocks are fairly large objects still, so we might be able to do more to reduce their size, but if we could somehow reduce the number of blocks (or reduce code size?), that would probably have the biggest impact.
Code size may be larger on ARM64 than on x86 (to be verified).