sprinkly SPA_LIKELY/UNLIKELY around

This commit is contained in:
Wim Taymans 2020-03-16 12:52:28 +01:00
parent d762d57665
commit cb7bfdf98a
10 changed files with 118 additions and 129 deletions

View file

@ -546,7 +546,7 @@ static int alsa_recover(struct state *state, int err)
snd_pcm_status_t *status;
snd_pcm_status_alloca(&status);
if ((res = snd_pcm_status(state->hndl, status)) < 0) {
if (SPA_UNLIKELY((res = snd_pcm_status(state->hndl, status)) < 0)) {
spa_log_error(state->log, NAME" %p: snd_pcm_status error: %s",
state, snd_strerror(res));
return res;
@ -581,7 +581,7 @@ static int alsa_recover(struct state *state, int err)
break;
}
if ((res = snd_pcm_recover(state->hndl, err, true)) < 0) {
if (SPA_UNLIKELY((res = snd_pcm_recover(state->hndl, err, true)) < 0)) {
spa_log_error(state->log, NAME" %p: snd_pcm_recover error: %s",
state, snd_strerror(res));
return res;
@ -609,7 +609,7 @@ static int get_status(struct state *state, snd_pcm_uframes_t *delay, snd_pcm_ufr
snd_pcm_sframes_t avail;
int res;
if ((avail = snd_pcm_avail(state->hndl)) < 0) {
if (SPA_UNLIKELY((avail = snd_pcm_avail(state->hndl)) < 0)) {
if ((res = alsa_recover(state, avail)) < 0)
return res;
if ((avail = snd_pcm_avail(state->hndl)) < 0) {
@ -659,7 +659,7 @@ static int update_time(struct state *state, uint64_t nsec, snd_pcm_sframes_t del
else
err = (target + 128) - delay;
if (state->bw == 0.0) {
if (SPA_UNLIKELY(state->bw == 0.0)) {
set_loop(state, BW_MAX);
state->next_time = nsec;
state->base_time = nsec;
@ -670,7 +670,7 @@ static int update_time(struct state *state, uint64_t nsec, snd_pcm_sframes_t del
corr = 1.0 - (state->z2 + state->z3);
if (state->last_threshold != state->threshold) {
if (SPA_UNLIKELY(state->last_threshold != state->threshold)) {
int32_t diff = (int32_t) (state->last_threshold - state->threshold);
spa_log_trace(state->log, NAME" %p: follower:%d quantum change %d",
state, follower, diff);
@ -678,7 +678,7 @@ static int update_time(struct state *state, uint64_t nsec, snd_pcm_sframes_t del
state->last_threshold = state->threshold;
}
if ((state->next_time - state->base_time) > BW_PERIOD) {
if (SPA_UNLIKELY((state->next_time - state->base_time) > BW_PERIOD)) {
state->base_time = state->next_time;
if (state->bw == BW_MAX)
set_loop(state, BW_MED);
@ -701,7 +701,7 @@ static int update_time(struct state *state, uint64_t nsec, snd_pcm_sframes_t del
state->next_time += state->threshold / corr * 1e9 / state->rate;
if (!follower && state->clock) {
if (SPA_LIKELY(!follower && state->clock)) {
state->clock->nsec = nsec;
state->clock->position += state->duration;
state->clock->duration = state->duration;
@ -724,7 +724,7 @@ int spa_alsa_write(struct state *state, snd_pcm_uframes_t silence)
snd_pcm_uframes_t written, frames, offset, off, to_write, total_written;
int res;
if (state->position && state->duration != state->position->clock.duration) {
if (SPA_LIKELY(state->position && state->duration != state->position->clock.duration)) {
state->duration = state->position->clock.duration;
state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom;
}
@ -733,16 +733,16 @@ int spa_alsa_write(struct state *state, snd_pcm_uframes_t silence)
uint64_t nsec;
snd_pcm_uframes_t delay, target;
if ((res = get_status(state, &delay, &target)) < 0)
if (SPA_UNLIKELY((res = get_status(state, &delay, &target)) < 0))
return res;
if (!state->alsa_recovering && delay > target + state->threshold) {
if (SPA_UNLIKELY(!state->alsa_recovering && delay > target + state->threshold)) {
spa_log_warn(state->log, NAME" %p: follower delay:%ld resync %f %f %f",
state, delay, state->z1, state->z2, state->z3);
init_loop(state);
state->alsa_sync = true;
}
if (state->alsa_sync) {
if (SPA_UNLIKELY(state->alsa_sync)) {
if (delay > target)
snd_pcm_rewind(state->hndl, delay - target);
else
@ -753,14 +753,14 @@ int spa_alsa_write(struct state *state, snd_pcm_uframes_t silence)
}
nsec = state->position->clock.nsec;
if ((res = update_time(state, nsec, delay, target, true)) < 0)
if (SPA_UNLIKELY((res = update_time(state, nsec, delay, target, true)) < 0))
return res;
}
total_written = 0;
again:
frames = state->buffer_frames;
if ((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &frames)) < 0) {
if (SPA_UNLIKELY((res = snd_pcm_mmap_begin(hndl, &my_areas, &offset, &frames)) < 0)) {
spa_log_error(state->log, NAME" %p: snd_pcm_mmap_begin error: %s",
state, snd_strerror(res));
return res;
@ -801,7 +801,7 @@ again:
l1 = n_bytes - l0;
spa_memcpy(dst, src + offs, l0);
if (l1 > 0)
if (SPA_UNLIKELY(l1 > 0))
spa_memcpy(dst + l0, src, l1);
state->ready_offset += n_bytes;
@ -825,7 +825,7 @@ again:
silence = 0;
}
if (silence > 0) {
if (SPA_UNLIKELY(silence > 0)) {
spa_log_trace_fp(state->log, NAME" %p: silence %ld", state, silence);
snd_pcm_areas_silence(my_areas, off, state->channels, silence, state->format);
written += silence;
@ -835,7 +835,7 @@ again:
state, offset, written, state->sample_count);
total_written += written;
if ((res = snd_pcm_mmap_commit(hndl, offset, written)) < 0) {
if (SPA_UNLIKELY((res = snd_pcm_mmap_commit(hndl, offset, written)) < 0)) {
spa_log_error(state->log, NAME" %p: snd_pcm_mmap_commit error: %s",
state, snd_strerror(res));
if (res != -EPIPE && res != -ESTRPIPE)
@ -847,7 +847,7 @@ again:
state->sample_count += total_written;
if (!state->alsa_started && total_written > 0) {
if (SPA_UNLIKELY(!state->alsa_started && total_written > 0)) {
spa_log_trace(state->log, NAME" %p: snd_pcm_start %lu", state, written);
if ((res = snd_pcm_start(hndl)) < 0) {
spa_log_error(state->log, NAME" %p: snd_pcm_start: %s",
@ -1012,13 +1012,13 @@ static int handle_play(struct state *state, uint64_t nsec,
{
int res;
if (delay > target + state->last_threshold) {
if (SPA_UNLIKELY(delay > target + state->last_threshold)) {
spa_log_trace(state->log, NAME" %p: early wakeup %ld %ld", state, delay, target);
state->next_time = nsec + (delay - target) * SPA_NSEC_PER_SEC / state->rate;
return -EAGAIN;
}
if ((res = update_time(state, nsec, delay, target, false)) < 0)
if (SPA_UNLIKELY((res = update_time(state, nsec, delay, target, false)) < 0))
return res;
if (spa_list_is_empty(&state->ready)) {
@ -1076,15 +1076,15 @@ static void alsa_on_timeout_event(struct spa_source *source)
uint64_t expire;
int res;
if (state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0)
if (SPA_UNLIKELY(state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0))
spa_log_warn(state->log, NAME" %p: error reading timerfd: %m", state);
if (state->position) {
if (SPA_LIKELY(state->position)) {
state->duration = state->position->clock.duration;
state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom;
}
if ((res = get_status(state, &delay, &target)) < 0)
if (SPA_UNLIKELY((res = get_status(state, &delay, &target)) < 0))
return;
state->current_time = state->next_time;

View file

@ -1074,15 +1074,15 @@ static int impl_node_process(void *object)
r = spa_node_process(this->nodes[i]);
spa_log_trace_fp(this->log, NAME " %p: process %d %d: %s",
this, i, r, r < 0 ? spa_strerror(r) : "ok");
if (r < 0)
if (SPA_UNLIKELY(r < 0))
return r;
if (r & SPA_STATUS_HAVE_DATA)
ready++;
if (i == 0)
if (SPA_UNLIKELY(i == 0))
res |= r & SPA_STATUS_NEED_DATA;
if (i == this->n_nodes-1)
if (SPA_UNLIKELY(i == this->n_nodes-1))
res |= r & SPA_STATUS_HAVE_DATA;
}
if (res & SPA_STATUS_HAVE_DATA)

View file

@ -37,7 +37,7 @@ conv_s16_to_f32d_1s_sse2(void *data, void * SPA_RESTRICT dst[], const void * SPA
__m128i in;
__m128 out, factor = _mm_set1_ps(1.0f / S16_SCALE);
if (SPA_IS_ALIGNED(d0, 16))
if (SPA_LIKELY(SPA_IS_ALIGNED(d0, 16)))
unrolled = n_samples & ~3;
else
unrolled = 0;

View file

@ -37,8 +37,8 @@ static inline void mix_2(float * dst, const float * SPA_RESTRICT src, uint32_t n
uint32_t n, unrolled;
__m128 in1[4], in2[4];
if (SPA_IS_ALIGNED(src, 16) &&
SPA_IS_ALIGNED(dst, 16))
if (SPA_LIKELY(SPA_IS_ALIGNED(src, 16) &&
SPA_IS_ALIGNED(dst, 16)))
unrolled = n_samples & ~15;
else
unrolled = 0;

View file

@ -666,11 +666,11 @@ static int impl_node_process(void *object)
spa_log_trace_fp(this->log, NAME " %p: status %p %d %d",
this, outio, outio->status, outio->buffer_id);
if (outio->status == SPA_STATUS_HAVE_DATA)
if (SPA_UNLIKELY(outio->status == SPA_STATUS_HAVE_DATA))
return outio->status;
/* recycle */
if (outio->buffer_id < outport->n_buffers) {
if (SPA_LIKELY(outio->buffer_id < outport->n_buffers)) {
queue_buffer(this, outport, &outport->buffers[outio->buffer_id]);
outio->buffer_id = SPA_ID_INVALID;
}
@ -686,10 +686,10 @@ static int impl_node_process(void *object)
struct spa_io_buffers *inio = NULL;
struct buffer *inb;
if (!inport->valid ||
if (SPA_UNLIKELY(!inport->valid ||
(inio = inport->io) == NULL ||
inio->buffer_id >= inport->n_buffers ||
inio->status != SPA_STATUS_HAVE_DATA) {
inio->status != SPA_STATUS_HAVE_DATA)) {
spa_log_trace_fp(this->log, NAME " %p: skip input idx:%d valid:%d "
"io:%p status:%d buf_id:%d n_buffers:%d", this,
i, inport->valid, inio,
@ -711,7 +711,7 @@ static int impl_node_process(void *object)
}
outb = dequeue_buffer(this, outport);
if (outb == NULL) {
if (SPA_UNLIKELY(outb == NULL)) {
spa_log_trace(this->log, NAME " %p: out of buffers", this);
return -EPIPE;
}

View file

@ -297,10 +297,11 @@ static int loop_iterate(void *object, int timeout)
}
for (i = 0; i < nfds; i++) {
struct spa_source *s = ep[i].data;
if (s->rmask && s->fd != -1 && s->loop == loop)
if (SPA_LIKELY(s->rmask && s->fd != -1 && s->loop == loop))
s->func(s);
}
process_destroy(impl);
if (SPA_UNLIKELY(!spa_list_is_empty(&impl->destroy_list)))
process_destroy(impl);
return nfds;
}
@ -478,7 +479,7 @@ static int loop_signal_event(void *object, struct spa_source *source)
struct source_impl *impl = SPA_CONTAINER_OF(source, struct source_impl, source);
int res;
if ((res = spa_system_eventfd_write(impl->impl->system, source->fd, 1)) < 0)
if (SPA_UNLIKELY((res = spa_system_eventfd_write(impl->impl->system, source->fd, 1)) < 0))
spa_log_warn(impl->impl->log, NAME " %p: failed to write event fd %d: %s",
source, source->fd, spa_strerror(res));
return res;
@ -490,8 +491,8 @@ static void source_timer_func(struct spa_source *source)
uint64_t expirations;
int res;
if ((res = spa_system_timerfd_read(impl->impl->system,
source->fd, &expirations)) < 0)
if (SPA_UNLIKELY((res = spa_system_timerfd_read(impl->impl->system,
source->fd, &expirations)) < 0))
spa_log_warn(impl->impl->log, NAME " %p: failed to read timer fd %d: %s",
source, source->fd, spa_strerror(res));
@ -547,18 +548,18 @@ loop_update_timer(void *object, struct spa_source *source,
int flags = 0, res;
spa_zero(its);
if (value) {
if (SPA_LIKELY(value)) {
its.it_value = *value;
} else if (interval) {
its.it_value = *interval;
absolute = true;
}
if (interval)
if (SPA_UNLIKELY(interval))
its.it_interval = *interval;
if (absolute)
if (SPA_LIKELY(absolute))
flags |= SPA_FD_TIMER_ABSTIME;
if ((res = spa_system_timerfd_settime(impl->system, source->fd, flags, &its, NULL)) < 0)
if (SPA_UNLIKELY((res = spa_system_timerfd_settime(impl->system, source->fd, flags, &its, NULL)) < 0))
return res;
return 0;