Skip to content

Commit

Permalink
Tune Lock::Async code stucture
Browse files Browse the repository at this point in the history
Smaller private methods allow common-case paths to be inlined. This
code structure also needs less closure clones, by moving the dynamic
var declarations into the signature of a private. As a further bonus,
factoring out a little repetition makes the code slightly shorter.
Shaves 4 GC runs off `my $s = supply emit 42; for ^10_000 { react
whenever $s {  } }` due to the reduced allocations, and gives a
speedup.
  • Loading branch information
jnthn committed Jan 15, 2018
1 parent b6a236b commit f36a1f4
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/core/Lock/Async.pm
Original file line number Diff line number Diff line change
Expand Up @@ -161,46 +161,45 @@ my class Lock::Async {

method !on-recursion-list() {
my $rec-list := nqp::getlexdyn('$*LOCK-ASYNC-RECURSION-LIST');
if nqp::isnull($rec-list) {
False
}
else {
my int $n = nqp::elems($rec-list);
loop (my int $i = 0; $i < $n; ++$i) {
return True if nqp::eqaddr(nqp::atpos($rec-list, $i), self);
}
False
nqp::isnull($rec-list) ?? False !! self!search-recursion-list($rec-list)
}

method !search-recursion-list(IterationBuffer \rec-list) {
my int $n = nqp::elems(rec-list);
loop (my int $i = 0; $i < $n; ++$i) {
return True if nqp::eqaddr(nqp::atpos(rec-list, $i), self);
}
False
}

method !run-with-updated-recursion-list(&code) {
my $current := nqp::getlexdyn('$*LOCK-ASYNC-RECURSION-LIST');
my $new-held := nqp::isnull($current) ?? nqp::list() !! nqp::clone($current);
my $new-held := nqp::isnull($current)
?? nqp::create(IterationBuffer)
!! nqp::clone($current);
nqp::push($new-held, self);
{
my $*LOCK-ASYNC-RECURSION-LIST := $new-held;
code();
}
self!run-under-recursion-list($new-held, &code);
}

method with-lock-hidden-from-recursion-check(&code) {
my $current := nqp::getlexdyn('$*LOCK-ASYNC-RECURSION-LIST');
my $new-held;
if nqp::isnull($current) {
$new-held := nqp::null();
}
else {
$new-held := nqp::list();
my int $n = nqp::elems($current);
loop (my int $i = 0; $i < $n; ++$i) {
my $lock := nqp::atpos($current, $i);
nqp::push($new-held, $lock) unless nqp::eqaddr($lock, self);
}
}
{
my $*LOCK-ASYNC-RECURSION-LIST := $new-held;
code();
nqp::isnull($current)
?? code()
!! self!hidden-in-recursion-list($current, &code)
}

method !hidden-in-recursion-list(IterationBuffer \current, &code) {
my $new-held := nqp::create(IterationBuffer);
my int $n = nqp::elems(current);
loop (my int $i = 0; $i < $n; ++$i) {
my $lock := nqp::atpos(current, $i);
nqp::push($new-held, $lock) unless nqp::eqaddr($lock, self);
}
self!run-under-recursion-list($new-held, &code);
}

method !run-under-recursion-list(IterationBuffer $*LOCK-ASYNC-RECURSION-LIST, &code) {
code()
}
}

Expand Down

0 comments on commit f36a1f4

Please sign in to comment.