commit e57131a3dfd4e73747538a27ec472f9803e09803
Author:     Laslo Hunhold <[email protected]>
AuthorDate: Tue Dec 14 13:41:58 2021 +0100
Commit:     Laslo Hunhold <[email protected]>
CommitDate: Tue Dec 14 13:41:58 2021 +0100

    Optimize bsearch-comparison-function for cp-ranges
    
    Instead of having two comparisons all the time, reduce it to one with
    a clever case-shuffling. Also, instead of calculating the "offset"
    of the cp from the range, simply indicate by sign if we're above or
    below (the bsearch() function/binary search algorithm cannot make use
    of the magnitude anyway), which also conveniently avoids possible
    overflows.
    
    This change leads to a speedup of ~6%.
    
    Signed-off-by: Laslo Hunhold <[email protected]>

diff --git a/src/util.c b/src/util.c
index a46cece..de3cbad 100644
--- a/src/util.c
+++ b/src/util.c
@@ -41,10 +41,16 @@ heisenstate_set(struct lg_internal_heisenstate *h, int 
slot, int state)
 static int
 cp_cmp(const void *a, const void *b)
 {
-       uint_least32_t cp = *(const uint_least32_t *)a;
+       const uint_least32_t cp = *(const uint_least32_t *)a;
        const uint_least32_t *range = (const uint_least32_t *)b;
 
-       return (cp >= range[0] && cp <= range[1]) ? 0 : (int)(cp - range[0]);
+       if (cp < range[0]) {
+               return -1;
+       } else if (cp > range[1]) {
+               return 1;
+       } else {
+               return 0;
+       }
 }
 
 int

Reply via email to