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

chromatic at svn.parrot.org chromatic at svn.parrot.org
Sun Aug 16 07:49:55 UTC 2009


Author: chromatic
Date: Sun Aug 16 07:49:55 2009
New Revision: 40587
URL: https://trac.parrot.org/parrot/changeset/40587

Log:
[PMC] Optimized NCI registration signature handling slightly by avoiding
malloc/free in almost every case -- a static char buffer sufficies for almost
every signature -- and by reusing a calculated key length rather than
recalculating it.  Parrot startup is now 0.55% faster.

Modified:
   trunk/src/pmc/nci.pmc

Modified: trunk/src/pmc/nci.pmc
==============================================================================
--- trunk/src/pmc/nci.pmc	Sun Aug 16 07:21:58 2009	(r40586)
+++ trunk/src/pmc/nci.pmc	Sun Aug 16 07:49:55 2009	(r40587)
@@ -21,11 +21,13 @@
 typedef INTVAL (*nci_sub_t)(PARROT_INTERP, PMC *);
 typedef INTVAL (*nci_jit_sub_t)(PARROT_INTERP, PMC *, char *);
 
-void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const nci_info);
-void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const nci_info) {
-    size_t  sig_length = Parrot_str_byte_length(interp, sig);
-    char   *param_sig  = mem_allocate_n_typed(sig_length, char);
-    size_t  j          = 0;
+void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const nci_info, size_t sig_length);
+void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const nci_info, size_t sig_length) {
+    char    param_buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+    char   *param_sig    = sig_length <= 7
+                         ? param_buf
+                         : mem_allocate_n_typed(sig_length, char);
+    size_t  j            = 0;
     size_t  i;
 
     for (i = 1; i < sig_length; i++) {
@@ -73,6 +75,8 @@
                 param_sig[j++] = 'S';
                 break;
             default:
+                if (sig_length > 7)
+                    mem_sys_free(param_sig);
                 Parrot_ex_throw_from_c_args(interp, NULL,
                     EXCEPTION_JIT_ERROR,
                     "Unknown param Signature %c\n", (char)c);
@@ -90,7 +94,8 @@
     else
         nci_info->pcc_params_signature = CONST_STRING(interp, "");
 
-    mem_sys_free(param_sig);
+    if (sig_length > 7)
+        mem_sys_free(param_sig);
 }
 
 
@@ -176,8 +181,9 @@
     }
 
     VTABLE void set_pointer_keyed_str(STRING *key, void *func) {
-        Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
-        int                             jitted = 0;
+        Parrot_NCI_attributes * const nci_info   = PARROT_NCI(SELF);
+        int                           jitted     = 0;
+        size_t                        key_length = Parrot_str_byte_length(interp, key);
 
         /* Store the original function and signature. */
         SET_ATTR_orig_func(INTERP, SELF, func);
@@ -185,21 +191,21 @@
         /* ensure that the STRING signature is constant */
         if (!PObj_constant_TEST(key)) {
             char * const key_c = Parrot_str_to_cstring(INTERP, key);
-            key                = string_make(interp, key_c, strlen(key_c),
+            key                = string_make(interp, key_c, key_length,
                                     NULL, PObj_constant_FLAG);
             Parrot_str_free_cstring(key_c);
         }
 
         nci_info->signature  = key;
-        pcc_params(INTERP, key, nci_info);
+        pcc_params(INTERP, key, nci_info, key_length);
 
         /* Arity is length of that string minus one (the return type). */
-        nci_info->arity      = Parrot_str_byte_length(INTERP, key) - 1;
+        nci_info->arity      = key_length - 1;
 
         /* Build call function. */
         nci_info->func       = (PMC *)(build_call_func(INTERP, SELF,
                                             key, &jitted));
-        nci_info->jitted = jitted;
+        nci_info->jitted     = jitted;
     }
 
 /*


More information about the parrot-commits mailing list