Skip to content

Commit

Permalink
Avoid a closure per call to !run-supply-code
Browse files Browse the repository at this point in the history
Which saves 20,000 of them plus a GC run in `my $s = supply emit 42; for
^10_000 { react whenever $s {  } }`.
  • Loading branch information
jnthn committed Jan 12, 2018
1 parent d0b2cc1 commit f04b1d7
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/core/Supply.pm
Expand Up @@ -1927,36 +1927,34 @@ augment class Rakudo::Internals {
method !run-supply-code(&code, \value, $state, &add-whenever) {
my @run-after;
my $queued := $state.run-async-lock.protect-or-queue-on-recursion: {
if $state.active > 0 {
my &*ADD-WHENEVER := &add-whenever;
my $emitter = {
if $state.active {
my \ex := nqp::exception();
my $emit-handler := $state.emit;
$emit-handler(nqp::getpayload(ex)) if $emit-handler.DEFINITE;
nqp::resume(ex)
}
}
my $done = {
$state.get-and-zero-active();
self!teardown($state);
my $done-handler := $state.done;
$done-handler() if $done-handler.DEFINITE;
}
my $catch = {
my \ex = EXCEPTION(nqp::exception());
$state.get-and-zero-active();
self!teardown($state);
my $quit-handler = $state.quit;
$quit-handler(ex) if $quit-handler;
my &*ADD-WHENEVER := &add-whenever;
my $emitter = -> {
if $state.active {
my \ex := nqp::exception();
my $emit-handler := $state.emit;
$emit-handler(nqp::getpayload(ex)) if $emit-handler.DEFINITE;
nqp::resume(ex)
}
nqp::handle(code(value),
'EMIT', $emitter(),
'DONE', $done(),
'CATCH', $catch(),
'NEXT', 0);
@run-after = $state.awaiter.take-all;
}
my $done = -> {
$state.get-and-zero-active();
self!teardown($state);
my $done-handler := $state.done;
$done-handler() if $done-handler.DEFINITE;
}
my $catch = -> {
my \ex = EXCEPTION(nqp::exception());
$state.get-and-zero-active();
self!teardown($state);
my $quit-handler = $state.quit;
$quit-handler(ex) if $quit-handler;
}
$state.active > 0 and nqp::handle(code(value),
'EMIT', $emitter(),
'DONE', $done(),
'CATCH', $catch(),
'NEXT', 0);
@run-after = $state.awaiter.take-all;
}
if $queued.defined {
$queued.then({ self!run-add-whenever-awaits(@run-after) });
Expand Down

0 comments on commit f04b1d7

Please sign in to comment.