From 4a31d0daf7c2112c32743a6f48e3937281f6469d Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Wed, 28 Feb 2018 19:29:01 +0000 Subject: [PATCH] Optimize boolification methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace .Bool calls with nqp::p6bool() in Bool.pm (other areas of the compiler still use the method call, which could be changed too) Using `my $x = …; {for ^3000_000 {my $ = ?$x}; say now - ENTER now}` Makes: - with Num; 9x faster - with `class {}.new`; 11% faster - with `class { method Bool { False } }.new` or `but False` of core types; 4.6x SLOWER - with an unmixed Int or Str, no change --- src/core/Bool.pm | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/core/Bool.pm b/src/core/Bool.pm index 466de05152d..718b0f4dfd9 100644 --- a/src/core/Bool.pm +++ b/src/core/Bool.pm @@ -66,12 +66,12 @@ multi sub postfix:<-->(Bool:D $a is rw) { proto sub prefix:(Mu $) is pure {*} multi sub prefix:(Bool:D \a) { a } multi sub prefix:(Bool:U \a) { Bool::False } -multi sub prefix:(Mu \a) { a.Bool } +multi sub prefix:(Mu \a) { nqp::p6bool(a) } proto sub prefix:(Mu $) is pure {*} multi sub prefix:(Bool:D \a) { a } multi sub prefix:(Bool:U \a) { Bool::False } -multi sub prefix:(Mu \a) { a.Bool } +multi sub prefix:(Mu \a) { nqp::p6bool(a) } proto sub prefix:(Mu $) is pure {*} multi sub prefix:(Bool \a) { nqp::p6bool(nqp::not_i(nqp::istrue(a))) } @@ -85,16 +85,18 @@ proto sub prefix:(Mu $) is pure {*} multi sub prefix:(Mu \a) { not a } proto sub infix:(Mu $?, Mu $?) is pure {*} -multi sub infix:(Mu $x = Bool::True) { $x.Bool } -multi sub infix:(Mu \a, Mu \b) { a.Bool && b.Bool } +multi sub infix:(Mu $x = Bool::True) { nqp::p6bool($x) } +multi sub infix:(Mu \a, Mu \b) { nqp::p6bool(a && b) } proto sub infix:(Mu $?, Mu $?) is pure {*} -multi sub infix:(Mu $x = Bool::False) { $x.Bool } -multi sub infix:(Mu \a, Mu \b) { a.Bool || b.Bool } +multi sub infix:(Mu $x = Bool::False) { nqp::p6bool($x) } +multi sub infix:(Mu \a, Mu \b) { nqp::p6bool(a || b) } proto sub infix:(Mu $?, Mu $?) is pure {*} -multi sub infix:(Mu $x = Bool::False) { $x.Bool } -multi sub infix:(Mu \a, Mu \b) { nqp::p6bool(nqp::ifnull(nqp::xor(a.Bool,b.Bool), 0)) } +multi sub infix:(Mu $x = Bool::False) { nqp::p6bool($x) } +multi sub infix:(Mu \a, Mu \b) { + nqp::p6bool(nqp::ifnull(nqp::xor(nqp::p6bool(a),nqp::p6bool(b)), 0)) +} # These operators are normally handled as macros in the compiler; # we define them here for use as arguments to functions.