Skip to content

Commit 2a9c45d

Browse files
committed
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.
1 parent 854efd3 commit 2a9c45d

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/QRegex/Cursor.nqp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@ role NQPCursorRole is export {
306306
method !protoregex($name) {
307307
# Obtain and run NFA.
308308
my $shared := $!shared;
309-
my $nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
309+
my $nfa := self.HOW.cache_get(self, $name);
310+
if nqp::isnull($nfa) {
311+
$nfa := self.'!protoregex_nfa'($name);
312+
self.HOW.cache_add(self, $name, $nfa);
313+
}
310314
my @fates := $nfa.run(nqp::getattr_s($shared, ParseShared, '$!target'), $!pos);
311315

312316
# Update highwater mark.
@@ -367,7 +371,11 @@ role NQPCursorRole is export {
367371
}
368372

369373
# Evaluate the alternation.
370-
my $nfa := self.HOW.cache(self, $name, { self.'!alt_nfa'($!regexsub, $name) });
374+
my $nfa := self.HOW.cache_get(self, $name);
375+
if nqp::isnull($nfa) {
376+
$nfa := self.'!alt_nfa'($!regexsub, $name);
377+
self.HOW.cache_add(self, $name, $nfa);
378+
}
371379
$nfa.run_alt(nqp::getattr_s($shared, ParseShared, '$!target'), $pos, $!bstack, $!cstack, @labels);
372380
}
373381

@@ -395,7 +403,7 @@ role NQPCursorRole is export {
395403
sub precomp_alt_nfas($meth) {
396404
if nqp::can($meth, 'ALT_NFAS') {
397405
for $meth.ALT_NFAS -> $name {
398-
self.HOW.cache(self, $name, { self.'!alt_nfa'($meth, $name.key) });
406+
self.HOW.cache(self, ~$name, { self.'!alt_nfa'($meth, $name.key) });
399407
}
400408
}
401409
}

src/how/NQPClassHOW.nqp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ knowhow NQPClassHOW {
734734
##
735735
## Cache-related
736736
##
737+
737738
method cache($obj, $key, $value_generator) {
738739
%!caches := nqp::hash() unless nqp::ishash(%!caches);
739740
nqp::existskey(%!caches, $key) ??
@@ -746,7 +747,17 @@ knowhow NQPClassHOW {
746747
%!caches := {} unless nqp::isnull(%!caches);
747748
nqp::scwbenable();
748749
}
749-
750+
751+
method cache_get($obj, $key) {
752+
my %caches := %!caches;
753+
nqp::ishash(%caches) ?? nqp::atkey(%caches, $key) !! nqp::null()
754+
}
755+
756+
method cache_add($obj, $key, $value) {
757+
%!caches := nqp::hash() unless nqp::ishash(%!caches);
758+
%!caches{$key} := $value;
759+
}
760+
750761
##
751762
## Mix-ins
752763
##

0 commit comments

Comments
 (0)