Igor Sysoev wrote:
> 
>> Fairly, there is even more speedup hacks. For example, you can use
>> TARG-macros, if you allocate string with Newx() call.
> 
> Could you explain in detail ?

If you return exactly one scalar then efficient way is to use
TARG-macros. Every opcode and XSUB-routine has specially assigned SV,
which are created/freed only once. You can use it to place return value
on stack. Mega-optimized variant can be next:

MODULE = XXX
PROTOTYPES: DISABLE

void
myfunc(in)
   CODE:
   {
        dXSTARG;
        char *res;
        STRLEN len_res;

        // build *res ...

        sv_setpvn(TARG, res, len_res);  // string IS copied!
        ST(0) = TARG;   // fairly, we must call
                        // XSprePUSH; PUSHTARG;
                        // but it is not required for our simplest case

        // SvSETMAGIC(TARG); // Skipped, because we return only
                        //simple scalars without any magic
        XSRETURN(1);
   }

If you don't want use sv_setpvn() then you can use
SvPV_set/SvFAKE_on/... from my previous email.

TARG can not be used for multiple return values.

See "man perlguts":
"XSUBs and the Argument Stack"
"Putting a C value on Perl stack"
for futher reference.

> By the way, is it possible to avoid creating my mortal SV and create just
> perl SV ?

If you don't want to create new SV at all - you can modify passed
arguments in-place.

I.e. instead of:

my $src = "aaa";
my $dst = my_uppercase($src);
# invariant: $dst eq 'AAA'

you can use:

my $src = "aaa";
my_uppercase($src);
# invariant: $src eq 'AAA'

-- 
Sergey Skvortsov
mailto: [EMAIL PROTECTED]

Reply via email to