Skip to content

Commit

Permalink
[Cursor] We don't actually use the ability to attach multiple fates t…
Browse files Browse the repository at this point in the history
…o a NFA node. -0.05s startup time

git-svn-id: http://svn.pugscode.org/pugs@31353 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information
sorear committed Jun 18, 2010
1 parent dcdcbfe commit 18b0b4b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 53 deletions.
19 changes: 8 additions & 11 deletions CursorBase.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -1852,17 +1852,14 @@ sub _dump_nfa { my ($name, $nfa) = @_;
for (my $j = 2; $j < @{ $nfa->[$ix] }; $j += 2) {
push @go, "[" . join("-",@{$nfa->[$ix][$j] // []}) . "] => " . $nfa->[$ix][$j+1];
}
my $h = sprintf "%4d: %-30s %s ", $ix, join(", ", @go),
my $l = sprintf "%4d: %-30s %s ", $ix, join(", ", @go),
($nfa->[$ix][0]{I} ? 'I' : ' ');
my @fate = map { my @x = @$_;
my $y = pop @x;
push @x, "..." if $y;
join(" ", "-->", @x) } @{ $nfa->[$ix][1] };
@fate = ('') if !@fate;
for (@fate) {
print ::LOG $h . $_ . "\n";
$h = ' ' x length($h);
if ($nfa->[$ix][1]) {
my @x = @{ $nfa->[$ix][1] };
push @x, "..." if pop(@x);
$l .= join(" ", "-->", @x);
}
print ::LOG $l, "\n";
}
print ::LOG "---- END NFA DUMP ----\n";
}
Expand Down Expand Up @@ -2067,7 +2064,7 @@ sub _jit_dfa_node { my ($lexer, $node) = @_;
$black{$nix} = 1;
my $nfn = $nfa->[$nix];

push @{ $node->[0] }, @{ $nfn->[1] };
push @{ $node->[0] }, $nfn->[1] if $nfn->[1];
for (my $i = 2; $i < @$nfn; $i += 2) {
if (!$nfn->[$i]) {
push @grey, $nfn->[$i+1];
Expand Down Expand Up @@ -2261,7 +2258,7 @@ sub _AUTOLEXpeek { my $self = shift;
if ($::AUTOLEXED{$realkey}) { # no left recursion allowed in lexer!
die "Left recursion in $key" if $fakepos == $::AUTOLEXED{$realkey};
$self->deb("Suppressing lexer recursion on $key") if DEBUG & DEBUG::autolexer;
return { USED_METHODS => {}, NFA => [[{I=>1}, [[1]]]] }; # (but if we advanced just assume a :: here)
return { USED_METHODS => {}, NFA => [[{I=>1}, [1]]] }; # (but if we advanced just assume a :: here)
}
$key = 'termish' if $key eq 'EXPR';
return $::LEXERS{ref $self}->{$realkey} //= do {
Expand Down
77 changes: 35 additions & 42 deletions RE_ast.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use utf8;
use strict; use warnings;
use DEBUG;
use Encode;
my $DIMP = [[{I=>1}, [[1]] ]];
my $DNULL = [[undef, [[0]] ]];
my $DIMP = [[{I=>1}, [1] ]];
my $DNULL = [[undef, [0] ]];

# The DFA engine has two priorities; top priority is to generate the correct
# pattern prefixes; second is to generate as much fate as it can.
Expand Down Expand Up @@ -109,37 +109,30 @@ sub nfarebase { my ($onto, $ary) = @_;
$offs;
}

sub nfaprefate { my ($fates, $nfa) = @_;
sub nfaprefate { my ($fate, $nfa) = @_;
my @out = @$nfa;

for my $onode (@out) {
$onode = [ @$onode ];
# Non-accepting can just be copied
next unless @{$onode->[1]};
next unless $onode->[1];

my @nfates;
for my $f1 (@$fates) {
if ($f1->[-1]) {
# Non-extensible fate makes this easy
push @nfates, $f1;
next;
}
my @f1 = @$f1;
pop @f1;
for my $f2 (@{$onode->[1]}) {
push @nfates, [ @f1, @$f2 ];
}
if ($fate->[-1]) {
# Non-extensible fate makes this easy
$onode->[1] = $fate;
} else {
my @f = @$fate;
pop @f;
$onode->[1] = [ @f, @{ $onode->[1] } ];
}

$onode->[1] = \@nfates;
}

\@out;
}

sub nfaltmprefate { my ($tag, $val, $tb, $inner) = @_;
my $ord = pack("NN", (~($inner->[0][0]{LITLEN} // 0)), $tb);
nfaprefate([[ $tag, $val, $ord, 0 ]], $inner);
nfaprefate([ $tag, $val, $ord, 0 ], $inner);
}

# When a non-LTM alternation or quantifier is applied to a subregex, it becomes
Expand All @@ -149,7 +142,7 @@ sub nfaltmprefate { my ($tag, $val, $tb, $inner) = @_;
sub nfahasnontrivialfates { my ($inner) = @_;
my $ok = 1;
NODE: for my $n (@$inner) {
for my $fate (@{ $n->[1] }) {
if (my $fate = $n->[1]) {
if ((@$fate > 1) || $fate->[0]) {
$ok = 0;
last NODE;
Expand All @@ -170,7 +163,7 @@ sub nfaseq { my ($fst, $sndthunk) = @_;

for my $j (0 .. $max) {
# Non-accepting can just be copied
next unless @{$out[$j][1]};
next unless $out[$j][1];
$out[$j] = [ @{ $out[$j] } ];
# Imperative acceptors stay accepting and in the same way
next if $out[$j][0]{I};
Expand All @@ -179,7 +172,7 @@ sub nfaseq { my ($fst, $sndthunk) = @_;
# willing to lose deep fating... food for thought.
push @{$out[$j]}, undef, nfarebase(\@out,
nfaprefate($out[$j][1], $sndthunk->()));
$out[$j][1] = []; # not accepting any more
$out[$j][1] = undef; # not accepting any more
}

if ($out[0][0]{LITERAL}) {
Expand All @@ -195,49 +188,49 @@ sub nfaseq { my ($fst, $sndthunk) = @_;
# nfaplus would be possible, but it would be a pessimization from a deep fating
# standpoint
sub nfastar { my ($in) = @_;
my @out = ( [undef, []] );
my @out = ( [undef, undef] );
nfarebase(\@out, $in);
my $fates = [[ 1 ]];
my $fate = [ 1 ];

# all nodes already cloned by nfarebase
for my $node (@out) {
next unless @{ $node->[1] };
next unless $node->[1];
if ($node->[0]{I}) {
$node->[1] = $fates;
$node->[1] = $fate;
next;
}
$node->[1] = [];
$node->[1] = undef;
push @$node, undef, 0;
}

$out[0][1] = $fates;
$out[0][1] = $fate;
push @{ $out[0] }, undef, 1;
\@out;
}

sub nfaopt { my ($in) = @_;
my @out = ( [undef, [], undef, 1, undef, 2], [undef, []] );
my @out = ( [undef, undef, undef, 1, undef, 2], [undef, undef] );

my $fates = [[ 1 ]];
my $fate = [ 1 ];
nfarebase(\@out, $in);

# all nodes already cloned by nfarebase
for my $node (@out) {
next unless @{ $node->[1] };
next unless $node->[1];
if ($node->[0]{I}) {
$node->[1] = $fates;
$node->[1] = $fate;
next;
}
$node->[1] = [];
$node->[1] = undef;
push @$node, undef, 1;
}

$out[1][1] = $fates;
$out[1][1] = $fate;
\@out;
}

sub nfadisj { my @ins = @_;
my @out = ( [ undef, [] ] );
my @out = ( [ undef, undef ] );

for my $in (@ins) {
push @{ $out[0] }, undef, nfarebase(\@out, $in);
Expand All @@ -247,19 +240,19 @@ sub nfadisj { my @ins = @_;
}

sub nfacclass { my @terms = @_;
return [ [ undef, [], map { $_, 1 } @terms ],
[ undef, [[0]] ] ];
return [ [ undef, undef, map { $_, 1 } @terms ],
[ undef, [0] ] ];
}

sub nfastring { my ($i, $text) = @_;
my @nfa;
for my $c (split //, $text) {
my @e = $i ? (lc($c), uc($c)) : ($c);
push @nfa, [ undef, [], map {[$_], @nfa+1} @e ];
push @nfa, [ undef, undef, map {[$_], @nfa+1} @e ];
}
$nfa[0][0]{LITERAL} = 1;
$nfa[0][0]{LITLEN} = length($text);
[ @nfa, [ undef, [[0]] ] ];
[ @nfa, [ undef, [0] ] ];
}

{ package REbase;
Expand Down Expand Up @@ -413,7 +406,7 @@ sub nfastring { my ($i, $text) = @_;
'::' => $DIMP,
':::' => $DIMP,
'.*?' => $DIMP,
'.*' => [ [ undef, [[0]], ['ALL'], 0 ] ],
'.*' => [ [ undef, [0], ['ALL'], 0 ] ],
);

sub nfa { my $self = shift; my ($C) = @_;
Expand Down Expand Up @@ -493,7 +486,7 @@ sub nfastring { my ($i, $text) = @_;
my $alts = $self->{'zyg'};
::here(0+@$alts);
# block fate propagation
::nfaprefate([[1]], $alts->[0]->nfa($C));
::nfaprefate([1], $alts->[0]->nfa($C));
}
}

Expand Down Expand Up @@ -538,7 +531,7 @@ sub nfastring { my ($i, $text) = @_;
$text =~ s/^<\s*//;
$text =~ s/\s*>$//;

::nfaprefate([[1]], ::nfadisj(map { ::nfastring($self->{i}, $_) } split(/\s+/, $text)));
::nfaprefate([1], ::nfadisj(map { ::nfastring($self->{i}, $_) } split(/\s+/, $text)));
}
}

Expand Down

0 comments on commit 18b0b4b

Please sign in to comment.