Permalink
Browse files

Make Blob:D cmp/lt/gt/le/ge Blob:D 3800x faster

- for two 10K identical buffers, faster still for inequal buffers
- implement Blob:D.COMPARE helper method
- which checks number of elements before checking contents
- no longer allocates copies of both buffers, so more memory friendly
  • Loading branch information...
lizmat committed Feb 26, 2016
1 parent 1969a42 commit e3342da00e7cfca618acbab37b90f13a133c73f6
Showing with 22 additions and 22 deletions.
  1. +22 −22 src/core/Buf.pm
@@ -161,6 +161,21 @@ my role Blob[::T = uint8] does Positional[T] does Stringy is repr('VMArray') is
$subbuf
}

method COMPARE(Blob:D: Blob:D \other) {
my $other := nqp::decont(other);
my int $elems = nqp::elems(self);
if nqp::cmp_i($elems,nqp::elems($other)) -> $diff {
$diff
}
else {
my int $i = -1;
return nqp::cmp_i(nqp::atpos_i(self,$i),nqp::atpos_i($other,$i))
if nqp::cmp_i(nqp::atpos_i(self,$i),nqp::atpos_i($other,$i))
while nqp::islt_i($i = $i + 1,$elems);
0
}
}

method SAME(Blob:D: Blob:D \other) {
my $other := nqp::decont(other);
my int $elems = nqp::elems(self);
@@ -650,28 +665,13 @@ multi sub infix:<eqv>(Blob:D \a, Blob:D \b) {
!! False
}

multi sub infix:<cmp>(Blob:D $a, Blob:D $b) {
[||] $a.list Z<=> $b.list or $a.elems <=> $b.elems
}

multi sub infix:<eq>(Blob:D \a, Blob:D \b) { a.SAME(b) }
multi sub infix:<ne>(Blob:D \a, Blob:D \b) { !a.SAME(b) }

multi sub infix:<lt>(Blob:D $a, Blob:D $b) {
($a cmp $b) == -1
}

multi sub infix:<gt>(Blob:D $a, Blob:D $b) {
($a cmp $b) == 1
}

multi sub infix:<le>(Blob:D $a, Blob:D $b) {
($a cmp $b) != 1
}

multi sub infix:<ge>(Blob:D $a, Blob:D $b) {
($a cmp $b) != -1
}
multi sub infix:<cmp>(Blob:D \a, Blob:D \b) { ORDER(a.COMPARE(b)) }
multi sub infix:<eq> (Blob:D \a, Blob:D \b) { a.SAME(b) }
multi sub infix:<ne> (Blob:D \a, Blob:D \b) { !a.SAME(b) }
multi sub infix:<lt> (Blob:D \a, Blob:D \b) { a.COMPARE(b) == -1 }
multi sub infix:<gt> (Blob:D \a, Blob:D \b) { a.COMPARE(b) == 1 }
multi sub infix:<le> (Blob:D \a, Blob:D \b) { a.COMPARE(b) != 1 }
multi sub infix:<ge> (Blob:D \a, Blob:D \b) { a.COMPARE(b) != -1 }

sub subbuf-rw($b is rw, $from = 0, $elems = $b.elems - $from) is rw {
my Blob $subbuf = $b.subbuf($from, $elems);

0 comments on commit e3342da

Please sign in to comment.