Skip to content

Instantly share code, notes, and snippets.

View jnthn's full-sized avatar

Jonathan Worthington jnthn

View GitHub Profile
multi trait_mod:<is>(Routine:D $r, :$randomly_dispatched!) {
$r does role {
method find_best_dispatchee(|) {
self.dispatchees.pick()
}
}
}
proto foo($) is randomly_dispatched { * }
multi foo($x) { say 'omg' }
@jnthn
jnthn / gist:5125371
Last active December 14, 2015 17:49
my $z = [ do { 1 }
+ 2 ];
say $z;
my %a; sub c { }
%a<b> = 1;
use QASTJVM;
# Build a simple QAST tree.
my $cu := QAST::CompUnit.new();
my $b := QAST::Block.new(
QAST::Op.new(
:op('say'),
QAST::SVal.new( :value('JVM compiles QAST to JVM') )
));
$cu.push($b);
class FreezeHash {
has $.hash handles *;
method new(*@args) { self.bless: *, hash => Hash.new: @args }
method at_key(FreezeHash:D: $key is copy) is rw {
$!hash.exists($key) or warn "Cannot set non-existing key '$key'";
$!hash.at_key($key);
}
}
my $fh = FreezeHash.new: "a" => 1, "b" => 2;
use QRegexJVM;
sub try_match($s) {
# Create a cursor.
my $ci := NQPCursor.'!cursor_init'($s);
# Start it, match a digit.
my $c := $ci.'!cursor_start_cur'();
$c.'!cursor_pos'(0);
my $d := $c.digit();
> timecmd nqp nqp-jvm-cc.nqp -e "sub foo() { }; my $i := 0; while $i++ < 100000000 { foo() }; say($i)"
100000001
command took 0:0:5.36 (5.36s total)
> timecmd nqp -e "sub foo() { }; my $i := 0; while $i++ < 100000000 { foo() }; say($i)"
100000001
command took 0:1:25.11 (85.11s total)
> type x.nqp
my knowhow A {
method m() { nqp::say("omg method") }
}
A.m();
> nqp nqp-jvm-cc.nqp --setting=NULL --javaclass=OMGClass --output=OMGClass.class --target=classfile x.nqp
> java -cp .;bin;3rdparty/bcel/bcel-5.2.jar OMGClass
omg method

Postcircumfix Change Proposal

A while back, if memory serves me correctly, sorear++ pointed out the operator-y nature of the [] and {} postcircumfixes and suggested they should perhaps be subs, not methods. This makes a lot of sense in so far as adding new ones is a language tweaks. However, I believe that making a change like this is important for optimizability.

Today, looking up an array element is two method dispatches, first to a postcircumfix and then to at_pos or at_key. The late bound nature of these makes optimization difficult. Inlining of subroutines is already done by the Rakudo optimizer. Inlining of methods at compile time, on the other hand, is much harder. Nothing is closed or finalized by default, meaning at best a static optimizer can emit a speculative inline with a fallback. A fancy JIT or, on things like the JVM, doing our own runtime tracing and producing specialized JVM bytecode, are possible. However, something as fundamental as array access being so hard to optimize is a bit of a

diff --git a/src/QRegex/P6Regex/Actions.nqp b/src/QRegex/P6Regex/Actions.nqp
index 5fe283a..90597c2 100755
--- a/src/QRegex/P6Regex/Actions.nqp
+++ b/src/QRegex/P6Regex/Actions.nqp
@@ -432,7 +432,7 @@ class QRegex::P6Regex::Actions is HLL::Actions {
while $i < $n {
my $ast := $clist[$i].ast;
if $ast.negate {
- $ast.subtype('zerowidth');
+ $ast := QAST::Regex.new( :rxtype<conj>, :subtype<zerowidth>, $ast );