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: Compress BranchGenFn and BranchShape #7401

Merged
merged 3 commits into from Feb 28, 2023

Conversation

k0kubun
Copy link
Member

@k0kubun k0kubun commented Feb 28, 2023

We have only a few variants of BranchGenFn. Using this property, we could use an enum to represent each function and set it instead of a function pointer, which is the core idea of this PR.

I also made another change, making BranchShape part of the BranchGenFn enum, which I believe contributes to its readability since it's made obvious that some functions don't use BranchShapes other than Default. (I hoped it would make an enum of an enum 1 byte like #7368, but this time it didn't seem to work. But I want to keep this change anyway)

This reduces yjit_alloc_size by 0.28MB (3.1%) on 25 itrs of railsbench with arm64-darwin22 stats build.

Before

yjit_alloc_size:           9,026,109

After

yjit_alloc_size:           8,747,009

@matzbot matzbot requested a review from a team February 28, 2023 05:25
Copy link
Member

@XrXr XrXr left a comment

Choose a reason for hiding this comment

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

I love the idea here. I think it also allows us to derive Debug on Branch now because the function pointer is gone. But, I think this conflicts with the raw pointer change I'm trying a bit. In particular, the gen_fun never changes for a branch, but its shape can, so in my diff I have { gen_fun: Fun, shape: Cell<BranchShape>, .. } to show this. I mean, it's not a huge problem and I can just make a new copy with the new branch shape and just have Cell<Fun>. I think I'd prefer that the mutability of the shape is in contrast though. I think both representation should give the Branch struct the same layout in the end (it recursively pulls out and aggregate enum variant bits at the top level). Change this if you want, it's not a big deal.

EDIT: actually, having the enum reflect the fact that some gen funs should/can only have the default shape (it's implicit), the representation here is probably better than being able to see the mutability of shape for some of the variants.

@jimmyhmiller
Copy link
Contributor

Just wanted to say I love this change. Makes the code much more readable. Thanks for the great change :)

@k0kubun
Copy link
Member Author

k0kubun commented Feb 28, 2023

I think it also allows us to derive Debug on Branch now because the function pointer is gone.

Nice. I did so in 6b9dda5.

having the enum reflect the fact that some gen funs should/can only have the default shape (it's implicit), the representation here is probably better than being able to see the mutability of shape for some of the variants.

Yeah, I want to keep that property.

I can just make a new copy with the new branch shape and just have Cell

I wonder if you can make it BranchGenFn<Cell<BranchShape>> for some variants. If Rust doesn't allow that, then maybe I would live with Cell<BranchGenFn> in your branch, preferring the benefit of having the constraint between BranchGenFn and BranchShape.

@XrXr
Copy link
Member

XrXr commented Feb 28, 2023

By the way, the fact that we couldn't derive(Debug) when there was a function pointer seems to be some gnarly higher rank type bound issue WRT lifetime of references in the function pointer parameters: rust-lang/rust#54508 rust-lang/rust#46989

yjit/src/core.rs Outdated Show resolved Hide resolved
yjit/src/core.rs Outdated Show resolved Hide resolved
yjit/src/core.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@maximecb maximecb left a comment

Choose a reason for hiding this comment

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

Really minor comments, othewise ready to merge.

Co-authored-by: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@shopify.com>
@k0kubun k0kubun merged commit 966adfb into ruby:master Feb 28, 2023
@k0kubun k0kubun deleted the yjit-branch-shapes branch February 28, 2023 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants