Skip to content

Commit

Permalink
Provide optimized form of assigning Nil
Browse files Browse the repository at this point in the history
So that the spesh plugin can return that, instead of falling back on the
fully general case that cannot be inlined.
  • Loading branch information
jnthn committed Aug 19, 2019
1 parent e5d2933 commit c7056f2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/vm/moar/spesh-plugins.nqp
Expand Up @@ -305,6 +305,11 @@ sub assign-fallback($cont, $value) {
sub assign-scalar-no-whence-no-typecheck($cont, $value) {
nqp::bindattr($cont, Scalar, '$!value', $value);
}
sub assign-scalar-nil-no-whence($cont, $value) {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
nqp::bindattr($cont, Scalar, '$!value',
nqp::getattr($desc, ContainerDescriptor, '$!default'))
}
sub assign-scalar-no-whence($cont, $value) {
my $desc := nqp::getattr($cont, Scalar, '$!descriptor');
my $type := nqp::getattr($desc, ContainerDescriptor, '$!of');
Expand Down Expand Up @@ -357,13 +362,20 @@ nqp::speshreg('perl6', 'assign', sub ($cont, $value) {
my $desc := nqp::speshguardgetattr($cont, Scalar, '$!descriptor');
if nqp::eqaddr($desc.WHAT, ContainerDescriptor) && nqp::isconcrete($desc) {
# Simple assignment, no whence. But is Nil being assigned?
my $of := $desc.of;
if nqp::eqaddr($value, Nil) {
# Yes; NYI.
# Yes; just copy in the default, provided we've a simple type.
if $of.HOW.archetypes.nominal {
nqp::speshguardtype($desc, $desc.WHAT);
nqp::speshguardconcrete($desc);
my $of := nqp::speshguardgetattr($desc, ContainerDescriptor, '$!of');
nqp::speshguardobj($of);
return &assign-scalar-nil-no-whence;
}
}
else {
# No whence, no Nil. Is it a nominal type? If yes, we can check
# it here.
my $of := $desc.of;
unless $of.HOW.archetypes.nominal {
nqp::speshguardobj($desc);
return &assign-scalar-no-whence;
Expand Down

3 comments on commit c7056f2

@salortiz
Copy link
Contributor

Choose a reason for hiding this comment

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

@jnthn
This commit broke a test in DBIish.
Coincidentally I was removing a 'diag' in it and travis CI detect the problem.
Reverting my build to the previous one (e5d2933) the test succeeds.
The path broken seem to be https://github.com/perl6/DBIish/blob/master/lib/DBDish/ErrorHandling.pm6#L26

@salortiz
Copy link
Contributor

Choose a reason for hiding this comment

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

The case golfed:

$ perl6 -v
This is Rakudo version 2019.07.1-174-ge5d2933b3 built on MoarVM version 2019.07.1-72-g352ae27e4
$ perl6 -e 'class F { has Exception $!e; method set($a){$!e=$a}; method check() {say so $!e}}; my $f = F.new(); $f.set(Nil);  $f.set(X::AdHoc.new()); $f.check()'
True

vs

$ perl6 -v
This is Rakudo version 2019.07.1-176-g207b825ea built on MoarVM version 2019.07.1-72-g352ae27e4
implementing Perl 6.d.
$ perl6 -e 'class F { has Exception $!e; method set($a){$!e=$a}; method check() {say so $!e}}; my $f = F.new(); $f.set(Nil);  $f.set(X::AdHoc.new()); $f.check()'
False

Seems that with your commit, the second assignment to $!e, (after setting to Nil) isn't preserved.

@jnthn
Copy link
Member Author

@jnthn jnthn commented on c7056f2 Aug 19, 2019

Choose a reason for hiding this comment

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

@salortiz Excellent golf (and will make a nice spectest!) I suspect a missing guard; I'll look at it in the morning. :)

Please sign in to comment.