Skip to content
Permalink
Browse files

Micro-optimize Lock::Async.lock/unlock

As these are heavily used in server applications, it seemed worthwhile to
get the 2.5% improvement.  Benchmark based on basic react / whenever listen /
whenever connection.Supply.lines -> $line block getting hammered by a single
client for 100_000 times.
  • Loading branch information...
lizmat committed Feb 18, 2019
1 parent 51f035c commit f56e0756f1bd9abe5df61ef534a6672220906aba
Showing with 6 additions and 6 deletions.
  1. +6 −6 src/core/Lock/Async.pm6
@@ -69,12 +69,12 @@ my class Lock::Async {
my $p := Promise.new;
my $v := $p.vow;
my $holder-update := $holder.queue-vow($v);
if cas($!holder, $holder, $holder-update) =:= $holder {
if nqp::eqaddr(cas($!holder, $holder, $holder-update),$holder) {
return $p;
}
}
else {
if cas($!holder, NO_HOLDER, SINGLE_HOLDER) =:= NO_HOLDER {
if nqp::eqaddr(cas($!holder, NO_HOLDER, SINGLE_HOLDER),NO_HOLDER) {
# Successfully acquired and we're the only holder
return KEPT-PROMISE;
}
@@ -90,9 +90,9 @@ my class Lock::Async {
#?if !js
loop {
my $holder := ⚛$!holder;
if $holder =:= SINGLE_HOLDER {
if nqp::eqaddr($holder,SINGLE_HOLDER) {
# We're the single holder and there's no wait queue.
if cas($!holder, SINGLE_HOLDER, NO_HOLDER) =:= SINGLE_HOLDER {
if nqp::eqaddr(cas($!holder, SINGLE_HOLDER, NO_HOLDER),SINGLE_HOLDER) {
# Successfully released to NO_HOLDER state.
return;
}
@@ -101,7 +101,7 @@ my class Lock::Async {
my int $queue-length = $holder.waiter-queue-length();
my $v := $holder.head-vow;
if $queue-length == 1 {
if cas($!holder, $holder, SINGLE_HOLDER) =:= $holder {
if nqp::eqaddr(cas($!holder, $holder, SINGLE_HOLDER),$holder) {
# Successfully released; keep the head vow, thus
# giving the lock to the next waiter.
$v.keep(True);
@@ -110,7 +110,7 @@ my class Lock::Async {
}
else {
my $new-holder := $holder.without-head-vow();
if cas($!holder, $holder, $new-holder) =:= $holder {
if nqp::eqaddr(cas($!holder, $holder, $new-holder),$holder) {
# Successfully released and installed remaining queue;
# keep the head vow which we successfully removed.
$v.keep(True);

0 comments on commit f56e075

Please sign in to comment.
You can’t perform that action at this time.