Perl 6: Performance and Bugfixing of Rationals / Constraints on Constants


Zoffix Znet

Amount Requested:

USD 1,999


The proposal is to perform two pieces of work on the Rakudo Perl 6 compiler along with a third bonus piece:

1. Implement support for type constraints on constants and polish some of the rough edges with assignment and .= initializer calls
2. Fix several bugs and a race condition as well as fix problems in edge cases in instantiation and literals in core Rational types, also try to improve performance in this area.
3. BONUS Work: Fix bugs in native uint64 attributes and try to use it to boost performance of Rat type

Benefits to the Perl Community


Prior to the submission of this Grant Proposal, I drafted two work proposals, presented to the Rakudo core developers, that spec the behaviour of type constraints on constants ("Constants Type Constraints" work proposal) as well as detail the work on fixing various issues in Rational types ("Polishing Rationals" work proposal).

The deliverables for this Grant Proposal are:

Project Details

The full details for the two primary deliverables are documented in the "Constants Type Constraints" work proposal and the "Polishing Rationals" work proposal.

The documents were originally prepared to detail the planned work to other Perl 6 core developers and include considered-but-rejected ideas as well as preliminary performance improvement measurements that were made.


For the BONUS Work item, I marked it as "BONUS Work" because it requires me learning new things about MoarVM and the QAST-MAST compiler. As I currently don't know much about them, I can't say with 100% certainty that I will succeed in fixing the bugs mentioned in the BONUS Work. However, if I am successful, the new knowledge will also allow me to fix many other bugs in my role as a Rakudo core developer, even after this Grant has completed.


1.1 Write Perl 6 Specification tests specifying behaviour described in the "Constants Type Constraints" work proposal
1.2 Implement type-constraint checking logic in type_declarator:sym<constant>($/) method in the Actions
1.3 Implement typecheck and coercers on constants with @ and % sigils.
1.4 Implement additional typed exceptions described in the work proposal and include additional Grammar rules in the type_declarator:sym<constant>($/) token to give more useful error messages when unsupported constructs are used.
1.5 Fully document the newly-clarified behaviour on
2.1 Write Perl 6 Specification tests specifying behaviour described in the "Polishing Rationals" work proposal
2.2 Remove .REDUCE-ME and &DON'T_DIVIDE_NUMBERS routines and their use, and always go through reduction for all operations instead.
2.3 Optimize and &DIVIDE_NUMBERS routines using techniques that produced preliminary performance improvement measurements when the work proposal was drafted
2.4 Make normalize zero-denominator Rationals to <1/0>, <-1/0>, <0/0>. Fix zero-denominator math issues, if any still remain after this change.
2.5 Evaluate whether any significant performance advantages are produced by demarcating zero-denominator rationals with ZeroDenominatorRational role and extracting zero-denominator-rational-specific logic into separate multi candidates for operators and routines. If there are such advantages, fully implement zero-denominator rationals in terms of that role.
2.6 Implement FatRatStr allomorph and all of the required operators to support it.
2.7 Make return FatRat if after reduction the denominator is over 64 bits.
2.8 Ensure Rational literals produce FatRat types when the given precision produces denominators over 64-bit. This may fall-out automatically from change in (2.6); if not, modify Grammar and Actions to support this behaviour.
2.9 Make &val produce FatRatStr for Rationals with denominators over 64-bit. This may fall-out automatically from change in (2.6), but if not, modify the logic in &val routine.
2.10 Ensure quotewords and other allomorph-creating constructs (such as arguments to sub MAIN) create FatRatStr objects for Rationals with denominators overe 64 bits. This may fall-out automatically from changes in (2.6) and (2.8).
2.11 Document the FatRatStr type as well as the newly-clarified behaviour for Rational literals, zero-denominator Rationals, and
3.1 BONUS Work: Write tests in Perl 6 Specification covering unsigned attributes and variables, ensuring to test the behaviour that's expected from unsigned types exists (some of these tests should already exists as TODOs)
3.2 BONUS Work: study how native types are implemented and implement full support for unsigned types on attributes
3.3 BONUS Work: Change parametarization of the Rat type to be [Int, uint64] and change supporting routines like &DIVIDE_NUMBERS to handle native type arguments without having to box them.
3.4 BONUS Work: implement nqp::p6gcd and nqp::p6div ops that compile to nqp::gcd_i and nqp::div_i if their parameters are native attributes (and to _I variants otherwise). Alternatively or in addition to, implement more convenient ops for handling of Rationals; what that might be will be more obvious after all of the work on Rationals in item (2) is completed.
4.1 Go through the bug ticket queue and marked all tickets fixed by this Grant as resolved, writing explicit tests to cover the reported bugs, as needed.

Project Schedule

I can begin work as soon as this Grant Proposal is approved.

I estimate it will take me 1 month to finish the work on the constants, 2 months to finish the work on Rationals, and 2 months to finish the BONUS Work on native attributes and corresponding optimizations on Rat with native denominator.

Completeness Criteria

1. Commits implementing the work in this proposal will be committed to rakudo repository (the BONUS Work portion will also see commits made to nqp repositor and MoarVM repository).
2. Commits specifying behaviour in the work proposals as well as testing fixed bugs will be commited to the Perl 6 Specification repository.
3. Commits documenting all newly-clarified behaviour will be made to the Perl 6 Documentation repository.

NOTE: the extra NQP operators described in the BONUS Work will be implemented only for the MoarVM backend. Their implementation for JVM and JavaScript backends is outside the scope of this Grant Proposal.


I've been a member of the Perl 6 Core Development team since summer of 2016. I have previously successfully completed the Perl 6 IO Grant with The Perl Foundation and to date have made 1,154 commits to The Perl 6 Documentation (#1 rank), 1,398 commits to The Perl 6 Specification (#2 rank), and 1,620 commits to the Rakudo repository (#5 rank). I also wrote numerous Perl 6 tutorials published on the Rakudo.Party website.

My previous work in the area of constants and initializers includes fixing bugs with .= initializer on attributes, sigiless variables, and $- and sigiless constants for types with :: in their names as well as compound types such as Array[Int] (Rakudo commits 7793f420e, abea32429, 562edfc50, 8ba3c86e7, and 700a07747)

My previous work with Rationals includes fixing numerous issues with zero-denominator rationals, fixing several bugs due to .REDUCE-ME optimization that I plan to remove/redesign as part of this grant, as well as performing some optimizations to routines involving Rationals. (Rakudo commits 8aeaf469, 6c299bf9f, cb2476f9b, 73182d4e9, 748d1a57b, c91bcc2af, 042cb7413, 893d09ffa, 6dbe85eda, aac9efcbd, 79553d0fc, 637241703, 7434a8f73, and b5aa3c591).

My previous work on QAST/MAST/MoarVM (which would be involved to complete BONUS Work) involves fixing spurious "useless use" warnings, re-designing the WhateverCode currier to be more performant (both in compile-time and in run-time), fixing numerous bugs with QAST::Block migration, implementation of hypered versions of extended method call variants (e.g. $foo>>.?&elems), fixing crashes with native types in conditionals, as well as implementation of optimizations in dispatch:<var> method calls, conditionals with natives, and implementation of nqp::chainstatic op for staticalizing of chained op calls. (Rakudo commits fb3dfa567, c0c7756f4, 1ee89b540, e8c6c259c, 752bb8b38, 58de239cc, ef2dc1b8b, 3c4041eab, and 97359ae42; NQP commits 71658ad85, df45cb8ce, and 414520586). I also fixed bugs in zeroing of VMArray elements, flushing of TTYs on WSL, EOF detection, and abs_n and div_i ops. (MoarVM commits 8d94732aa, 4541cf6f6, cb4c1941a, 09482f9bd, 43c926f9e, 912f96783, 3f3045d6e, cfb0bffc0, 9d7bee40e, fd7300d27, and 1c1746e52).