mdbx: using expect_with_probability() macro.

This commit is contained in:
Леонид Юрьев (Leonid Yuriev) 2022-08-04 16:55:37 +03:00
parent c0f8ecd6f2
commit a11c045f1e

View File

@ -1878,14 +1878,14 @@ static int lcklist_detach_locked(MDBX_env *env) {
#define SORT_CMP_SWAP(TYPE, CMP, a, b) \ #define SORT_CMP_SWAP(TYPE, CMP, a, b) \
do { \ do { \
const TYPE swap_tmp = (a); \ const TYPE swap_tmp = (a); \
const bool swap_cmp = CMP(swap_tmp, b); \ const bool swap_cmp = expect_with_probability(CMP(swap_tmp, b), 0, .5); \
(a) = swap_cmp ? swap_tmp : b; \ (a) = swap_cmp ? swap_tmp : b; \
(b) = swap_cmp ? b : swap_tmp; \ (b) = swap_cmp ? b : swap_tmp; \
} while (0) } while (0)
#else #else
#define SORT_CMP_SWAP(TYPE, CMP, a, b) \ #define SORT_CMP_SWAP(TYPE, CMP, a, b) \
do \ do \
if (!CMP(a, b)) { \ if (expect_with_probability(!CMP(a, b), 0, .5)) { \
const TYPE swap_tmp = (a); \ const TYPE swap_tmp = (a); \
(a) = (b); \ (a) = (b); \
(b) = swap_tmp; \ (b) = swap_tmp; \
@ -2138,7 +2138,7 @@ static int lcklist_detach_locked(MDBX_env *env) {
\ \
static __inline bool NAME##_is_sorted(const TYPE *first, const TYPE *last) { \ static __inline bool NAME##_is_sorted(const TYPE *first, const TYPE *last) { \
while (++first <= last) \ while (++first <= last) \
if (CMP(first[0], first[-1])) \ if (expect_with_probability(CMP(first[0], first[-1]), 1, .1)) \
return false; \ return false; \
return true; \ return true; \
} \ } \
@ -2171,9 +2171,9 @@ static int lcklist_detach_locked(MDBX_env *env) {
TYPE *right = hi - 1; \ TYPE *right = hi - 1; \
TYPE *left = lo + 1; \ TYPE *left = lo + 1; \
while (1) { \ while (1) { \
while (CMP(*left, *mid)) \ while (expect_with_probability(CMP(*left, *mid), 0, .5)) \
++left; \ ++left; \
while (CMP(*mid, *right)) \ while (expect_with_probability(CMP(*mid, *right), 0, .5)) \
--right; \ --right; \
if (unlikely(left > right)) { \ if (unlikely(left > right)) { \
if (EXPECT_LOW_CARDINALITY_OR_PRESORTED) { \ if (EXPECT_LOW_CARDINALITY_OR_PRESORTED) { \
@ -2287,24 +2287,24 @@ static int lcklist_detach_locked(MDBX_env *env) {
length >>= 1; \ length >>= 1; \
const TYPE_LIST *const middle = first + length; \ const TYPE_LIST *const middle = first + length; \
const unsigned left = whole - length - 1; \ const unsigned left = whole - length - 1; \
const bool cmp = CMP(*middle, item); \ const bool cmp = expect_with_probability(CMP(*middle, item), 0, .5); \
length = cmp ? left : length; \ length = cmp ? left : length; \
first = cmp ? middle + 1 : first; \ first = cmp ? middle + 1 : first; \
} \ } \
\ \
switch (length) { \ switch (length) { \
case 3: \ case 3: \
if (!CMP(*first, item)) \ if (expect_with_probability(!CMP(*first, item), 0, .5)) \
break; \ break; \
++first; \ ++first; \
__fallthrough /* fall through */; \ __fallthrough /* fall through */; \
case 2: \ case 2: \
if (!CMP(*first, item)) \ if (expect_with_probability(!CMP(*first, item), 0, .5)) \
break; \ break; \
++first; \ ++first; \
__fallthrough /* fall through */; \ __fallthrough /* fall through */; \
case 1: \ case 1: \
if (!CMP(*first, item)) \ if (expect_with_probability(!CMP(*first, item), 0, .5)) \
break; \ break; \
++first; \ ++first; \
__fallthrough /* fall through */; \ __fallthrough /* fall through */; \
@ -2944,7 +2944,7 @@ __hot __noinline static MDBX_dpl *mdbx_dpl_sort_slowpath(const MDBX_txn *txn) {
MDBX_dp *l = dl->items + dl->sorted; MDBX_dp *l = dl->items + dl->sorted;
MDBX_dp *r = end - 1; MDBX_dp *r = end - 1;
do { do {
const bool cmp = l->pgno > r->pgno; const bool cmp = expect_with_probability(l->pgno > r->pgno, 0, .5);
*w = cmp ? *l : *r; *w = cmp ? *l : *r;
l -= cmp; l -= cmp;
r += cmp - 1; r += cmp - 1;