Skip to content

Commit

Permalink
performance: Only load the atomic invalidate index when necessary
Browse files Browse the repository at this point in the history
We don't care about the previous value of i if the write has wrapped, so we can only load it if it hasn't.
  • Loading branch information
DNedic committed Mar 21, 2024
1 parent 73a86b8 commit 2b8f722
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lfbb/src/lfbb.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ void LFBB_WriteRelease(LFBB_Inst_Type *inst, const size_t written) {
assert(inst != NULL);
assert(inst->data != NULL);

/* Preload variables with adequate memory ordering */
size_t w = atomic_load_explicit(&inst->w, memory_order_relaxed);
size_t i = atomic_load_explicit(&inst->i, memory_order_relaxed);

/* If the write wrapped set the invalidate index and reset write index*/
size_t i;
if (inst->write_wrapped) {
inst->write_wrapped = false;
i = w;
w = 0U;
} else {
i = atomic_load_explicit(&inst->i, memory_order_relaxed);
}

/* Increment the write index */
Expand Down

0 comments on commit 2b8f722

Please sign in to comment.