All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] lib: remove remaining likely/unlikely branch annotations
@ 2020-12-04 11:43 Philippe Gerum
  2020-12-04 11:43 ` [PATCH 2/2] boilerplate: drop likely/unlikely annotations Philippe Gerum
  0 siblings, 1 reply; 3+ messages in thread
From: Philippe Gerum @ 2020-12-04 11:43 UTC (permalink / raw)
  To: xenomai

From: Philippe Gerum <rpm@xenomai.org>

In user-space at least, we'd be better off trusting the CPU's branch
predictor, instead of relying on our limited perception when it comes
to determining the likeliness of a condition, or every compiler to do
the right thing with respect to efficient branching.

We only have a few unlikely predictions in-tree on straightforward
conditions, which we can remove safely:

- POSIX condvars wait/signal loops on x86, arm and arm64 showed no
  observable performance penalty.

- other callers from the thread cancellation path, or debug
  instrumentation are slow paths in essence anyway.

Signed-off-by: Philippe Gerum <rpm@xenomai.org>
---
 include/boilerplate/debug.h | 4 ++--
 lib/cobalt/cond.c           | 4 ++--
 lib/cobalt/mutex.c          | 4 ++--
 lib/copperplate/threadobj.c | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/boilerplate/debug.h b/include/boilerplate/debug.h
index f3eb68749..d3ab0ef80 100644
--- a/include/boilerplate/debug.h
+++ b/include/boilerplate/debug.h
@@ -76,7 +76,7 @@ void debug_init(void);
 #define __bt(__exp)						\
 	({							\
 		typeof(__exp) __ret = (__exp);			\
-		if (unlikely(__ret < 0))			\
+		if (__ret < 0)					\
 			backtrace_log((int)__ret, __FUNCTION__,	\
 				      __FILE__, __LINE__);	\
 		__ret;						\
@@ -85,7 +85,7 @@ void debug_init(void);
 #define __bterrno(__exp)					\
 	({							\
 		typeof(__exp) __ret = (__exp);			\
-		if (unlikely(__ret < 0))			\
+		if (__ret < 0)					\
 			backtrace_log(-errno, __FUNCTION__,	\
 				      __FILE__, __LINE__);	\
 		__ret;						\
diff --git a/lib/cobalt/cond.c b/lib/cobalt/cond.c
index e66b20922..1bf5c74b3 100644
--- a/lib/cobalt/cond.c
+++ b/lib/cobalt/cond.c
@@ -167,7 +167,7 @@ static int __attribute__((cold))
 
 static inline int cobalt_cond_autoinit(union cobalt_cond_union *ucond)
 {
-	if (unlikely(ucond->shadow_cond.magic != COBALT_COND_MAGIC))
+	if (ucond->shadow_cond.magic != COBALT_COND_MAGIC)
 		return cobalt_cond_doautoinit(ucond);
 	return 0;
 }
@@ -201,7 +201,7 @@ COBALT_IMPL(int, pthread_cond_destroy, (pthread_cond_t *cond))
 	struct cobalt_cond_shadow *_cond =
 		&((union cobalt_cond_union *)cond)->shadow_cond;
 
-	if (unlikely(_cond->magic != COBALT_COND_MAGIC))
+	if (_cond->magic != COBALT_COND_MAGIC)
 		return (cobalt_cond_autoinit_type(cond) < 0) ? EINVAL : 0;
 
 	return -XENOMAI_SYSCALL1( sc_cobalt_cond_destroy, _cond);
diff --git a/lib/cobalt/mutex.c b/lib/cobalt/mutex.c
index ad0ed1eba..29236c75a 100644
--- a/lib/cobalt/mutex.c
+++ b/lib/cobalt/mutex.c
@@ -240,7 +240,7 @@ static int __attribute__((cold))
 
 static inline int cobalt_mutex_autoinit(union cobalt_mutex_union *umutex)
 {
-	if (unlikely(umutex->shadow_mutex.magic != COBALT_MUTEX_MAGIC))
+	if (umutex->shadow_mutex.magic != COBALT_MUTEX_MAGIC)
 		return cobalt_mutex_doautoinit(umutex);
 	return 0;
 }
@@ -273,7 +273,7 @@ COBALT_IMPL(int, pthread_mutex_destroy, (pthread_mutex_t *mutex))
 		&((union cobalt_mutex_union *)mutex)->shadow_mutex;
 	int err;
 
-	if (unlikely(_mutex->magic != COBALT_MUTEX_MAGIC))
+	if (_mutex->magic != COBALT_MUTEX_MAGIC)
 		return (cobalt_mutex_autoinit_type(mutex) < 0) ? EINVAL : 0;
 
 	err = XENOMAI_SYSCALL1(sc_cobalt_mutex_destroy, _mutex);
diff --git a/lib/copperplate/threadobj.c b/lib/copperplate/threadobj.c
index 8e7c14a64..a3101baa1 100644
--- a/lib/copperplate/threadobj.c
+++ b/lib/copperplate/threadobj.c
@@ -1022,7 +1022,7 @@ static int request_setschedparam(struct threadobj *thobj, int policy,
 #ifdef CONFIG_XENO_PSHARED
 	struct remote_request *rq;
 
-	if (unlikely(!threadobj_local_p(thobj))) {
+	if (!threadobj_local_p(thobj)) {
 		rq = xnmalloc(sizeof(*rq));
 		if (rq == NULL)
 			return -ENOMEM;
@@ -1065,7 +1065,7 @@ static int request_cancel(struct threadobj *thobj) /* thobj->lock held, dropped.
 	struct remote_request *rq;
 	int ret;
 
-	if (unlikely(!threadobj_local_p(thobj))) {
+	if (!threadobj_local_p(thobj)) {
 		threadobj_unlock(thobj);
 		rq = xnmalloc(sizeof(*rq));
 		if (rq == NULL)
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] boilerplate: drop likely/unlikely annotations
  2020-12-04 11:43 [PATCH 1/2] lib: remove remaining likely/unlikely branch annotations Philippe Gerum
@ 2020-12-04 11:43 ` Philippe Gerum
  2020-12-11  6:43   ` Jan Kiszka
  0 siblings, 1 reply; 3+ messages in thread
From: Philippe Gerum @ 2020-12-04 11:43 UTC (permalink / raw)
  To: xenomai

From: Philippe Gerum <rpm@xenomai.org>

We have no more in-tree users of these. Besides, let's assume that the
CPU's branch predictor always has better clues than we might have when
assessing the likeliness of a condition.

Bonus: this clears a recurring source of namespace clashes with C++
frameworks like Boost.

Signed-off-by: Philippe Gerum <rpm@xenomai.org>
---
 include/boilerplate/compiler.h | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/include/boilerplate/compiler.h b/include/boilerplate/compiler.h
index 94b9f8cad..263af6b0c 100644
--- a/include/boilerplate/compiler.h
+++ b/include/boilerplate/compiler.h
@@ -29,11 +29,6 @@
 #define __stringify_1(x...)	#x
 #define __stringify(x...)	__stringify_1(x)
 
-#ifndef likely
-#define likely(x)	__builtin_expect(!!(x), 1)
-#define unlikely(x)	__builtin_expect(!!(x), 0)
-#endif
-
 #ifndef __noreturn
 #define __noreturn	__attribute__((__noreturn__))
 #endif
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] boilerplate: drop likely/unlikely annotations
  2020-12-04 11:43 ` [PATCH 2/2] boilerplate: drop likely/unlikely annotations Philippe Gerum
