The following code performs far more work than it has to, mostly due to 
crossing the C/PCC boundary multiple times, as well as throwing away known 
information:

        $P0 = box 10
        $I0 = cmp $P0, 10

This:
        
    - calls VTABLE_cmp on $P1, reaching VTABLE_cmp in the Default PMC
        - calls Parrot_mmd_multi_dispatch_from_c_args
            - passing 'cmp', 'PP->I' signature, and args as varargs
            - builds sig object from varargs
                - loops through signature string
                    - creates a new CallSignature PMC
                    - creates a new return PMC for all return argument
                    - creates a new CPointer for each return argument
                    - pushes arguments onto the CallSignature PMC
                - builds a type tuple for MMD
                    - loops through signature stored in CallSignature to find
                    MMD-participant arguments
                    - loops through type signature to set argument types
            - checks MMD cache
                - use cached candidate if possible
                - find new candidate
                    - creates new array PMC for candidate list
                    - searches CallSignature's namespace for candidates (?)
                    - searches global MULTI namespace for candidates
                    - sorts candidate list by MMD type tuple
                        - loops through candidate list
                        - calculates distance to each candidate
                            - loops through each argument (parallel iteration
                            over type tuple and argument list)
                            - loops over all elements in MRO for each argument
                            type
            - calls Parrot_pcc_invoke_sub_from_sig_object
                - converts CallSignature string to C string
                - creates array PMCs for arguments and results
                - counts number of arguments and return values (looping over 
signature string)
                - sets up input parameters in current context
                    - loops over the C signature
                    - assigns each parameter to the appropriate context
                - invokes the Parrot sub (NCI)
                    - calls the NCI thunk (pcf_I_JPP)
                        - calls Parrot_init_arg_nci
                            - inits data structures
                                - calls Parrot_init_arg_indexes_and_sig_pmc
                                - calls Parrot_init_arg_sig
                        - calls C function
                        - calls set_nci_I to store return value
                            - converts argument to INTVAL if necessary
                            - stores argument into register
                - assigns return values from the context to the CallSignature
                    - loops over the C signature
                    - assigns each return value appropriately

The default Integer case performs a C-level <> comparison.  Most of this 
codepath is new as of the MMD branch merge.

Within the cmp op bodies, we *know* the arity and most of the types of MMD-
participant arguments at compile time.  We can get the types of PMC 
participants within the body of the op itself.  Thus we could avoid most of 
the argument marshalling and counting and analysis if we had a way to perform 
cached MMD lookup without constructing a CallSignature PMC.  That would clear 
up a third of the work.

Another area for optimization is invoking a Sub from a signature PMC; I 
believe we're throwing away and recalculating valuable information, though we 
may have to wait for dramatic improvements until we can unify contexts and 
CallSignature.

The final opportunity for optimization is making the PMC multis defined in 
PMCs use PCC instead of C calling conventions.  Corresponding multis written 
in PIR already use PCC, and we want to support that, so we should unify our 
approach.  That would remove the NCI expense here, though that's probably 
minor in comparison to the CallSignature PMC expense.

-- c


Reply via email to