b) pass any mildy sizable quantity of data by reference rather than through the stack--including largish scalars;Despite the plausibility of this, I find it makes very little difference (where XSubs are concerned) whether the data is passed by reference or via the stack.
Here's a demo:
use warnings;
use Benchmark;
use Inline C => Config =>
BUILD_NOISY => 1;
use Inline C => <<'EOC';
/* These subs taken from *
* "Extending and Embedding Perl" *
* by Jenness and Cozens */
double sum_as_ref(AV * avref) {
int i, len;
double sum = 0;
SV ** elem;
len = av_len(avref) + 1;
for(i = 0; i < len; i++) {
elem = av_fetch(avref, i, 0);
sum += (double)SvIV(*elem);
}
return sum;
}
double sum_as_list(SV * arg1, ...) {
int i, len;
double sum = 0;
SV * elem;
Inline_Stack_Vars;
len = Inline_Stack_Items;
for(i = 0; i < len; i++) {
elem = Inline_Stack_Item(i);
sum += (double)SvIV(elem);
}
return sum;
}
EOC
our @x = (1 .. 2e6);
our($list, $ref);
timethese (1, {
'list' => '$list = sum_as_list(@x);',
'ref' => '$ref = sum_as_ref(\@x);',
});
print "$list\n$ref\n";
And the result:
Benchmark: timing 1 iterations of list, ref...
list: -1 wallclock secs ( 0.14 usr + 0.00 sys = 0.14 CPU) @ 7
+.09/s (n=1
)
(warning: too few iterations for a reliable count)
ref: 0 wallclock secs ( 0.14 usr + 0.00 sys = 0.14 CPU) @ 7
+.09/s (n=1
)
(warning: too few iterations for a reliable count)
2000001000000
2000001000000
Cheers,
Rob