lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject lucy-clownfish git commit: Optimize XSBind_cfish_obj_to_sv_noinc
Date Thu, 26 Nov 2015 18:38:44 GMT
Repository: lucy-clownfish
Updated Branches:
  refs/heads/master 93d02b036 -> b51cc4684


Optimize XSBind_cfish_obj_to_sv_noinc

Also rename XSBind_cfish_obj_to_sv to XSBind_cfish_obj_to_sv_inc.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/b51cc468
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/b51cc468
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/b51cc468

Branch: refs/heads/master
Commit: b51cc468439756c14f9e130ddfc3492591ae0f69
Parents: 93d02b0
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Thu Nov 26 19:23:44 2015 +0100
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Thu Nov 26 19:23:44 2015 +0100

----------------------------------------------------------------------
 compiler/src/CFCPerlConstructor.c               |  9 +----
 .../perl/buildlib/Clownfish/Build/Binding.pm    |  6 ++--
 runtime/perl/xs/XSBind.c                        | 38 +++++++++++++++-----
 runtime/perl/xs/XSBind.h                        | 36 +++++++------------
 4 files changed, 47 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b51cc468/compiler/src/CFCPerlConstructor.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlConstructor.c b/compiler/src/CFCPerlConstructor.c
index 1d235ca..bde723b 100644
--- a/compiler/src/CFCPerlConstructor.c
+++ b/compiler/src/CFCPerlConstructor.c
@@ -160,14 +160,7 @@ CFCPerlConstructor_xsub_def(CFCPerlConstructor *self, CFCClass *klass)
{
         "    arg_%s = (%s)XSBind_new_blank_obj(aTHX_ ST(0));%s\n"
         "\n"
         "    retval = %s(%s);\n"
-        "    if (retval) {\n"
-        "        ST(0) = CFISH_OBJ_TO_SV(retval);\n"
-        "        CFISH_DECREF_NN(retval);\n"
-        "    }\n"
-        "    else {\n"
-        "        ST(0) = newSV(0);\n"
-        "    }\n"
-        "    sv_2mortal(ST(0));\n"
+        "    ST(0) = sv_2mortal(CFISH_OBJ_TO_SV_NOINC(retval));\n"
         "    XSRETURN(1);\n"
         "}\n\n";
     char *xsub_def

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b51cc468/runtime/perl/buildlib/Clownfish/Build/Binding.pm
----------------------------------------------------------------------
diff --git a/runtime/perl/buildlib/Clownfish/Build/Binding.pm b/runtime/perl/buildlib/Clownfish/Build/Binding.pm
index 068a29e..a50af01 100644
--- a/runtime/perl/buildlib/Clownfish/Build/Binding.pm
+++ b/runtime/perl/buildlib/Clownfish/Build/Binding.pm
@@ -200,7 +200,7 @@ singleton(either_sv, value)
     bool     value;
 CODE:
 {
-    RETVAL = CFISH_OBJ_TO_SV(cfish_Bool_singleton(value));
+    RETVAL = CFISH_OBJ_TO_SV_INC(cfish_Bool_singleton(value));
 }
 OUTPUT: RETVAL
 END_XS_CODE
@@ -506,7 +506,7 @@ fetch_obj(self, key)
     cfish_Hash *self;
     cfish_String *key;
 CODE:
-    RETVAL = CFISH_OBJ_TO_SV(CFISH_Hash_Fetch_IMP(self, key));
+    RETVAL = CFISH_OBJ_TO_SV_INC(CFISH_Hash_Fetch_IMP(self, key));
 OUTPUT: RETVAL
 
 void
@@ -895,7 +895,7 @@ fetch_obj(self, tick)
     cfish_Vector *self;
     uint32_t     tick;
 CODE:
-    RETVAL = CFISH_OBJ_TO_SV(CFISH_Vec_Fetch(self, tick));
+    RETVAL = CFISH_OBJ_TO_SV_INC(CFISH_Vec_Fetch(self, tick));
 OUTPUT: RETVAL
 END_XS_CODE
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b51cc468/runtime/perl/xs/XSBind.c
----------------------------------------------------------------------
diff --git a/runtime/perl/xs/XSBind.c b/runtime/perl/xs/XSBind.c
index a03f5f7..c89f669 100644
--- a/runtime/perl/xs/XSBind.c
+++ b/runtime/perl/xs/XSBind.c
@@ -176,9 +176,7 @@ S_maybe_perl_to_cfish(pTHX_ SV *sv, cfish_Class *klass, bool increment,
                 // Mortalize the converted object -- which is somewhat
                 // dangerous, but is the only way to avoid requiring that the
                 // caller take responsibility for a refcount.
-                SV *mortal = XSBind_cfish_obj_to_sv(aTHX_ obj);
-                CFISH_DECREF(obj);
-                sv_2mortal(mortal);
+                sv_2mortal(XSBind_cfish_obj_to_sv_noinc(aTHX_ obj));
             }
 
             *obj_ptr = obj;
@@ -426,9 +424,9 @@ SI_is_string_type(cfish_Class *klass) {
     return false;
 }
 
-// Returns an incref'd, blessed RV.
+// Returns a blessed RV.
 static SV*
-S_lazy_init_host_obj(pTHX_ cfish_Obj *self) {
+S_lazy_init_host_obj(pTHX_ cfish_Obj *self, bool increment) {
     cfish_Class  *klass      = self->klass;
     cfish_String *class_name = CFISH_Class_Get_Name(klass);
 
@@ -441,6 +439,7 @@ S_lazy_init_host_obj(pTHX_ cfish_Obj *self) {
      * will assume responsibility for maintaining the refcount. */
     cfish_ref_t old_ref = self->ref;
     size_t excess = old_ref.count >> XSBIND_REFCOUNT_SHIFT;
+    if (!increment) { excess -= 1; }
     SvREFCNT(inner_obj) += excess;
 
     // Overwrite refcount with host object.
@@ -550,12 +549,12 @@ cfish_dec_refcount(void *vself) {
 }
 
 SV*
-XSBind_cfish_obj_to_sv(pTHX_ cfish_Obj *obj) {
+XSBind_cfish_obj_to_sv_inc(pTHX_ cfish_Obj *obj) {
     if (obj == NULL) { return newSV(0); }
 
     SV *perl_obj;
     if (obj->ref.count & XSBIND_REFCOUNT_FLAG) {
-        perl_obj = S_lazy_init_host_obj(aTHX_ obj);
+        perl_obj = S_lazy_init_host_obj(aTHX_ obj, true);
     }
     else {
         perl_obj = newRV_inc((SV*)obj->ref.host_obj);
@@ -572,10 +571,33 @@ XSBind_cfish_obj_to_sv(pTHX_ cfish_Obj *obj) {
     return perl_obj;
 }
 
+SV*
+XSBind_cfish_obj_to_sv_noinc(pTHX_ cfish_Obj *obj) {
+    if (obj == NULL) { return newSV(0); }
+
+    SV *perl_obj;
+    if (obj->ref.count & XSBIND_REFCOUNT_FLAG) {
+        perl_obj = S_lazy_init_host_obj(aTHX_ obj, false);
+    }
+    else {
+        perl_obj = newRV_noinc((SV*)obj->ref.host_obj);
+    }
+
+    // Enable overloading for Perl 5.8.x
+#if PERL_VERSION <= 8
+    HV *stash = SvSTASH((SV*)obj->ref.host_obj);
+    if (Gv_AMG(stash)) {
+        SvAMAGIC_on(perl_obj);
+    }
+#endif
+
+    return perl_obj;
+}
+
 void*
 CFISH_Obj_To_Host_IMP(cfish_Obj *self) {
     dTHX;
-    return XSBind_cfish_obj_to_sv(aTHX_ self);
+    return XSBind_cfish_obj_to_sv_inc(aTHX_ self);
 }
 
 /*************************** Clownfish::Class ******************************/

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b51cc468/runtime/perl/xs/XSBind.h
----------------------------------------------------------------------
diff --git a/runtime/perl/xs/XSBind.h b/runtime/perl/xs/XSBind.h
index 40c98c7..0927382 100644
--- a/runtime/perl/xs/XSBind.h
+++ b/runtime/perl/xs/XSBind.h
@@ -77,37 +77,27 @@ cfish_XSBind_sv_defined(pTHX_ SV *sv) {
 CFISH_VISIBLE bool
 cfish_XSBind_sv_true(pTHX_ SV *sv);
 
-/** Derive an SV from a Clownfish object.  If the Clownfish object is NULL, the SV
- * will be undef.  Doesn't invoke To_Host and always returns a reference to a
- * Clownfish::Obj.
+/** Derive an SV from a Clownfish object.  If the Clownfish object is NULL,
+ * the SV will be undef.  Doesn't invoke To_Host and always returns a
+ * reference to a Clownfish::Obj.
  *
  * The new SV has single refcount for which the caller must take
  * responsibility.
  */
 CFISH_VISIBLE SV*
-cfish_XSBind_cfish_obj_to_sv(pTHX_ cfish_Obj *obj);
+cfish_XSBind_cfish_obj_to_sv_inc(pTHX_ cfish_Obj *obj);
 
-/** XSBind_cfish_obj_to_sv, with a cast.
+/** XSBind_cfish_obj_to_sv_inc, with a cast.
  */
-#define CFISH_OBJ_TO_SV(_obj) \
-    cfish_XSBind_cfish_obj_to_sv(aTHX_ (cfish_Obj*)_obj)
+#define CFISH_OBJ_TO_SV_INC(_obj) \
+    cfish_XSBind_cfish_obj_to_sv_inc(aTHX_ (cfish_Obj*)_obj)
 
-/** As XSBind_cfish_obj_to_sv above, except decrements the object's refcount
- * after creating the SV. This is useful when the Clownfish expression creates a new
- * refcount, e.g.  a call to a constructor.
+/** As XSBind_cfish_obj_to_sv_inc above, except decrements the object's
+ * refcount after creating the SV. This is useful when the Clownfish
+ * expression creates a new refcount, e.g.  a call to a constructor.
  */
-static CFISH_INLINE SV*
-cfish_XSBind_cfish_obj_to_sv_noinc(pTHX_ cfish_Obj *obj) {
-    SV *retval;
-    if (obj) {
-        retval = cfish_XSBind_cfish_obj_to_sv(aTHX_ obj);
-        CFISH_DECREF_NN(obj);
-    }
-    else {
-        retval = newSV(0);
-    }
-    return retval;
-}
+CFISH_VISIBLE SV*
+cfish_XSBind_cfish_obj_to_sv_noinc(pTHX_ cfish_Obj *obj);
 
 /** XSBind_cfish_obj_to_sv_noinc, with a cast.
  */
@@ -215,7 +205,7 @@ cfish_XSBind_arg_to_cfish(pTHX_ SV *value, const char *label, bool nullable,
 #define XSBind_foster_obj              cfish_XSBind_foster_obj
 #define XSBind_sv_defined              cfish_XSBind_sv_defined
 #define XSBind_sv_true                 cfish_XSBind_sv_true
-#define XSBind_cfish_obj_to_sv         cfish_XSBind_cfish_obj_to_sv
+#define XSBind_cfish_obj_to_sv_inc     cfish_XSBind_cfish_obj_to_sv_inc
 #define XSBind_cfish_obj_to_sv_noinc   cfish_XSBind_cfish_obj_to_sv_noinc
 #define XSBind_cfish_to_perl           cfish_XSBind_cfish_to_perl
 #define XSBind_perl_to_cfish           cfish_XSBind_perl_to_cfish


Mime
View raw message