Skip to content
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

YJIT: implement cache for recently encoded/decoded contexts #10938

Merged
merged 2 commits into from
Jun 7, 2024

Conversation

maximecb
Copy link
Contributor

@maximecb maximecb commented Jun 7, 2024

The decoded_from and one-entry caches seemed slightly hacky, particularly given that decoded_from broke equality comparison on Context objects.

Since we know from Kokubun's previous work that there is significant duplication among contexts, I thought it could make sense to implement a fixed-size cache of recently encoded/decoded contexts. This turns out to work quite well.

master branch (with var-len ctx and single-entry cache):

yjit_alloc_size:          19,953,035
context_data_bytes:          649,671
num_contexts_encoded:        134,390
bytes_per_context:              4.83

After, cache size 128:

yjit_alloc_size:          19,585,604
context_data_bytes:          294,313
num_contexts_encoded:        134,268
bytes_per_context:              2.19

After, cache size 256:

yjit_alloc_size:          19,546,263
context_data_bytes:          264,494
num_contexts_encoded:        134,361
bytes_per_context:              1.97

After, cache size 512:

yjit_alloc_size:          19,550,879 # ~2% memory savings vs master
context_data_bytes:          239,697
num_contexts_encoded:        134,445
bytes_per_context:              1.78

So this is pretty cool. With minimal changes we can get some amount of deduplication of contexts. If we keep making the cache bigger, at some point, memory usage starts going up again because of the size of the cache itself. This seems to happen at cache size 512 for lobsters. That being said, I'd be inclined to keep a size of 512 because the bigger the app, the less the cache overhead matters.

@maximecb maximecb requested review from XrXr and k0kubun June 7, 2024 21:18
@matzbot matzbot requested a review from a team June 7, 2024 21:18
Copy link
Member

@k0kubun k0kubun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@maximecb maximecb enabled auto-merge (squash) June 7, 2024 21:25
@maximecb maximecb merged commit 0d91887 into ruby:master Jun 7, 2024
101 checks passed
@maximecb maximecb deleted the yjit_ctx_cache branch June 7, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants