Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop calling cnd_timedwait() with a timeout of 0h #1481

Merged
merged 2 commits into from
Nov 8, 2017

Conversation

tbsaunde
Copy link
Contributor

Waiting 0ms to be signaled the queue now contains elements doesn't
really mean anything. However per the POSIX specification of
pthread_cond_timedwait() the lock must be released and reacquired. As a
result if we skip calling cnd_timedwait() when the timeout is 0 we
improve average and best case latency in various conditions by between
0.1 and 0.2ms.

@@ -361,7 +361,8 @@ rd_kafka_op_t *rd_kafka_q_pop_serve (rd_kafka_q_t *rkq, int timeout_ms,

/* No op, wait for one */
pre = rd_clock();
if (cnd_timedwait_ms(&rkq->rkq_cond,
if (timeout_ms == 0 ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simply unlock and return NULL if timeout_ms is 0 instead of trying again, that's what will happen below at the while (timeout_ms != ..NOWAIT) anyway,

Put the check above the pre = .. assignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edenhill, actually that's what this already does, because in the case cnd_timedwait() returns timedout we just unlock and return null. That said we could avoid the call to clock_gettime() which would save us a couple of microseconds while holding a lock. So I'll make this a separate check.

@edenhill
Copy link
Contributor

Nice find :)

 Waiting 0ms to be signaled the queue now contains elements doesn't
really mean anything.  However per the POSIX specification of
pthread_cond_timedwait() the lock must be released and reacquired.  As a
result if we skip calling cnd_timedwait() when the timeout is 0 we
improve average and best case latency in various conditions by between
0.1 and 0.2ms.
@edenhill edenhill merged commit 9640881 into confluentinc:master Nov 8, 2017
akhi3030 pushed a commit to akhi3030/librdkafka that referenced this pull request Nov 13, 2017
…tbsaunde)

* stop calling cnd_timedwait() with a timeout of 0

 Waiting 0ms to be signaled the queue now contains elements doesn't
really mean anything.  However per the POSIX specification of
pthread_cond_timedwait() the lock must be released and reacquired.  As a
result if we skip calling cnd_timedwait() when the timeout is 0 we
improve average and best case latency in various conditions by between
0.1 and 0.2ms.

* fix indent, use define
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants