This performance question has pestered me for awhile, so I'm hoping someone can shed light wo/ my having to do deep SV deconstruction:

I'm managing some large binary buffers (64K+, handled as scalars). I need to modify various pieces of the buffer in situ. Logically, using pack(), unpack(), and substr(), everything works fine.

But what of performance and passing the buffers around between methods/objects ? Since perl is pass-by-value, I assume my big buffer gets copied unless I explicitly pass it as a ref. But what happens with substr() (or other builtins) ? Are they optmized to use the underlying SV wo/ making a copy ? Obviously, lhs substr() modifies the original, but is it making a copy, modifying the copy, then replacing the SV's buffer, or does it truly work in situ ?

I've tried perusing the perl source, but there are so many substr's, I don't know which is which.

Update:

Many thanks to all the responders. Just to get a sense of the impact, I wrote a little (imprecise) test script:

use Time::HiRes qw(time); my $buffer = "\0" x 65000; my $start = time(); substr($buffer, 10, 4) = pack('L', $_) foreach (1..900000); print "inline took ", (time() - $start), " secs\n"; $start = time(); bySVBuffer3Arg($buffer, $_) foreach (1..900000); print "bySVBuffer3Arg() took ", (time() - $start), " secs\n"; $start = time(); bySVBuffer4Arg($buffer, $_) foreach (1..900000); print "bySVBuffer4Arg() took ", (time() - $start), " secs\n"; $start = time(); byRefBuffer(\$buffer, \$_) foreach (1..900000); print "byRefBuffer() took ", (time() - $start), " secs\n"; $start = time(); byCopyBuffer($buffer, $_) foreach (1..900000); print "byCopyBuffer() took ", (time() - $start), " secs\n"; sub byCopyBuffer { my ($buf, $val) = @_; substr($buf, 10, 4) = pack('L', $val); return 1; } sub byRefBuffer { my ($buf, $val) = @_; substr($$buf, 10, 4, pack('L', $$val)); return 1; } sub bySVBuffer3Arg { substr($_[0], 10, 4) = pack('L', $_[1]); return 1; } sub bySVBuffer4Arg { substr($_[0], 10, 4, pack('L', $_[1])); return 1; }
and got these results (WinXP, 2.4GHz, AS 5.8.6):

C:\Perl>perl bufref.pl
inline took 0.919242858886719 secs
bySVBuffer3Arg() took 1.52185487747192 secs
bySVBuffer4Arg() took 1.23141598701477 secs
byRefBuffer() took 2.32297611236572 secs
byCopyBuffer() took 20.3989539146423 secs
So even passing refs is about half the speed of direct param manipulation. And the copy is really expensive.

Update 2:

Updated above code to include a 4 arg substr() direct from params, which seems about 20% faster than an lhs 3 arg substr().


In reply to lhs substr(): refs vs. scalars by renodino

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.