[hackers] [libgrapheme] Optimize bsearch-comparison-function for cp-ranges || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Tue, 14 Dec 2021 13:44:16 +0100 (CET)

commit e57131a3dfd4e73747538a27ec472f9803e09803
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Tue Dec 14 13:41:58 2021 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
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 <dev_AT_frign.de>

diff --git a/src/util.c b/src/util.c
index a46cece..de3cbad 100644
--- a/src/util.c
+++ b/src/util.c
_AT_@ -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
Received on Tue Dec 14 2021 - 13:44:16 CET

This archive was generated by hypermail 2.3.0 : Tue Dec 14 2021 - 13:48:33 CET