perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

Compile anonymous subs as anoncode without srefgen.

Open FGasper opened this issue 3 years ago • 6 comments

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 avatar Sep 12 '22 13:09 FGasper

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 avatar Sep 12 '22 15:09 FGasper

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

atoomic avatar Sep 12 '22 16:09 atoomic

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.

FGasper avatar Sep 12 '22 20:09 FGasper

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

leonerd avatar Sep 12 '22 20:09 leonerd

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 avatar Sep 12 '22 20:09 FGasper

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

atoomic avatar Sep 13 '22 15:09 atoomic

@leonerd Have I resolved your concerns?

FGasper avatar Oct 04 '22 00:10 FGasper

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.

tonycoz avatar Oct 06 '22 04:10 tonycoz

@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.)

FGasper avatar Oct 07 '22 20:10 FGasper

On 9/16/22 16:49, Hugo van der Sanden wrote:

@.**** commented on this pull request.


In pp.c https://github.com/Perl/perl5/pull/20290#discussion_r973492756:

@@ -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

khwilliamson avatar Oct 11 '22 08:10 khwilliamson