mirror of
https://github.com/isar/libmdbx.git
synced 2025-01-06 18:54:14 +08:00
mdbx: some micro-optimizations.
This commit is contained in:
parent
d8f0c9dc44
commit
096d6a9bd6
42
src/core.c
42
src/core.c
@ -1439,7 +1439,7 @@ __cold void mdbx_rthc_global_init(void) {
|
|||||||
__Wpedantic_format_voidptr(&rthc_key), (unsigned)rthc_key);
|
__Wpedantic_format_voidptr(&rthc_key), (unsigned)rthc_key);
|
||||||
#endif
|
#endif
|
||||||
/* checking time conversion, this also avoids racing on 32-bit architectures
|
/* 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;
|
uint32_t proba = UINT32_MAX;
|
||||||
while (true) {
|
while (true) {
|
||||||
unsigned time_conversion_checkup =
|
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 */
|
/* Fast non-lexically comparator */
|
||||||
__hot static int cmp_lenfast(const MDBX_val *a, const MDBX_val *b) {
|
__hot static int cmp_lenfast(const MDBX_val *a, const MDBX_val *b) {
|
||||||
int diff = CMP2INT(a->iov_len, b->iov_len);
|
int diff = CMP2INT(a->iov_len, b->iov_len);
|
||||||
return likely(diff || a->iov_len == 0)
|
return likely(diff) || a->iov_len == 0
|
||||||
? diff
|
? diff
|
||||||
: memcmp(a->iov_base, b->iov_base, a->iov_len);
|
: 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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cr = 0, i = 0;
|
int i;
|
||||||
MDBX_cmp_func *cmp = mc->mc_dbx->md_cmp;
|
MDBX_cmp_func *cmp = mc->mc_dbx->md_cmp;
|
||||||
MDBX_val nodekey;
|
MDBX_val nodekey;
|
||||||
if (unlikely(IS_LEAF2(mp))) {
|
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);
|
nodekey.iov_base = page_leaf2key(mp, i, nodekey.iov_len);
|
||||||
mdbx_cassert(mc, (char *)mp + mc->mc_txn->mt_env->me_psize >=
|
mdbx_cassert(mc, (char *)mp + mc->mc_txn->mt_env->me_psize >=
|
||||||
(char *)nodekey.iov_base + nodekey.iov_len);
|
(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),
|
mdbx_debug("found leaf index %u [%s], rc = %i", i, DKEY_DEBUG(&nodekey),
|
||||||
cr);
|
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;
|
ret.exact = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
low = (cr < 0) ? low : i + 1;
|
|
||||||
high = (cr < 0) ? i - 1 : high;
|
|
||||||
} while (likely(low <= 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 */
|
/* store the key index */
|
||||||
mc->mc_ki[mc->mc_top] = (indx_t)i;
|
mc->mc_ki[mc->mc_top] = (indx_t)i;
|
||||||
ret.node = (i < nkeys)
|
ret.node = (i < nkeys)
|
||||||
@ -13727,32 +13727,30 @@ __hot static struct node_result mdbx_node_search(MDBX_cursor *mc,
|
|||||||
MDBX_node *node;
|
MDBX_node *node;
|
||||||
do {
|
do {
|
||||||
i = (low + high) >> 1;
|
i = (low + high) >> 1;
|
||||||
|
|
||||||
node = page_node(mp, i);
|
node = page_node(mp, i);
|
||||||
nodekey.iov_len = node_ks(node);
|
nodekey.iov_len = node_ks(node);
|
||||||
nodekey.iov_base = node_key(node);
|
nodekey.iov_base = node_key(node);
|
||||||
mdbx_cassert(mc, (char *)mp + mc->mc_txn->mt_env->me_psize >=
|
mdbx_cassert(mc, (char *)mp + mc->mc_txn->mt_env->me_psize >=
|
||||||
(char *)nodekey.iov_base + nodekey.iov_len);
|
(char *)nodekey.iov_base + nodekey.iov_len);
|
||||||
|
int cr = cmp(key, &nodekey);
|
||||||
cr = cmp(key, &nodekey);
|
|
||||||
if (IS_LEAF(mp))
|
if (IS_LEAF(mp))
|
||||||
mdbx_debug("found leaf index %u [%s], rc = %i", i, DKEY_DEBUG(&nodekey),
|
mdbx_debug("found leaf index %u [%s], rc = %i", i, DKEY_DEBUG(&nodekey),
|
||||||
cr);
|
cr);
|
||||||
else
|
else
|
||||||
mdbx_debug("found branch index %u [%s -> %" PRIaPGNO "], rc = %i", i,
|
mdbx_debug("found branch index %u [%s -> %" PRIaPGNO "], rc = %i", i,
|
||||||
DKEY_DEBUG(&nodekey), node_pgno(node), cr);
|
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;
|
ret.exact = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
low = (cr < 0) ? low : i + 1;
|
|
||||||
high = (cr < 0) ? i - 1 : high;
|
|
||||||
} while (likely(low <= 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 */
|
/* store the key index */
|
||||||
mc->mc_ki[mc->mc_top] = (indx_t)i;
|
mc->mc_ki[mc->mc_top] = (indx_t)i;
|
||||||
ret.node = (i < nkeys)
|
ret.node = (i < nkeys)
|
||||||
@ -13956,7 +13954,7 @@ mdbx_page_search_root(MDBX_cursor *mc, const MDBX_val *key, int flags) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const struct node_result nsr = mdbx_node_search(mc, key);
|
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;
|
i = mc->mc_ki[mc->mc_top] + nsr.exact - 1;
|
||||||
else
|
else
|
||||||
i = page_numkeys(mp) - 1;
|
i = page_numkeys(mp) - 1;
|
||||||
|
10
src/osal.c
10
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) {
|
MDBX_INTERNAL_FUNC uint32_t mdbx_osal_monotime_to_16dot16(uint64_t monotime) {
|
||||||
static uint64_t limit;
|
static uint64_t limit;
|
||||||
if (unlikely(monotime > limit)) {
|
if (unlikely(monotime > limit)) {
|
||||||
if (limit != 0)
|
if (likely(limit != 0))
|
||||||
return UINT32_MAX;
|
return UINT32_MAX;
|
||||||
limit = mdbx_osal_16dot16_to_monotime(UINT32_MAX - 1);
|
limit = mdbx_osal_16dot16_to_monotime(UINT32_MAX - 1);
|
||||||
if (monotime > limit)
|
if (unlikely(monotime > limit))
|
||||||
return UINT32_MAX;
|
return UINT32_MAX;
|
||||||
}
|
}
|
||||||
const uint32_t ret =
|
const uint32_t ret =
|
||||||
@ -2048,7 +2048,9 @@ MDBX_INTERNAL_FUNC uint32_t mdbx_osal_monotime_to_16dot16(uint64_t monotime) {
|
|||||||
#else
|
#else
|
||||||
(uint32_t)(monotime * 128 / 1953125);
|
(uint32_t)(monotime * 128 / 1953125);
|
||||||
#endif
|
#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) {
|
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;
|
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);
|
p->y += UINT64_C(64526882297375213);
|
||||||
bootid_shake(p);
|
bootid_shake(p);
|
||||||
for (size_t i = 0; i < n; ++i) {
|
for (size_t i = 0; i < n; ++i) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user