Skip to content
Permalink
Browse files

Streamline Rat +/- Rat, Int +/- Rat, Rat +/- Int

- use nqp::getattr instead of accessors
- make sure each attribute is only fetched once
- makes these about 20% faster
  • Loading branch information...
lizmat committed Jan 2, 2019
1 parent d836ca4 commit 29d7f709586930ed5b8dc1ed6253958f66097d10
Showing with 34 additions and 30 deletions.
  1. +34 −30 src/core/Rat.pm6
@@ -105,49 +105,53 @@ multi sub prefix:<->(FatRat:D \a --> FatRat:D) {
}

multi sub infix:<+>(Rational:D \a, Rational:D \b) {
DIVIDE_NUMBERS
a.numerator*b.denominator + b.numerator*a.denominator,
a.denominator*b.denominator,
a,
b
my \adenom := nqp::getattr(nqp::decont(a),a.WHAT,'$!denominator');
my \bdenom := nqp::getattr(nqp::decont(b),b.WHAT,'$!denominator');
DIVIDE_NUMBERS(
nqp::getattr(nqp::decont(a),a.WHAT,'$!numerator') * bdenom
+ nqp::getattr(nqp::decont(b),b.WHAT,'$!numerator') * adenom,
adenom * bdenom, a, b
)
}
multi sub infix:<+>(Rational:D \a, Int:D \b) {
DIVIDE_NUMBERS
a.numerator + b*a.denominator,
a.denominator,
a,
b
my \adenom := nqp::getattr(nqp::decont(a),a.WHAT,'$!denominator');
DIVIDE_NUMBERS(
nqp::getattr(nqp::decont(a),a.WHAT,'$!numerator') + b * adenom,
adenom, a, b
)
}
multi sub infix:<+>(Int:D \a, Rational:D \b) {
DIVIDE_NUMBERS
a*b.denominator + b.numerator,
b.denominator,
a,
b
my \bdenom := nqp::getattr(nqp::decont(b),b.WHAT,'$!denominator');
DIVIDE_NUMBERS(
a * bdenom + nqp::getattr(nqp::decont(b),b.WHAT,'$!numerator'),
bdenom, a, b
)
}

multi sub infix:<->(Rational:D \a, Rational:D \b) {
DIVIDE_NUMBERS
a.numerator*b.denominator - b.numerator*a.denominator,
a.denominator*b.denominator,
a,
b
my \adenom := nqp::getattr(nqp::decont(a),a.WHAT,'$!denominator');
my \bdenom := nqp::getattr(nqp::decont(b),b.WHAT,'$!denominator');
DIVIDE_NUMBERS(
nqp::getattr(nqp::decont(a),a.WHAT,'$!numerator') * bdenom
- nqp::getattr(nqp::decont(b),b.WHAT,'$!numerator') * adenom,
adenom * bdenom, a, b
)
}

multi sub infix:<->(Rational:D \a, Int:D \b) {
DIVIDE_NUMBERS
a.numerator - b * a.denominator,
a.denominator,
a,
b
my \adenom := nqp::getattr(nqp::decont(a),a.WHAT,'$!denominator');
DIVIDE_NUMBERS(
nqp::getattr(nqp::decont(a),a.WHAT,'$!numerator') - b * adenom,
adenom, a, b
)
}

multi sub infix:<->(Int:D \a, Rational:D \b) {
DIVIDE_NUMBERS
a * b.denominator - b.numerator,
b.denominator,
a,
b
my \bdenom := nqp::getattr(nqp::decont(b),b.WHAT,'$!denominator');
DIVIDE_NUMBERS(
a * bdenom - nqp::getattr(nqp::decont(b),b.WHAT,'$!numerator'),
bdenom, a, b
)
}

multi sub infix:<*>(Rational:D \a, Rational:D \b) {

0 comments on commit 29d7f70

Please sign in to comment.
You can’t perform that action at this time.