Bit ops?
sub using_str_bit_ops_and_s { my ($s1, $s2) = @_; (my $mask = $s1) =~ tr/\x00/\xFF/c; return ($s1 & $mask) | ($s2 & ~$mask); }
I was thinking in the same direction, except I'd use xor. After all, ((A xor B ) and 0) xor B is B, and ((A xor B) and true) xor B is A, for any value of A and B.

So:

sub using_str_bit_xor { my ($s1, $s2) = @_; (my $mask = $s1) =~ tr/\x00/\xFF/c; return (($s1 ^ $s2) & $mask ) ^ $s2; }

Benchmarks: (and yes I did use 64k byte strings):

my $x = pack 'C*', map int rand(256), 1 .. 64*1024; my $y = pack 'C*', map int rand(256), 1 .. 64*1024; using_str_bit_ops_and_s($x, $y) eq using_str_bit_xor($x, $y) or die "Results are different??"; use Benchmark 'cmpthese'; cmpthese -3, { using_str_bit_ops_and_s => sub{ my $r = using_str_bit_ops_and_s($x, +$y) }, using_str_bit_xor=> sub{ my $r = using_str_bit_xor($x, $y) }, };
Result:
Rate using_str_bit_ops_and_s using_str_ +bit_xor using_str_bit_ops_and_s 686/s -- + -16% using_str_bit_xor 821/s 20% + --
The speed gain is humble, but real.

In reply to Re^2: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer) by bart
in thread Challenge: CPU-optimized byte-wise or-equals (for a meter of beer) by dragonchild

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.