Skip to content

Commit

Permalink
Improve performance of muting/unmuting logic (#3287)
Browse files Browse the repository at this point in the history
This commit changes some of the muting/unmuting logic to improve
performance. It combines some atomic load/store pairs with single
add/sub atomic operations.
  • Loading branch information
dipinhora authored and SeanTAllen committed Aug 31, 2019
1 parent 4640a84 commit 4a33b2a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/libponyrt/actor/actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,17 +868,15 @@ bool ponyint_triggers_muting(pony_actor_t* actor)

bool ponyint_is_muted(pony_actor_t* actor)
{
return (atomic_fetch_add_explicit(&actor->is_muted, 0, memory_order_relaxed) > 0);
return (atomic_load_explicit(&actor->is_muted, memory_order_relaxed) > 0);
}

void ponyint_mute_actor(pony_actor_t* actor)
{
uint8_t is_muted = atomic_load_explicit(&actor->is_muted, memory_order_relaxed);
uint8_t is_muted = atomic_fetch_add_explicit(&actor->is_muted, 1, memory_order_relaxed);
pony_assert(is_muted == 0);
DTRACE1(ACTOR_MUTED, (uintptr_t)actor);
is_muted++;
atomic_store_explicit(&actor->is_muted, is_muted, memory_order_relaxed);

(void)is_muted;
}

void ponyint_unmute_actor(pony_actor_t* actor)
Expand Down
12 changes: 5 additions & 7 deletions src/libponyrt/sched/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1292,9 +1292,7 @@ void ponyint_sched_mute(pony_ctx_t* ctx, pony_actor_t* sender, pony_actor_t* rec
// This is safe because an actor can only ever be in a single scheduler's
// mutemap
ponyint_muteset_putindex(&mref->value, sender, index2);
size_t muted = atomic_load_explicit(&sender->muted, memory_order_relaxed);
muted++;
atomic_store_explicit(&sender->muted, muted, memory_order_relaxed);
atomic_fetch_add_explicit(&sender->muted, 1, memory_order_relaxed);
}
}

Expand Down Expand Up @@ -1327,12 +1325,12 @@ bool ponyint_sched_unmute_senders(pony_ctx_t* ctx, pony_actor_t* actor)
{
// This is safe because an actor can only ever be in a single scheduler's
// mutemap
size_t muted_count = atomic_load_explicit(&muted->muted, memory_order_relaxed);
size_t muted_count = atomic_fetch_sub_explicit(&muted->muted, 1, memory_order_relaxed);
pony_assert(muted_count > 0);
muted_count--;
atomic_store_explicit(&muted->muted, muted_count, memory_order_relaxed);

if (muted_count == 0)
// If muted_count used to be 1 before we decremented it; then the actor
// is longer muted
if(muted_count == 1)
{
needs_unmuting = ponyint_actorstack_push(needs_unmuting, muted);
}
Expand Down

0 comments on commit 4a33b2a

Please sign in to comment.