Skip to content

Commit aa69d06

Browse files
committed
First pass through optimizing the QAST node structures, which saves some time and some memory.
1 parent 8a9b631 commit aa69d06

File tree

7 files changed

+57
-8
lines changed

7 files changed

+57
-8
lines changed

src/QAST/Block.nqp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class QAST::Block is QAST::Node {
2+
has str $!name;
23
has str $!blocktype;
34
has int $!custom_args;
45
has str $!cuid;
56
has %!symbol;
67

8+
method name(*@value) { $!name := @value[0] if @value; $!name || "" }
79
method blocktype(*@value) { $!blocktype := @value[0] if @value; $!blocktype }
810
method custom_args(*@value) { $!custom_args := @value[0] if @value; $!custom_args }
911

src/QAST/BlockMemo.nqp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
class QAST::BlockMemo is QAST::Node {
2+
has str $!name;
23
has str $!cuid;
34

5+
method name(*@value) { $!name := @value[0] if @value; $!name || "" }
6+
47
my $cur_cuid := 0;
58
my $cuid_suffix := ~nqp::time_n();
69

src/QAST/Node.nqp

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
use NQPHLL;
22

3-
class QAST::Node is NQPCapture {
3+
class QAST::Node {
4+
# For children.
5+
has @!array
6+
is parrot_vtable_handler('get_pmc_keyed_int')
7+
is parrot_vtable_handler('set_pmc_keyed_int')
8+
is parrot_vtable_handler('exists_keyed_int')
9+
is parrot_vtable_handler('delete_keyed_int')
10+
is parrot_vtable_handler('unshift_pmc')
11+
is parrot_vtable_handler('push_pmc')
12+
;
13+
14+
# For annotations, lazily allocated.
15+
has %!hash;
16+
417
has $!node;
5-
has $!name;
618
has str $!named;
719
has $!returns;
820
has int $!arity;
@@ -11,27 +23,54 @@ class QAST::Node is NQPCapture {
1123

1224
method new(*@children, *%options) {
1325
my $new := self.CREATE();
14-
$new.BUILD();
15-
nqp::splice($new.list, @children, 0, 0);
26+
nqp::bindattr($new, QAST::Node, '@!array', @children);
1627
for %options {
1728
nqp::findmethod($new, $_.key)($new, $_.value);
1829
}
1930
$new;
2031
}
2132

2233
method node(*@value) { $!node := @value[0] if @value; $!node }
23-
method name(*@value) { $!name := @value[0] if @value; $!name }
2434
method named(*@value) { $!named := @value[0] if @value; $!named || "" }
2535
method returns(*@value) { $!returns := @value[0] if @value; $!returns }
2636
method arity(*@value) { $!arity := @value[0] if @value; $!arity }
2737
method flat(*@value) { $!flat := @value[0] if @value; $!flat }
2838
method childorder(*@value) { $!childorder := @value[0] if @value; $!childorder || "" }
2939

40+
method list() { @!array }
3041
method pop() { nqp::pop(self.list) }
3142
method push($value) { nqp::push(self.list, $value) }
3243
method shift() { nqp::shift(self.list) }
3344
method unshift($value) { nqp::unshift(self.list, $value) }
3445

46+
method hash() {
47+
%!hash
48+
}
49+
method ($key) is parrot_vtable('get_pmc_keyed_str') {
50+
%!hash{$key}
51+
}
52+
method ($key) is parrot_vtable('get_pmc_keyed') {
53+
%!hash{$key}
54+
}
55+
method ($key, $value) is parrot_vtable('set_pmc_keyed_str') {
56+
%!hash{$key} := $value;
57+
}
58+
method ($key, $value) is parrot_vtable('set_pmc_keyed') {
59+
%!hash{$key} := $value;
60+
}
61+
method ($key) is parrot_vtable('exists_keyed_str') {
62+
nqp::existskey(%!hash, $key)
63+
}
64+
method ($key) is parrot_vtable('exists_keyed') {
65+
nqp::existskey(%!hash, $key)
66+
}
67+
method ($key) is parrot_vtable('delete_keyed_str') {
68+
nqp::deletekey(%!hash, $key)
69+
}
70+
method ($key) is parrot_vtable('delete_keyed') {
71+
nqp::deletekey(%!hash, $key)
72+
}
73+
3574
my %uniques;
3675
method unique($prefix) {
3776
my $id := nqp::existskey(%uniques, $prefix) ??

src/QAST/Op.nqp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class QAST::Op is QAST::Node {
2+
has str $!name;
23
has str $!op;
34

4-
method op(*@value) { $!op := @value[0] if @value; $!op }
5+
method name(*@value) { $!name := @value[0] if @value; $!name || "" }
6+
method op(*@value) { $!op := @value[0] if @value; $!op }
57
}

src/QAST/Regex.nqp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class QAST::Regex is QAST::Node {
2+
has $!name;
23
has str $!rxtype;
34
has str $!subtype;
45
has str $!backtrack;
56
has int $!negate;
67
has int $!min;
78
has int $!max;
89

10+
method name(*@value) { $!name := @value[0] if @value; $!name }
911
method rxtype(*@value) { $!rxtype := @value[0] if @value; $!rxtype || "" }
1012
method subtype(*@value) { $!subtype := @value[0] if @value; $!subtype || "" }
1113
method backtrack(*@value) { $!backtrack := @value[0] if @value; $!backtrack || "" }

src/QAST/VM.nqp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ class QAST::VM is QAST::Node {
33

44
method new(*@children, *%alternatives) {
55
my $obj := nqp::create(self);
6-
nqp::bindattr($obj, NQPCapture, '@!array', @children);
7-
nqp::bindattr($obj, NQPCapture, '%!hash', nqp::hash());
6+
nqp::bindattr($obj, QAST::Node, '@!array', @children);
87
nqp::bindattr($obj, QAST::VM, '%!alternatives', %alternatives);
98
$obj
109
}

src/QAST/Var.nqp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class QAST::Var is QAST::Node {
2+
has str $!name;
23
has str $!scope;
34
has str $!decl;
45
has int $!slurpy;
56
has $!default;
67

8+
method name(*@value) { $!name := @value[0] if @value; $!name || "" }
79
method scope(*@value) { $!scope := @value[0] if @value; $!scope }
810
method decl(*@value) { $!decl := @value[0] if @value; $!decl }
911
method slurpy(*@value) { $!slurpy := @value[0] if @value; $!slurpy }

0 commit comments

Comments
 (0)