The problem with this pseudo-code:

${ X() } . ${ X() } concat( deref( X() ), deref( X() ), )

is that it means that concat() copies both it arguments -- rather than a copy of the second being appended to the first -- otherwise it would modified the referenced var.

Which means that here:

${ X() } . ' ' . ${ X() } concat( concat( deref( X() ), ' ', ), deref( X() ), )

The deref'd value and the space are copied once to put them together, and then the whole thing is copied again in the second concat().

That is not an optimisation.

If the sequences were:

${ X() } . ${ X() } concat( new( deref( X() ) ), deref( X() ), )
${ X() } . ' ' . ${ X() } concat( concat( new( deref( X() ) ), ' ', ), deref( X() ), )

Ie. concat() appends it second argument to its first (as in sv_catsv()) and new() return a copy of its argument (as in newSVsv()), the each part of the final string is only copied once (subject to the need for reallocs which will be unnecessary for two or three short strings because of allocation (alignment) minimums), and it would be more efficient.

It would also prevent the bug from manifesting itself.

Update: Maybe this will clarify things; or not:

use strict; use warnings; use feature qw( say ); package ike; { sub concat { $_[0] . $_[1] } sub deref :lvalue { ${ $_[0] } } my $a = \ '1'; my $b = \ '2'; say concat( deref( $a ), deref( $b ), ); say $$a; say <<'EOS'; Note: $$a has not changed, So, $$a was copied to produce the concatena +tion 1 + 1 = 2 bytes copied. EOS say concat( concat( deref( $a ), ' ', ), deref( $b ), ); say <<'EOS'; Note: $$a has not changed, So, $$a was copied to produce the concatena +tion Therefore, the whole of that result was copied (again) to produce the result of the second concatenation. ( 1 + 1 ) * 2 + 1 = 5 bytes copied. EOS } package buk; { sub concat { $_[0] .= $_[1] } sub deref { ${ $_[0] } } sub new{ "$_[0]" } my $a = \ '1'; my $b = \ '2'; say concat( concat( new( deref( $a ) ), ' ', ), deref( $b ), ); say <<'EOS'; New copies 1 byte its arg into a new string The first concat() copes the space and apend it to the new string. The second concat() copies 1 byte from $b and apend it to that. 1 + 1 + 1 = 3 bytes copied. EOS } __END__ C:\test>junk999 12 1 Note: $$a has not changed, So, $$a was copied to produce the concatena +tion 1 + 1 = 2 bytes copied. 1 2 Note: $$a has not changed, So, $$a was copied to produce the concatena +tion Therefore, the whole of that result was copied (again) to produce the result of the second concatenation. ( 1 + 1 ) * 2 + 1 = 5 bytes copied. 1 2 New copies 1 byte its arg into a new string The first concat() copes the space and apend it to the new string. The second concat() copies 1 byte from $b and apend it to that. 1 + 1 + 1 = 3 bytes copied.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re^6: Order of evaluation/interpolation of references (UPDATED) by BrowserUk
in thread Order of evaluation/interpolation of references by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.