Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Avoid a closure per NFA evaluation.
We achieve this by a re-think of how we work with the NFA cache. Needs
a patch to the Rakudo MOP also for its regexes/grammars to work with
this change. For CORE.setting this saves a dozen GCs from the code
objects produced alone.
  • Loading branch information
jnthn committed Jul 30, 2014
1 parent 854efd3 commit 2a9c45d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/QRegex/Cursor.nqp
Expand Up @@ -306,7 +306,11 @@ role NQPCursorRole is export {
method !protoregex($name) {
# Obtain and run NFA.
my $shared := $!shared;
my $nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
my $nfa := self.HOW.cache_get(self, $name);
if nqp::isnull($nfa) {
$nfa := self.'!protoregex_nfa'($name);
self.HOW.cache_add(self, $name, $nfa);
}
my @fates := $nfa.run(nqp::getattr_s($shared, ParseShared, '$!target'), $!pos);

# Update highwater mark.
Expand Down Expand Up @@ -367,7 +371,11 @@ role NQPCursorRole is export {
}

# Evaluate the alternation.
my $nfa := self.HOW.cache(self, $name, { self.'!alt_nfa'($!regexsub, $name) });
my $nfa := self.HOW.cache_get(self, $name);
if nqp::isnull($nfa) {
$nfa := self.'!alt_nfa'($!regexsub, $name);
self.HOW.cache_add(self, $name, $nfa);
}
$nfa.run_alt(nqp::getattr_s($shared, ParseShared, '$!target'), $pos, $!bstack, $!cstack, @labels);
}

Expand Down Expand Up @@ -395,7 +403,7 @@ role NQPCursorRole is export {
sub precomp_alt_nfas($meth) {
if nqp::can($meth, 'ALT_NFAS') {
for $meth.ALT_NFAS -> $name {
self.HOW.cache(self, $name, { self.'!alt_nfa'($meth, $name.key) });
self.HOW.cache(self, ~$name, { self.'!alt_nfa'($meth, $name.key) });
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/how/NQPClassHOW.nqp
Expand Up @@ -734,6 +734,7 @@ knowhow NQPClassHOW {
##
## Cache-related
##

method cache($obj, $key, $value_generator) {
%!caches := nqp::hash() unless nqp::ishash(%!caches);
nqp::existskey(%!caches, $key) ??
Expand All @@ -746,7 +747,17 @@ knowhow NQPClassHOW {
%!caches := {} unless nqp::isnull(%!caches);
nqp::scwbenable();
}


method cache_get($obj, $key) {
my %caches := %!caches;
nqp::ishash(%caches) ?? nqp::atkey(%caches, $key) !! nqp::null()
}

method cache_add($obj, $key, $value) {
%!caches := nqp::hash() unless nqp::ishash(%!caches);
%!caches{$key} := $value;
}

##
## Mix-ins
##
Expand Down

0 comments on commit 2a9c45d

Please sign in to comment.