Allow lambdas that don't access self to be made shareable#12567
Open
tenderlove wants to merge 1 commit intoruby:masterfrom
Open
Allow lambdas that don't access self to be made shareable#12567tenderlove wants to merge 1 commit intoruby:masterfrom
tenderlove wants to merge 1 commit intoruby:masterfrom
Conversation
af32eb4 to
5b1d8af
Compare
This commit allows lambdas that don't access self to be "made shareable"
regardless of the shareability of self.
For example, the following code used to produce a lambda that was not
shareable:
```ruby
class Foo
def make_block; lambda { 1234 }; end
end
Ractor.make_shareable(Foo.new.make_block) # exception
```
This lambda was not allowed to be shareable because it could possibly
access `self` which is an unfrozen instance of `Foo`. However, we know
by looking at the code that it doesn't access the instance of `Foo`, so
I think we should lift this restriction.
Upon calling `make_shareable`, this change scans the instructions of the
block looking for particular instructions that access `self`. If it
sees any of those instructions, then we use the default behavior
(checking sharability of `self`). If we don't see those instructions,
then we'll allow the lambda to be shareable.
For example, this is shareable:
```ruby
def make_block
foo = 123
lambda { foo }
end
```
But these are not shareable:
```ruby
def make_block
lambda { @foo }
end
```
```ruby
def make_block
lambda { @foo = 1 }
end
```
```ruby
def make_block
lambda { eval("123") }
end
```
[Feature #21033]
5b1d8af to
fd9d359
Compare
nobu
reviewed
Jan 14, 2025
|
|
||
| // Returns a boolean indicating whether or not the iseq accesses self. | ||
| bool | ||
| rb_vm_iseq_reads_self(const rb_iseq_t *iseq) |
Member
There was a problem hiding this comment.
Doesn't this name look like a predicate very much?
Member
Author
There was a problem hiding this comment.
Yes, it does. Should I add a _p, or?
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 commit allows lambdas that don't access self to be "made shareable" regardless of the shareability of self.
For example, the following code used to produce a lambda that was not shareable:
This lambda was not allowed to be shareable because it could possibly access
selfwhich is an unfrozen instance ofFoo. However, we know by looking at the code that it doesn't access the instance ofFoo, so I think we should lift this restriction.Upon calling
make_shareable, this change scans the instructions of the block looking for particular instructions that accessself. If it sees any of those instructions, then we use the default behavior (checking sharability ofself). If we don't see those instructions, then we'll allow the lambda to be shareable.For example, this is shareable:
But these are not shareable:
[Feature #21033]