Add CvGvNAME_HEK helper
authorNicolas R <atoomic@cpan.org>
Mon, 18 Sep 2017 20:37:48 +0000 (14:37 -0600)
committerFather Chrysostomos <sprout@cpan.org>
Thu, 21 Sep 2017 14:07:49 +0000 (07:07 -0700)
CvGvNAME_HEK can be used instead of the boilerplate:
CvNAMED(sv) ? CvNAME_HEK((CV *)sv) : GvNAME_HEK(CvGV(sv))

This is also saving an extra CvNAMED check from CvNAME_HEK.

cv.h
gv.c
scope.c

diff --git a/cv.h b/cv.h
index ebbdd36..dac83fa 100644 (file)
--- a/cv.h
+++ b/cv.h
@@ -234,7 +234,17 @@ CvNAME_HEK(CV *sv)
        ? ((XPVCV*)MUTABLE_PTR(SvANY(sv)))->xcv_gv_u.xcv_hek
        : 0;
 }
-/* This lowers the refernce count of the previous value, but does *not*
+
+/* helper for the common pattern:
+   CvNAMED(sv) ? CvNAME_HEK((CV *)sv) : GvNAME_HEK(CvGV(sv))
+*/
+#define CvGvNAME_HEK(sv) ( \
+        CvNAMED((CV*)sv) ? \
+            ((XPVCV*)MUTABLE_PTR(SvANY((SV*)sv)))->xcv_gv_u.xcv_hek\
+            : GvNAME_HEK(CvGV( (SV*) sv)) \
+        )
+
+/* This lowers the reference count of the previous value, but does *not*
    increment the reference count of the new value. */
 #define CvNAME_HEK_set(cv, hek) ( \
        CvNAME_HEK((CV *)(cv))                                           \
diff --git a/gv.c b/gv.c
index cfe4be5..6df78cc 100644 (file)
--- a/gv.c
+++ b/gv.c
@@ -2799,8 +2799,7 @@ Perl_Gv_AMupdate(pTHX_ HV *stash, bool destructing)
        gv = Perl_gv_fetchmeth_pvn(aTHX_ stash, cooky, l, -1, 0);
         cv = 0;
         if (gv && (cv = GvCV(gv)) && CvHASGV(cv)) {
-            const HEK * const gvhek =
-                CvNAMED(cv) ? CvNAME_HEK(cv) : GvNAME_HEK(CvGV(cv));
+            const HEK * const gvhek = CvGvNAME_HEK(cv);
             const HEK * const stashek =
                 HvNAME_HEK(CvNAMED(cv) ? CvSTASH(cv) : GvSTASH(CvGV(cv)));
             if (HEK_LEN(gvhek) == 3 && strEQ(HEK_KEY(gvhek), "nil")
diff --git a/scope.c b/scope.c
index 59cea3b..dfaab80 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -1194,10 +1194,7 @@ Perl_leave_scope(pTHX_ I32 base)
                         break;
                     case SVt_PVCV:
                     {
-                        HEK *hek =
-                             CvNAMED(sv)
-                               ? CvNAME_HEK((CV *)sv)
-                               : GvNAME_HEK(CvGV(sv));
+                        HEK *hek = CvGvNAME_HEK(sv);
                         assert(hek);
                         (void)share_hek_hek(hek);
                         cv_undef((CV *)sv);
@@ -1223,9 +1220,7 @@ Perl_leave_scope(pTHX_ I32 base)
                     case SVt_PVHV:     *svp = MUTABLE_SV(newHV());     break;
                     case SVt_PVCV:
                     {
-                        HEK * const hek = CvNAMED(sv)
-                                             ? CvNAME_HEK((CV *)sv)
-                                             : GvNAME_HEK(CvGV(sv));
+                        HEK * const hek = CvGvNAME_HEK(sv);
 
                         /* Create a stub */
                         *svp = newSV_type(SVt_PVCV);