@@ -567,7 +567,7 @@ say Buf.new(1000, 2000, 3000).List; # OUTPUT: «(232 208 184)»
567
567
say my uint8 @a = 1000, 2000, 3000; # OUTPUT: «232 208 184»
568
568
= end code
569
569
570
- = head2 Auto-Boxing
570
+ = head2 Auto-boxing
571
571
572
572
While they can be referred to as "native I < types > ", native numerics are not
573
573
actually classes that have any sort of methods available. However, you I < can >
@@ -595,9 +595,9 @@ my int $a-native = -42;
595
595
{ for ^1000_000 { $a-native.abs }; say now - ENTER now } # OUTPUT: «0.938720»
596
596
= end code
597
597
598
- As you can see above, the native variant is more than twice slower. The reason is the method
599
- call requires the native type to be boxed, while no such thing is needed in the non-native
600
- variant, hence the performance loss.
598
+ As you can see above, the native variant is more than twice slower. The reason
599
+ is the method call requires the native type to be boxed, while no such thing is
600
+ needed in the non-native variant, hence the performance loss.
601
601
602
602
In this particular case, we can simply switch to a subroutine form of L < abs|/routine/abs > , which can work
603
603
with native types without boxing them. In other cases, you may need to seek out other solutions
@@ -621,13 +621,13 @@ pragma).
621
621
622
622
= head2 Native dispatch
623
623
624
- It is possible to have native candidates alongside non-native candidates to, for example,
625
- offer faster algorithms with native candidates when sizes are predictable, but to fallback
626
- to slower non-native alternatives otherwise. The following are the rules concerning multi-dispatch
627
- involving native candidates.
624
+ It is possible to have native candidates alongside non-native candidates to, for
625
+ example, offer faster algorithms with native candidates when sizes are
626
+ predictable, but to fallback to slower non-native alternatives otherwise. The
627
+ following are the rules concerning multi-dispatch involving native candidates.
628
628
629
- First, the size of the native type does not play a role in dispatch and an C < int8 > is considered
630
- to be the same as C < int16 > or C < int > :
629
+ First, the size of the native type does not play a role in dispatch and an
630
+ C < int8 > is considered to be the same as C < int16 > or C < int > :
631
631
632
632
= begin code
633
633
multi foo(int $x) { say "int" }
@@ -640,10 +640,10 @@ foo my int $x = 42;
640
640
= end code
641
641
642
642
Second, if a routine is an C < only > —i.e. it is not a
643
- L « C < multi > |/language/functions#Multi-dispatch» —that
644
- takes a non- native type but a native one was given during the call, or vice-versa, then the argument
645
- will be auto-boxed or auto-unboxed to make the call possible. If the given
646
- argument is too large to fit into the native parameter, an exception will be thrown:
643
+ L « C < multi > |/language/functions#Multi-dispatch» —that takes a non-native type but
644
+ a native one was given during the call, or vice-versa, then the argument will be
645
+ auto-boxed or auto-unboxed to make the call possible. If the given argument is
646
+ too large to fit into the native parameter, an exception will be thrown:
647
647
648
648
= begin code
649
649
-> int {}( 42 ); # OK; auto-unboxing
@@ -652,18 +652,20 @@ argument is too large to fit into the native parameter, an exception will be thr
652
652
-> Int {}( my int $ = 42 ); # OK; auto-boxing
653
653
= end code
654
654
655
- When it comes to L « C < multi > |/language/functions#Multi-dispatch» routines, native arguments
656
- will always be auto-boxed if no native candidates are available to take them:
655
+ When it comes to L « C < multi > |/language/functions#Multi-dispatch» routines, native
656
+ arguments will always be auto-boxed if no native candidates are available to
657
+ take them:
657
658
658
659
= begin code
659
660
multi foo (Int $x) { $x }
660
661
say foo my int $ = 42; # OUTPUT: «42»
661
662
= end code
662
663
663
- The same luxury is not afforded when going the other way. If only a native candidate is
664
- available, a non-native argument will I < not > be auto-unboxed and instead an exception indicating
665
- no candidates matched will be thrown (the reason for this asymmetry is a native type can always
666
- be boxed, but a non-native may be too large to fit into a native):
664
+ The same luxury is not afforded when going the other way. If only a native
665
+ candidate is available, a non-native argument will I < not > be auto-unboxed and
666
+ instead an exception indicating no candidates matched will be thrown (the reason
667
+ for this asymmetry is a native type can always be boxed, but a non-native may be
668
+ too large to fit into a native):
667
669
668
670
= begin code
669
671
multi f(int $x) { $x }
0 commit comments