Skip to content

Commit

Permalink
Move pp_entersub DIE() code into S_croak_undefined_subroutine
Browse files Browse the repository at this point in the history
In `pp_entersub`, when trying to find the subroutine `CV` to execute,
there are three nearby code paths that can result in a `DIE()`. This
commit extracts the DIE() logic to a helper subroutine,
`S_croak_undefined_subroutine`. This is partly in anticipation of
additional warning logic, but also generally reduces the size of this
hot function.
  • Loading branch information
richardleach committed Jan 10, 2025
1 parent df4d5e8 commit 82669cd
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions pp_hot.c
Original file line number Diff line number Diff line change
Expand Up @@ -6212,6 +6212,26 @@ Perl_clear_defarray(pTHX_ AV* av, bool abandon)
}
}

/* S_croak_undefined_subroutine is a helper function for pp_entersub.
* It takes assorted DIE() logic out of that hot function.
*/
static void
S_croak_undefined_subroutine(pTHX_ CV const *cv, GV const *gv)
{
if (cv) {
if (CvLEXICAL(cv) && CvHASGV(cv))
croak("Undefined subroutine &%" SVf " called",
SVfARG(cv_name((CV*)cv, NULL, 0)));
else /* pp_entersub triggers when (CvANON(cv) || !CvHASGV(cv)) */
croak("Undefined subroutine called");
} else { /* pp_entersub triggers when (!cv) after `try_autoload` */
SV *sub_name = newSV_type_mortal(SVt_PV);
gv_efullname3(sub_name, gv, NULL);

croak("Undefined subroutine &%" SVf " called", SVfARG(sub_name));
}
NOT_REACHED; /* NOTREACHED */
}

PP(pp_entersub)
{
Expand Down Expand Up @@ -6306,15 +6326,12 @@ PP(pp_entersub)
assert((void*)&CvROOT(cv) == (void*)&CvXSUB(cv));
while (UNLIKELY(!CvROOT(cv))) {
GV* autogv;
SV* sub_name;

/* anonymous or undef'd function leaves us no recourse */
if (CvLEXICAL(cv) && CvHASGV(cv))
DIE(aTHX_ "Undefined subroutine &%" SVf " called",
SVfARG(cv_name(cv, NULL, 0)));
if (CvANON(cv) || !CvHASGV(cv)) {
DIE(aTHX_ "Undefined subroutine called");
}
S_croak_undefined_subroutine(aTHX_ cv, NULL);
if (CvANON(cv) || !CvHASGV(cv))
S_croak_undefined_subroutine(aTHX_ cv, NULL);

/* autoloaded stub? */
if (cv != GvCV(gv = CvGV(cv))) {
Expand All @@ -6330,11 +6347,8 @@ PP(pp_entersub)
: 0));
cv = autogv ? GvCV(autogv) : NULL;
}
if (!cv) {
sub_name = sv_newmortal();
gv_efullname3(sub_name, gv, NULL);
DIE(aTHX_ "Undefined subroutine &%" SVf " called", SVfARG(sub_name));
}
if (!cv)
S_croak_undefined_subroutine(aTHX_ NULL, gv);
}

/* unrolled "CvCLONE(cv) && ! CvCLONED(cv)" */
Expand Down

0 comments on commit 82669cd

Please sign in to comment.