${ 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".
|