__lttng_counter_add: skip effect-less code when global_sum_step=0
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 2 Sep 2022 20:19:06 +0000 (16:19 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 15 Jul 2024 21:01:43 +0000 (17:01 -0400)
Skip effect-less code in the common case where global_sum_step=0.
This saves about 1.5ns on x86-64.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I62e8d163143780e64d7130051b1013470fef48fe

include/counter/counter-api.h

index be372f45c32a89f862eaa358e75a319da9c54d15..ffc5ff7d2da0dec4aae03cd61b5e149fce2acbc7 100644 (file)
@@ -63,11 +63,13 @@ static __always_inline int __lttng_counter_add(const struct lib_counter_config *
                                move_sum = 0;
                                old = res;
                                n = (int8_t) ((uint8_t) old + (uint8_t) v);
-                               if (unlikely(n > (int8_t) global_sum_step))
-                                       move_sum = (int8_t) global_sum_step / 2;
-                               else if (unlikely(n < -(int8_t) global_sum_step))
-                                       move_sum = -((int8_t) global_sum_step / 2);
-                               n -= move_sum;
+                               if (unlikely(global_sum_step)) {
+                                       if (unlikely(n > (int8_t) global_sum_step))
+                                               move_sum = (int8_t) global_sum_step / 2;
+                                       else if (unlikely(n < -(int8_t) global_sum_step))
+                                               move_sum = -((int8_t) global_sum_step / 2);
+                                       n -= move_sum;
+                               }
                                res = cmpxchg_local(int_p, old, n);
                        } while (old != res);
                        break;
@@ -104,11 +106,13 @@ static __always_inline int __lttng_counter_add(const struct lib_counter_config *
                                move_sum = 0;
                                old = res;
                                n = (int16_t) ((uint16_t) old + (uint16_t) v);
-                               if (unlikely(n > (int16_t) global_sum_step))
-                                       move_sum = (int16_t) global_sum_step / 2;
-                               else if (unlikely(n < -(int16_t) global_sum_step))
-                                       move_sum = -((int16_t) global_sum_step / 2);
-                               n -= move_sum;
+                               if (unlikely(global_sum_step)) {
+                                       if (unlikely(n > (int16_t) global_sum_step))
+                                               move_sum = (int16_t) global_sum_step / 2;
+                                       else if (unlikely(n < -(int16_t) global_sum_step))
+                                               move_sum = -((int16_t) global_sum_step / 2);
+                                       n -= move_sum;
+                               }
                                res = cmpxchg_local(int_p, old, n);
                        } while (old != res);
                        break;
@@ -145,11 +149,13 @@ static __always_inline int __lttng_counter_add(const struct lib_counter_config *
                                move_sum = 0;
                                old = res;
                                n = (int32_t) ((uint32_t) old + (uint32_t) v);
-                               if (unlikely(n > (int32_t) global_sum_step))
-                                       move_sum = (int32_t) global_sum_step / 2;
-                               else if (unlikely(n < -(int32_t) global_sum_step))
-                                       move_sum = -((int32_t) global_sum_step / 2);
-                               n -= move_sum;
+                               if (unlikely(global_sum_step)) {
+                                       if (unlikely(n > (int32_t) global_sum_step))
+                                               move_sum = (int32_t) global_sum_step / 2;
+                                       else if (unlikely(n < -(int32_t) global_sum_step))
+                                               move_sum = -((int32_t) global_sum_step / 2);
+                                       n -= move_sum;
+                               }
                                res = cmpxchg_local(int_p, old, n);
                        } while (old != res);
                        break;
@@ -187,11 +193,13 @@ static __always_inline int __lttng_counter_add(const struct lib_counter_config *
                                move_sum = 0;
                                old = res;
                                n = (int64_t) ((uint64_t) old + (uint64_t) v);
-                               if (unlikely(n > (int64_t) global_sum_step))
-                                       move_sum = (int64_t) global_sum_step / 2;
-                               else if (unlikely(n < -(int64_t) global_sum_step))
-                                       move_sum = -((int64_t) global_sum_step / 2);
-                               n -= move_sum;
+                               if (unlikely(global_sum_step)) {
+                                       if (unlikely(n > (int64_t) global_sum_step))
+                                               move_sum = (int64_t) global_sum_step / 2;
+                                       else if (unlikely(n < -(int64_t) global_sum_step))
+                                               move_sum = -((int64_t) global_sum_step / 2);
+                                       n -= move_sum;
+                               }
                                res = cmpxchg_local(int_p, old, n);
                        } while (old != res);
                        break;
This page took 0.026886 seconds and 4 git commands to generate.