in reply to How to avoid string copies in function calls?

Your tests are bad. The only copying is happening outside of the functions you are testing. Specifically, they occurring when you assign the string to $tmp in $tmp = out1() and in $tmp = fun1($str).

Right now, you have

my $str = out1(); my $ref = out2(); my $str = fun1($str); my $ref = fun2(\$str);

If you wanted to fix the test, you'd use

my $str = out1(); my $str = ${ out2() }; my $str = fun1($str); my $str = ${ fun2(\$str) };

or

my $ref = \out1(); my $ref = out2(); my $ref = \fun1($str); my $ref = fun2(\$str);

As you can see, passing a string by reference seems to slow down (in2 is slower than in1)

Perl always passes by reference. in2 passes *a* reference (by reference). This reference has to be created, dereferenced and destroyed. This is all work that in2 does on top of what in1 does, so of course it will be slower.

Update: The last paragraph was saying "out1" and "out2" when I meant "in1" and "in2". Fixed.

Replies are listed 'Best First'.
Re^2: How to avoid string copies in function calls?
by Anonymous Monk on Nov 09, 2011 at 08:48 UTC
    Thanks for your comments but further tests do show a difference. Here is what I've used:
    "out1ax$len" => sub { for (1 .. COUNT) { $tmp = out1() } }, "out1bx$len" => sub { for (1 .. COUNT) { $tmp = \out1() } }, "out2ax$len" => sub { for (1 .. COUNT) { $tmp = ${ out2() } } }, "out2bx$len" => sub { for (1 .. COUNT) { $tmp = out2() } },
    And here is what it gives:
     out1ax100: 44 wallclock secs (43.20 usr +  0.01 sys = 43.21 CPU) @  0.23/s (n=10)
     out1bx100: 42 wallclock secs (42.03 usr +  0.01 sys = 42.04 CPU) @  0.24/s (n=10)
     out2ax100: 41 wallclock secs (40.59 usr +  0.00 sys = 40.59 CPU) @  0.25/s (n=10)
     out2bx100:  2 wallclock secs ( 1.61 usr +  0.00 sys =  1.61 CPU) @  6.21/s (n=10)
    
    So out2b (real return by reference) is much faster than out1b (take the reference of the result). So string copy does take place when Perl handles the return value of the function. Or did I miss something?

      Perfect. This is what I was expecting to see. Yes, non-lvalue functions copy their result.

      $ perl -MDevel::Peek -e'Dump sub { my $x = "abc"; Dump $x; $x }->()' SV = PV(0x8bfe730) at 0x8c1b2b0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x8c16088 "abc"\0 <-- Note the address CUR = 3 LEN = 12 SV = PV(0x8bfe780) at 0x8c00768 REFCNT = 1 FLAGS = (TEMP,POK,pPOK) PV = 0x8c1e2e0 "abc"\0 <-- A copy CUR = 3 LEN = 12

      That's why returning a string ($ref = \out1()) is as expensive as assigning a string ($str = ${ out2() }).

      So why is both returning a string and assigning it not ($str = out1()) twice as expensive?

      When assigning a string slated to be destroyed* such as the automatic copy of a return value, the assignment will steal the buffer instead of copying it.

      * — (FLAGS & TEMP) && REFCNT == 1, I think.