[svn:parrot] r39268 - trunk/src/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Sat May 30 13:36:22 UTC 2009


Author: bacek
Date: Sat May 30 13:36:21 2009
New Revision: 39268
URL: https://trac.parrot.org/parrot/changeset/39268

Log:
[pmc] Reuse "dest" pmc in Integer arithmetics when possible.

This reducing GC pressure for basic Integer operations. primes2.pir now executed
30% faster for calculating 5000 primes.

Modified:
   trunk/src/pmc/integer.pmc

Modified: trunk/src/pmc/integer.pmc
==============================================================================
--- trunk/src/pmc/integer.pmc	Sat May 30 12:59:17 2009	(r39267)
+++ trunk/src/pmc/integer.pmc	Sat May 30 13:36:21 2009	(r39268)
@@ -43,6 +43,37 @@
     return self;
 }
 
+/* Try to reuse dest PMC if possible */
+static PMC*
+reuse_or_new(PARROT_INTERP, PMC *self, PMC * value, PMC *dest)
+{
+    /* Can't reuse dest because we'll lost value */
+    if (dest == value)
+        return pmc_new(interp, VTABLE_type(interp, self));
+
+    /* Can't reuse Null */
+    if (PMC_IS_NULL(dest))
+        return pmc_new(interp, VTABLE_type(interp, self));
+
+    /* Can't reuse read-only variables */
+    if (dest->vtable->flags & VTABLE_IS_READONLY_FLAG)
+        return pmc_new(interp, VTABLE_type(interp, self));
+
+    /* It's safe in this case. Caller want to replace us anyway */
+    if (dest == self)
+        return dest;
+
+    /* Morph to Integer is required */
+    {
+        INTVAL type = VTABLE_type(interp, self);
+        if (dest->vtable->base_type != type)
+            pmc_reuse(interp, dest, type, 0);
+    }
+
+    return dest;
+}
+
+
 pmclass Integer extends scalar provides integer provides scalar {
     ATTR INTVAL iv; /* the value of this Integer */
 
@@ -347,7 +378,7 @@
         const INTVAL c = a + b;
 
         if ((c^a) >= 0 || (c^b) >= 0) {
-            dest = pmc_new(INTERP, VTABLE_type(interp, SELF));
+            dest = reuse_or_new(INTERP, SELF, value, dest);
 
             /* need this for e.g. Undef PMC */
             VTABLE_set_integer_native(INTERP, dest, c);
@@ -364,7 +395,7 @@
 
     MULTI PMC *add(Complex value, PMC *dest) {
         const INTVAL a = SELF.get_integer();
-        dest           = pmc_new(INTERP, VTABLE_type(interp, value));
+        dest           = reuse_or_new(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest,
                 a + VTABLE_get_number_keyed_int(INTERP, value, 0));
@@ -384,7 +415,7 @@
 
 
     MULTI PMC *add(DEFAULT value, PMC *dest) {
-        dest = pmc_new(INTERP, VTABLE_type(interp, value));
+        dest = reuse_or_new(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest,
                 SELF.get_integer() + VTABLE_get_number(INTERP, value));
@@ -397,7 +428,7 @@
         const INTVAL c = a + b;
 
         if ((c^a) >= 0 || (c^b) >= 0) {
-            dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+            dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -486,7 +517,7 @@
         const INTVAL c = a - b;
 
         if ((c^a) >= 0 || (c^~b) >= 0) {
-            dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+            dest = reuse_or_new(INTERP, SELF, value, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -502,7 +533,7 @@
 
     MULTI PMC *subtract(Complex value, PMC *dest) {
         const INTVAL a = SELF.get_integer();
-        dest           = pmc_new(INTERP, VTABLE_type(INTERP, value));
+        dest           = reuse_or_new(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest,
                 a - VTABLE_get_number_keyed_int(INTERP, value, 0));
@@ -522,7 +553,7 @@
 
 
     MULTI PMC *subtract(DEFAULT value, PMC *dest) {
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, value));
+        dest = reuse_or_new(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest,
                 SELF.get_integer() - VTABLE_get_number(INTERP, value));
@@ -545,7 +576,7 @@
         const INTVAL c = a - b;
 
         if ((c^a) >= 0 || (c^~b) >= 0) {
-            dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+            dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -644,7 +675,7 @@
         const double cf = (double)a * (double)b;
 
         if ((double) c == cf) {
-            dest = pmc_new(INTERP, VTABLE_type(interp, SELF));
+            dest = reuse_or_new(INTERP, SELF, value, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -675,7 +706,7 @@
 
     MULTI PMC *multiply(DEFAULT value, PMC *dest) {
         const FLOATVAL valf = VTABLE_get_number(INTERP, value);
-        dest                = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest                = reuse_or_new(INTERP, SELF, value, dest);
 
         VTABLE_set_number_native(INTERP, dest, SELF.get_number() * valf);
         return dest;
@@ -688,7 +719,7 @@
         const double cf = (double)a * (double)b;
 
         if ((double) c == cf) {
-            dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+            dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
             VTABLE_set_integer_native(INTERP, dest, c);
             return dest;
@@ -793,7 +824,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, value, dest);
         VTABLE_set_number_native(INTERP, dest, SELF.get_number() / d);
         return dest;
     }
@@ -855,7 +886,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, value, dest);
 
         f = floor(SELF.get_number() / d);
         VTABLE_set_integer_native(INTERP, dest, (INTVAL)f);
@@ -870,7 +901,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
         f = floor(SELF.get_number() / value);
         VTABLE_set_integer_native(INTERP, dest, (INTVAL)f);
@@ -885,7 +916,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "float division by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
         f = floor(SELF.get_number() / value);
         VTABLE_set_integer_native(INTERP, dest, (INTVAL)f);
@@ -976,7 +1007,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "int modulus by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, value, dest);
 
         VTABLE_set_integer_native(INTERP, dest,
                 intval_mod(SELF.get_integer(), d));
@@ -989,7 +1020,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "int modulus by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest,
                 intval_mod(SELF.get_integer(), value));
@@ -1002,7 +1033,7 @@
             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_DIV_BY_ZERO,
                     "int modulus by zero");
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest,
                 intval_mod(SELF.get_integer(), (INTVAL)value));
@@ -1121,7 +1152,7 @@
             }
         }
 
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest, r);
         return dest;
@@ -1308,7 +1339,7 @@
         const INTVAL a = abs(SELF.get_integer());
 
         /* RT #46635 overflow for -maxint */
-        dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+        dest = reuse_or_new(INTERP, SELF, NULL, dest);
 
         VTABLE_set_integer_native(INTERP, dest, a);
         return dest;


More information about the parrot-commits mailing list