[hackers] [libgrapheme] Mark likely branches || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Tue, 4 Jan 2022 19:05:37 +0100 (CET)

commit e917e53a05de7cab2591d6b2fa3f2ce5ece89bcf
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Tue Jan 4 18:56:38 2022 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Tue Jan 4 18:56:38 2022 +0100

    Mark likely branches
    
    The likely() and unlikely() macros (known from the Linux kernel) in
    src/util.h are defined in a portable way.
    
    This addition brings a performance improvment of around 6%, which also
    shows how well-optimized grapheme_is_character_break() already is. One
    break check for two codepoints now takes only around 10┬Ás on average on
    my machine, which is insanely fast when you consider the complexity of
    the algorithm behind it.
    
    Signed-off-by: Laslo Hunhold <dev_AT_frign.de>

diff --git a/src/character.c b/src/character.c
index 80a9a79..27e1cf0 100644
--- a/src/character.c
+++ b/src/character.c
_AT_@ -105,10 +105,10 @@ static const uint_least16_t dont_break_gb12_13[2 * NUM_BREAK_PROPS] = {
 static enum break_property
 get_break_prop(uint_least32_t cp)
 {
- if (cp > 0x10FFFF) {
- return BREAK_PROP_OTHER;
- } else {
+ if (likely(cp <= 0x10FFFF)) {
                 return prop[minor[major[cp >> 8] + (cp & 0xff)]].break_property;
+ } else {
+ return BREAK_PROP_OTHER;
         }
 }
 
_AT_@ -118,11 +118,11 @@ grapheme_is_character_break(uint_least32_t cp0, uint_least32_t cp1, GRAPHEME_STA
         enum break_property cp0_prop, cp1_prop;
         bool notbreak = false;
 
- if (state) {
- if (!state->prop_set) {
- cp0_prop = get_break_prop(cp0);
- } else {
+ if (likely(state)) {
+ if (likely(state->prop_set)) {
                         cp0_prop = state->prop;
+ } else {
+ cp0_prop = get_break_prop(cp0);
                 }
                 cp1_prop = get_break_prop(cp1);
 
_AT_@ -153,7 +153,7 @@ grapheme_is_character_break(uint_least32_t cp0, uint_least32_t cp1, GRAPHEME_STA
                             UINT16_C(1 << cp1_prop));
 
                 /* update or reset flags (when we have a break) */
- if (!notbreak) {
+ if (likely(!notbreak)) {
                         state->gb11_flag = state->gb12_13_flag = false;
                 }
         } else {
diff --git a/src/util.h b/src/util.h
index 049af2f..b61a026 100644
--- a/src/util.h
+++ b/src/util.h
_AT_@ -10,4 +10,19 @@
 
 #define LEN(x) (sizeof(x) / sizeof(*(x)))
 
+#undef likely
+#undef unlikely
+#ifdef __has_builtin
+ #if __has_builtin(__builtin_expect)
+ #define likely(expr) __builtin_expect(!!(expr), 1)
+ #define unlikely(expr) __builtin_expect(!!(expr), 0)
+ #else
+ #define likely(expr) (expr)
+ #define unlikely(expr) (expr)
+ #endif
+#else
+ #define likely(expr) (expr)
+ #define unlikely(expr) (expr)
+#endif
+
 #endif /* UTIL_H */
Received on Tue Jan 04 2022 - 19:05:37 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 04 2022 - 19:12:32 CET