diff --git a/src/core.c b/src/core.c index 14c4be12..25437165 100644 --- a/src/core.c +++ b/src/core.c @@ -1439,7 +1439,7 @@ __cold void mdbx_rthc_global_init(void) { __Wpedantic_format_voidptr(&rthc_key), (unsigned)rthc_key); #endif /* checking time conversion, this also avoids racing on 32-bit architectures - * during writing calculated 64-bit ratio(s) into memory. */ + * during storing calculated 64-bit ratio(s) into memory. */ uint32_t proba = UINT32_MAX; while (true) { unsigned time_conversion_checkup = @@ -13647,7 +13647,7 @@ __hot static int cmp_reverse(const MDBX_val *a, const MDBX_val *b) { /* Fast non-lexically comparator */ __hot static int cmp_lenfast(const MDBX_val *a, const MDBX_val *b) { int diff = CMP2INT(a->iov_len, b->iov_len); - return likely(diff || a->iov_len == 0) + return likely(diff) || a->iov_len == 0 ? diff : memcmp(a->iov_base, b->iov_base, a->iov_len); } @@ -13685,7 +13685,7 @@ __hot static struct node_result mdbx_node_search(MDBX_cursor *mc, return ret; } - int cr = 0, i = 0; + int i; MDBX_cmp_func *cmp = mc->mc_dbx->md_cmp; MDBX_val nodekey; if (unlikely(IS_LEAF2(mp))) { @@ -13696,21 +13696,21 @@ __hot static struct node_result mdbx_node_search(MDBX_cursor *mc, nodekey.iov_base = page_leaf2key(mp, i, nodekey.iov_len); mdbx_cassert(mc, (char *)mp + mc->mc_txn->mt_env->me_psize >= (char *)nodekey.iov_base + nodekey.iov_len); - cr = cmp(key, &nodekey); + int cr = cmp(key, &nodekey); mdbx_debug("found leaf index %u [%s], rc = %i", i, DKEY_DEBUG(&nodekey), cr); - if (unlikely(cr == 0)) { + if (cr > 0) + /* Found entry is less than the key. */ + /* Skip to get the smallest entry larger than key. */ + low = ++i; + else if (cr < 0) + high = i - 1; + else { ret.exact = true; break; } - low = (cr < 0) ? low : i + 1; - high = (cr < 0) ? i - 1 : high; } while (likely(low <= high)); - /* Found entry is less than the key. */ - /* Skip to get the smallest entry larger than key. */ - i += cr > 0; - /* store the key index */ mc->mc_ki[mc->mc_top] = (indx_t)i; ret.node = (i < nkeys) @@ -13727,32 +13727,30 @@ __hot static struct node_result mdbx_node_search(MDBX_cursor *mc, MDBX_node *node; do { i = (low + high) >> 1; - node = page_node(mp, i); nodekey.iov_len = node_ks(node); nodekey.iov_base = node_key(node); mdbx_cassert(mc, (char *)mp + mc->mc_txn->mt_env->me_psize >= (char *)nodekey.iov_base + nodekey.iov_len); - - cr = cmp(key, &nodekey); + int cr = cmp(key, &nodekey); if (IS_LEAF(mp)) mdbx_debug("found leaf index %u [%s], rc = %i", i, DKEY_DEBUG(&nodekey), cr); else mdbx_debug("found branch index %u [%s -> %" PRIaPGNO "], rc = %i", i, DKEY_DEBUG(&nodekey), node_pgno(node), cr); - if (unlikely(cr == 0)) { + if (cr > 0) + /* Found entry is less than the key. */ + /* Skip to get the smallest entry larger than key. */ + low = ++i; + else if (cr < 0) + high = i - 1; + else { ret.exact = true; break; } - low = (cr < 0) ? low : i + 1; - high = (cr < 0) ? i - 1 : high; } while (likely(low <= high)); - /* Found entry is less than the key. */ - /* Skip to get the smallest entry larger than key. */ - i += cr > 0; - /* store the key index */ mc->mc_ki[mc->mc_top] = (indx_t)i; ret.node = (i < nkeys) @@ -13956,7 +13954,7 @@ mdbx_page_search_root(MDBX_cursor *mc, const MDBX_val *key, int flags) { } } else { const struct node_result nsr = mdbx_node_search(mc, key); - if (nsr.node) + if (likely(nsr.node)) i = mc->mc_ki[mc->mc_top] + nsr.exact - 1; else i = page_numkeys(mp) - 1; diff --git a/src/osal.c b/src/osal.c index 719f297e..91a3f8c7 100644 --- a/src/osal.c +++ b/src/osal.c @@ -2034,10 +2034,10 @@ mdbx_osal_16dot16_to_monotime(uint32_t seconds_16dot16) { MDBX_INTERNAL_FUNC uint32_t mdbx_osal_monotime_to_16dot16(uint64_t monotime) { static uint64_t limit; if (unlikely(monotime > limit)) { - if (limit != 0) + if (likely(limit != 0)) return UINT32_MAX; limit = mdbx_osal_16dot16_to_monotime(UINT32_MAX - 1); - if (monotime > limit) + if (unlikely(monotime > limit)) return UINT32_MAX; } const uint32_t ret = @@ -2048,7 +2048,9 @@ MDBX_INTERNAL_FUNC uint32_t mdbx_osal_monotime_to_16dot16(uint64_t monotime) { #else (uint32_t)(monotime * 128 / 1953125); #endif - return likely(ret || monotime == 0) ? ret : /* fix underflow */ 1; + if (likely(ret > 0)) + return ret; + return monotime > 0 /* fix underflow */; } MDBX_INTERNAL_FUNC uint64_t mdbx_osal_monotime(void) { @@ -2091,7 +2093,7 @@ static void bootid_shake(bin128_t *p) { p->d = e + p->a; } -static void bootid_collect(bin128_t *p, const void *s, size_t n) { +__cold static void bootid_collect(bin128_t *p, const void *s, size_t n) { p->y += UINT64_C(64526882297375213); bootid_shake(p); for (size_t i = 0; i < n; ++i) {