libgrapheme

Freestanding C library for unicode string handling
git clone https://git.sinitax.com/suckless/libgrapheme
Log | Files | Refs | README | LICENSE | sfeed.txt

commit e57131a3dfd4e73747538a27ec472f9803e09803
parent cc64e10596e638abf7689130ebee5ae28455f8f4
Author: Laslo Hunhold <dev@frign.de>
Date:   Tue, 14 Dec 2021 13:41:58 +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@frign.de>

Diffstat:
Msrc/util.c | 10++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git 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