Skip to content

Conversation

@FGasper
Copy link
Contributor

@FGasper FGasper commented Sep 12, 2022

Heretofore the compiler represented an anonymous subroutine as an anoncode op then an srefgen op; the latter’s only purpose was to return a reference to the anoncode’s returned CV.

This changeset slightly optimizes that by making pp_anoncode return a reference if so bidden. The same optimization is applied to pp_anonconst as well.

Hat-tip to @leonerd for the suggestion and guidance.

@FGasper FGasper requested review from atoomic and leonerd September 12, 2022 13:03
Copy link
Member

@atoomic atoomic left a comment

Choose a reason for hiding this comment

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

I've the feeling that the B::Deparse::pp_refgen also needs to be adjusted as part of your change
for example: $anoncode->first->first->sibling

 sub pp_refgen {
     my $self = shift;
     my($op, $cx) = @_;
     my $kid = $op->first;
     if ($kid->name eq "null") {
     my $anoncode = $kid = $kid->first;
     if ($anoncode->name eq "anonconst") {
         $anoncode = $anoncode->first->first->sibling;
     }
     if ($anoncode->name eq "anoncode"
      or !null($anoncode = $kid->sibling) and
          $anoncode->name eq "anoncode") {
             return $self->e_anoncode({ code => $self->padval($anoncode->targ) });
     } elsif ($kid->name eq "pushmark") {
             my $sib_name = $kid->sibling->name;
             if ($sib_name eq 'entersub') {
                 my $text = $self->deparse($kid->sibling, 1);
                 # Always show parens for \(&func()), but only with -p otherwise
                 $text = "($text)" if $self->{'parens'}
                                  or $kid->sibling->private & OPpENTERSUB_AMPER;
                 return "\\$text";
             }
         }
     }
     local $self->{'in_refgen'} = 1;
     $self->pfixop($op, $cx, "\\", 20);
 }

@FGasper
Copy link
Contributor Author

FGasper commented Sep 12, 2022

I've the feeling that the B::Deparse::pp_refgen also needs to be adjusted as part of your change

Can you expand more on this, please?

I could see removing the anoncode and anonconst sections from there since I think those ops will no longer happen in tandem with srefgen. That would just be pruning, though; as far as I can tell (and as the test suite shows), no change there seems necessary for Perl to work.

@FGasper FGasper force-pushed the felipe_anoncode_eliminate_extra_srefgen branch from ff447ad to a17d22a Compare September 12, 2022 15:43
@atoomic
Copy link
Member

atoomic commented Sep 12, 2022

I've the feeling that the B::Deparse::pp_refgen also needs to be adjusted as part of your change

Can you expand more on this, please?

I could see removing the anoncode and anonconst sections from there since I think those ops will no longer happen in tandem with srefgen. That would just be pruning, though; as far as I can tell (and as the test suite shows), no change there seems necessary for Perl to work.

Was looking for consumer of that anoncode / anon post
It looks like that this is now dead code with your change
as a refgen is not holding one anoncode anymore

-					     ->sibling #       srefgen
- 					     ->first   #         ex-list
- 					     ->first   #           anoncode

@FGasper FGasper force-pushed the felipe_anoncode_eliminate_extra_srefgen branch from a17d22a to 904693f Compare September 12, 2022 16:22
@FGasper
Copy link
Contributor Author

FGasper commented Sep 12, 2022

It looks like that this is now dead code with your change
as a refgen is not holding one anoncode anymore

Per PMs I’ve replaced the relevant sections of pp_refgen with croak()s.

@leonerd
Copy link
Contributor

leonerd commented Sep 12, 2022

You probably want to leave B::Deparse's code in place for deparsing the old optree shapes (give or take some additional comments explaining why), because XS modules that generate optrees may still output that shape for some time to come, until they adopt the new ability. It'd be nice not to break them unnecessarily

@FGasper FGasper force-pushed the felipe_anoncode_eliminate_extra_srefgen branch from 904693f to 0aa694b Compare September 12, 2022 20:16
@FGasper
Copy link
Contributor Author

FGasper commented Sep 12, 2022

XS modules that generate optrees may still output that shape for some time to come, until they adopt the new ability. It'd be nice not to break them unnecessarily

Reverted, with relevant comments added.

@FGasper FGasper requested a review from atoomic September 13, 2022 00:17
@atoomic
Copy link
Member

atoomic commented Sep 13, 2022

You probably want to leave B::Deparse's code in place for deparsing the old optree shapes (give or take some additional comments explaining why), because XS modules that generate optrees may still output that shape for some time to come, until they adopt the new ability. It'd be nice not to break them unnecessarily

Sorry I’m confused.

anonconst is experimental and I think the the main advantage of such features is to avoid the deprecation cycle.
So, if we cannot remove that code from B::Deparse as part of this Pull Request.
When will be a good time to remove it?

Regarding anoncode which is not experimental I understand the argument about XS.
You are saying we cannot remove it now, but not sure there would be a better timing in the future too.
Wouldn’t it be hard to audit existing XS packages building optree the previous way?
Adding a comment (as already done) explaining that Perl no longer generates such code seems a good idea.

my 2cts

Copy link
Contributor

@leonerd leonerd left a comment

Choose a reason for hiding this comment

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

Overall the pp_anoncode looks good, but I would apply the same OPf_REF flag to the pp_anonconst as well. Otherwise, what you've done here is changed the shape of how it works without adding the flag; this might have more of a knock-on effect on XS modules that build such optrees themselves. The point of using the flag on OP_ANONCODE was to shield them from that - the same surely should apply to OP_ANONCONST.

@FGasper FGasper force-pushed the felipe_anoncode_eliminate_extra_srefgen branch from c9bb532 to 6e56618 Compare September 20, 2022 18:39
@FGasper
Copy link
Contributor Author

FGasper commented Oct 4, 2022

@leonerd Have I resolved your concerns?

Copy link
Contributor

@leonerd leonerd left a comment

Choose a reason for hiding this comment

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

Yeah, looks good now.

@tonycoz
Copy link
Contributor

tonycoz commented Oct 6, 2022

This introduces some noise at test time:

../lib/B/Deparse-subclass.t .......................................... ok
===(  464165;213  14281/?  185/?  1/? )=================================:const is experimental at (eval 95) line 13, <DATA> chunk 60.
../ext/XS-APItest/t/utf8_warn02.t .................................... ok

which should probably eliminated.

@FGasper FGasper force-pushed the felipe_anoncode_eliminate_extra_srefgen branch 2 times, most recently from eff3f5c to d1e4098 Compare October 7, 2022 17:19
Heretofore the compiler represented an anonymous subroutine as an
anoncode op then an srefgen op; the latter’s only purpose was to
return a reference to the anoncode’s returned CV.

This changeset slightly optimizes that by making pp_anoncode return
a reference if so bidden. The same optimization is applied to
pp_anonconst as well.

Deparse.pm is updated accordingly.
@FGasper FGasper force-pushed the felipe_anoncode_eliminate_extra_srefgen branch from d1e4098 to 0d20891 Compare October 7, 2022 19:12
@FGasper
Copy link
Contributor Author

FGasper commented Oct 7, 2022

@tonycoz I’ve updated the branch to remove that warning.

(NB: The warning came from the test I added in the 1st commit, not from the production code changes in the 2nd commit.)

@tonycoz tonycoz merged commit 35458d3 into Perl:blead Oct 9, 2022
@FGasper FGasper deleted the felipe_anoncode_eliminate_extra_srefgen branch October 10, 2022 01:00
@khwilliamson
Copy link
Contributor

khwilliamson commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants