Skip to content

Commit

Permalink
[PMC] Optimized NCI registration signature handling slightly by avoiding
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://svn.parrot.org/parrot/trunk@40587 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
chromatic committed Aug 16, 2009
1 parent b7f6fa9 commit 1764f4c
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/pmc/nci.pmc
Expand Up @@ -21,11 +21,13 @@ The vtable functions for the native C call functions.
typedef INTVAL (*nci_sub_t)(PARROT_INTERP, PMC *); typedef INTVAL (*nci_sub_t)(PARROT_INTERP, PMC *);
typedef INTVAL (*nci_jit_sub_t)(PARROT_INTERP, PMC *, char *); 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);
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) {
size_t sig_length = Parrot_str_byte_length(interp, sig); char param_buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
char *param_sig = mem_allocate_n_typed(sig_length, char); char *param_sig = sig_length <= 7
size_t j = 0; ? param_buf
: mem_allocate_n_typed(sig_length, char);
size_t j = 0;
size_t i; size_t i;


for (i = 1; i < sig_length; i++) { for (i = 1; i < sig_length; i++) {
Expand Down Expand Up @@ -73,6 +75,8 @@ void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const nci_in
param_sig[j++] = 'S'; param_sig[j++] = 'S';
break; break;
default: default:
if (sig_length > 7)
mem_sys_free(param_sig);
Parrot_ex_throw_from_c_args(interp, NULL, Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_JIT_ERROR, EXCEPTION_JIT_ERROR,
"Unknown param Signature %c\n", (char)c); "Unknown param Signature %c\n", (char)c);
Expand All @@ -90,7 +94,8 @@ void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const nci_in
else else
nci_info->pcc_params_signature = CONST_STRING(interp, ""); nci_info->pcc_params_signature = CONST_STRING(interp, "");


mem_sys_free(param_sig); if (sig_length > 7)
mem_sys_free(param_sig);
} }




Expand Down Expand Up @@ -176,30 +181,31 @@ Sets the specified function pointer and signature (C<*key>).
} }


VTABLE void set_pointer_keyed_str(STRING *key, void *func) { VTABLE void set_pointer_keyed_str(STRING *key, void *func) {
Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF); Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
int jitted = 0; int jitted = 0;
size_t key_length = Parrot_str_byte_length(interp, key);


/* Store the original function and signature. */ /* Store the original function and signature. */
SET_ATTR_orig_func(INTERP, SELF, func); SET_ATTR_orig_func(INTERP, SELF, func);


/* ensure that the STRING signature is constant */ /* ensure that the STRING signature is constant */
if (!PObj_constant_TEST(key)) { if (!PObj_constant_TEST(key)) {
char * const key_c = Parrot_str_to_cstring(INTERP, 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); NULL, PObj_constant_FLAG);
Parrot_str_free_cstring(key_c); Parrot_str_free_cstring(key_c);
} }


nci_info->signature = key; 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). */ /* 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. */ /* Build call function. */
nci_info->func = (PMC *)(build_call_func(INTERP, SELF, nci_info->func = (PMC *)(build_call_func(INTERP, SELF,
key, &jitted)); key, &jitted));
nci_info->jitted = jitted; nci_info->jitted = jitted;
} }


/* /*
Expand Down

0 comments on commit 1764f4c

Please sign in to comment.