-
Notifications
You must be signed in to change notification settings - Fork 602
Compile anonymous subs as anoncode without srefgen. #20290
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
Compile anonymous subs as anoncode without srefgen. #20290
Conversation
There was a problem hiding this 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);
}
Can you expand more on this, please? I could see removing the |
ff447ad to
a17d22a
Compare
Was looking for consumer of that anoncode / anon post |
a17d22a to
904693f
Compare
Per PMs I’ve replaced the relevant sections of pp_refgen with croak()s. |
|
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 |
904693f to
0aa694b
Compare
Reverted, with relevant comments added. |
Sorry I’m confused.
Regarding my 2cts |
leonerd
left a comment
There was a problem hiding this 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.
c9bb532 to
6e56618
Compare
|
@leonerd Have I resolved your concerns? |
leonerd
left a comment
There was a problem hiding this 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.
|
This introduces some noise at test time: which should probably eliminated. |
eff3f5c to
d1e4098
Compare
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.
d1e4098 to
0d20891
Compare
|
@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.) |
|
On 9/16/22 16:49, Hugo van der Sanden wrote:
***@***.**** commented on this pull request.
------------------------------------------------------------------------
In pp.c <#20290 (comment)>:
> @@ -6986,10 +6994,14 @@ PP(pp_anonconst)
{
dSP;
dTOPss;
- SETs(sv_2mortal((SV *)newCONSTSUB(SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV
- ? CopSTASH(PL_curcop)
- : NULL,
- NULL, SvREFCNT_inc_simple_NN(sv))));
+
+ CV* constsub = newCONSTSUB(
+ SvTYPE(CopSTASH(PL_curcop))==SVt_PVHV ? CopSTASH(PL_curcop) : NULL,
+ NULL,
+ SvREFCNT_inc_simple_NN(sv)
+ );
+
+ SETs(refto( sv_2mortal((SV *)constsub) ));
(Ugh, hate that the email versions of these comments tend to quote extra
lines, making it appear to be a conversation about the |newCONSTSUB()|
call rather than |SETs()|.)
FWIW I have no objection to the extra spaces here - they're correctly
balanced, and make the line more readable by giving the eye extra
reference points to help it see how to parse the code. In the same way
in perl code I'd tend to write |$array[$index]| but |$array[
$subarray[$subindex] ]|.
It won't kill me if they are avoided in any specific case - it's really
a judgement call when they improve things and when they detract - but
I'd hate to see a style guide mandating against them.
+1
|
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.