in reply to Re: How to avoid string copies in function calls?
in thread How to avoid string copies in function calls?

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?

Replies are listed 'Best First'.
Re^3: How to avoid string copies in function calls?
by ikegami (Patriarch) on Nov 09, 2011 at 19:02 UTC

    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.