@ 2020-12-11  6:43   ` Jan Kiszka
  0 siblings, 0 replies; 3+ messages in thread
From: Jan Kiszka @ 2020-12-11  6:43 UTC (permalink / raw)
  To: Philippe Gerum, xenomai

On 04.12.20 12:43, Philippe Gerum wrote:
> From: Philippe Gerum <rpm@xenomai.org>
> 
> We have no more in-tree users of these. Besides, let's assume that the
> CPU's branch predictor always has better clues than we might have when
> assessing the likeliness of a condition.
> 
> Bonus: this clears a recurring source of namespace clashes with C++
> frameworks like Boost.
> 
> Signed-off-by: Philippe Gerum <rpm@xenomai.org>
> ---
>  include/boilerplate/compiler.h | 5 -----
>  1 file changed, 5 deletions(-)
> 
> diff --git a/include/boilerplate/compiler.h b/include/boilerplate/compiler.h
> index 94b9f8cad..263af6b0c 100644
> --- a/include/boilerplate/compiler.h
> +++ b/include/boilerplate/compiler.h
> @@ -29,11 +29,6 @@
>  #define __stringify_1(x...)	#x
>  #define __stringify(x...)	__stringify_1(x)
>  
> -#ifndef likely
> -#define likely(x)	__builtin_expect(!!(x), 1)
> -#define unlikely(x)	__builtin_expect(!!(x), 0)
> -#endif
> -
>  #ifndef __noreturn
>  #define __noreturn	__attribute__((__noreturn__))
>  #endif
> 

Thanks, both applied.

Jan

-- 
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-11  6:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-04 11:43 [PATCH 1/2] lib: remove remaining likely/unlikely branch annotations Philippe Gerum
2020-12-04 11:43 ` [PATCH 2/2] boilerplate: drop likely/unlikely annotations Philippe Gerum
2020-12-11  6:43   ` Jan Kiszka

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.