mdbx: fix nasty/stupid mistake in cmp-functions.

Change-Id: If9aff5d9cd46dde614ddaa688113be8be12f3e5d
This commit is contained in:
Leo Yuriev 2016-03-29 22:48:57 +03:00
parent f833877083
commit 33e2b52118
2 changed files with 63 additions and 72 deletions

2
lmdb.h
View File

@ -262,7 +262,7 @@ typedef struct MDB_val {
#endif /* MDBX_MODE_ENABLED */ #endif /* MDBX_MODE_ENABLED */
/** @brief A callback function used to compare two keys in a database */ /** @brief A callback function used to compare two keys in a database */
typedef long (MDB_cmp_func)(const MDB_val *a, const MDB_val *b); typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
/** @brief A callback function used to relocate a position-dependent data item /** @brief A callback function used to relocate a position-dependent data item
* in a fixed-address database. * in a fixed-address database.

133
mdb.c
View File

@ -5121,57 +5121,66 @@ mdb_env_close(MDB_env *env)
mdbx_env_close_ex(env, 0); mdbx_env_close_ex(env, 0);
} }
/* LY: fast enough on most arches
*
* /
* | -1, a < b
* cmp2int(a,b) = < 0, a == b
* | 1, a > b
* \
*/
#define mdbx_cmp2int(a, b) (((a) > (b)) - ((b) > (a)))
/** Compare two items pointing at aligned unsigned int's. */ /** Compare two items pointing at aligned unsigned int's. */
static long __hot static int __hot
mdb_cmp_int_ai(const MDB_val *a, const MDB_val *b) mdb_cmp_int_ai(const MDB_val *a, const MDB_val *b)
{ {
mdb_assert(NULL, a->mv_size == b->mv_size); mdb_assert(NULL, a->mv_size == b->mv_size);
mdb_assert(NULL, 0 == (uintptr_t) a->mv_data % sizeof(int) mdb_assert(NULL, 0 == (uintptr_t) a->mv_data % sizeof(int)
&& 0 == (uintptr_t) b->mv_data % sizeof(int)); && 0 == (uintptr_t) b->mv_data % sizeof(int));
if (sizeof(int) != sizeof(long) && likely(a->mv_size == sizeof(long))) if (sizeof(int) != sizeof(size_t) && likely(a->mv_size == sizeof(size_t)))
return *(long *)a->mv_data - *(long *)b->mv_data; return mdbx_cmp2int( *(size_t *)a->mv_data, *(size_t *)b->mv_data );
mdb_assert(NULL, a->mv_size == sizeof(int) ); mdb_assert(NULL, a->mv_size == sizeof(int) );
return *(int *)a->mv_data - *(int *)b->mv_data; return mdbx_cmp2int( *(unsigned *)a->mv_data, *(unsigned *)b->mv_data );
} }
/** Compare two items pointing at 2-byte aligned unsigned int's. */ /** Compare two items pointing at 2-byte aligned unsigned int's. */
static long __hot static int __hot
mdb_cmp_int_a2(const MDB_val *a, const MDB_val *b) mdb_cmp_int_a2(const MDB_val *a, const MDB_val *b)
{ {
mdb_assert(NULL, a->mv_size == b->mv_size); mdb_assert(NULL, a->mv_size == b->mv_size);
mdb_assert(NULL, 0 == a->mv_size % sizeof(short)); mdb_assert(NULL, 0 == (uintptr_t) a->mv_data % sizeof(uint16_t)
mdb_assert(NULL, 0 == (uintptr_t) a->mv_data % sizeof(short) && 0 == (uintptr_t) b->mv_data % sizeof(uint16_t));
&& 0 == (uintptr_t) b->mv_data % sizeof(short));
#ifdef MISALIGNED_OK #ifdef MISALIGNED_OK
if (sizeof(int) != sizeof(long) && likely(a->mv_size == sizeof(long))) if (sizeof(int) != sizeof(size_t) && likely(a->mv_size == sizeof(size_t)))
return *(long *)a->mv_data - *(long *)b->mv_data; return mdbx_cmp2int( *(size_t *)a->mv_data, *(size_t *)b->mv_data );
mdb_assert(NULL, a->mv_size == sizeof(int) ); mdb_assert(NULL, a->mv_size == sizeof(int) );
return *(int *)a->mv_data - *(int *)b->mv_data; return mdbx_cmp2int( *(unsigned *)a->mv_data, *(unsigned *)b->mv_data );
#else #else
mdb_assert(NULL, 0 == a->mv_size % sizeof(uint16_t));
{ {
long x; int diff;
unsigned short *u, *c; const uint16_t *pa, *pb, *end;
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
u = (unsigned short *) ((char *) a->mv_data + a->mv_size); end = (const uint16_t *) a->mv_data;
c = (unsigned short *) ((char *) b->mv_data + a->mv_size); pa = (const uint16_t *) ((char *) a->mv_data + a->mv_size);
pb = (const uint16_t *) ((char *) b->mv_data + a->mv_size);
do { do {
x = *--u - *--c; diff = *--pa - *--pb;
} while(!x && u > (unsigned short *)a->mv_data);
#else /* BYTE_ORDER */ #else /* BYTE_ORDER */
unsigned short *end; end = (const uint16_t *) ((char *) a->mv_data + a->mv_size);
pa = (const uint16_t *) a->mv_data;
end = (unsigned short *) ((char *) a->mv_data + a->mv_size); pb = (const uint16_t *) b->mv_data;
u = (unsigned short *)a->mv_data;
c = (unsigned short *)b->mv_data;
do { do {
x = *u++ - *c++; diff = *pa++ - *pb++;
} while(!x && u < end);
#endif /* BYTE_ORDER */ #endif /* BYTE_ORDER */
return x; if (likely(diff != 0)) break;
} while(pa != end);
return diff;
} }
#endif /* MISALIGNED_OK */ #endif /* MISALIGNED_OK */
} }
@ -5180,82 +5189,64 @@ mdb_cmp_int_a2(const MDB_val *a, const MDB_val *b)
* *
* This is also set as #MDB_INTEGERDUP|#MDB_DUPFIXED's #MDB_dbx.%md_dcmp. * This is also set as #MDB_INTEGERDUP|#MDB_DUPFIXED's #MDB_dbx.%md_dcmp.
*/ */
static long __hot static int __hot
mdb_cmp_int_ua(const MDB_val *a, const MDB_val *b) mdb_cmp_int_ua(const MDB_val *a, const MDB_val *b)
{ {
mdb_assert(NULL, a->mv_size == b->mv_size); mdb_assert(NULL, a->mv_size == b->mv_size);
#if MISALIGNED_OK #if MISALIGNED_OK
if (sizeof(int) != sizeof(long) && likely(a->mv_size == sizeof(long))) if (sizeof(int) != sizeof(size_t) && likely(a->mv_size == sizeof(size_t)))
return *(long *)a->mv_data - *(long *)b->mv_data; return mdbx_cmp2int( *(size_t *)a->mv_data, *(size_t *)b->mv_data );
mdb_assert(NULL, a->mv_size == sizeof(int) ); mdb_assert(NULL, a->mv_size == sizeof(int) );
return *(int *)a->mv_data - *(int *)b->mv_data; return mdbx_cmp2int( *(unsigned *)a->mv_data, *(unsigned *)b->mv_data );
#else #else
mdb_assert(NULL, a->mv_size == sizeof(int) || a->mv_size == sizeof(size_t)); mdb_assert(NULL, a->mv_size == sizeof(int) || a->mv_size == sizeof(size_t));
{
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
const unsigned char *p1, *p2; {
long diff; int diff;
const uint8_t *pa, *pb;
p1 = (const unsigned char *)a->mv_data + a->mv_size; pa = (const uint8_t *)a->mv_data + a->mv_size;
p2 = (const unsigned char *)b->mv_data + a->mv_size; pb = (const uint8_t *)b->mv_data + a->mv_size;
do { do {
diff = *--p1 - *--p2; diff = *--pa - *--pb;
if (diff) if (likely(diff)) break;
return diff; } while(pa != a->mv_data);
} while(p1 != a->mv_data); return diff;
return 0;
} }
#else /* BYTE_ORDER */ #else /* BYTE_ORDER */
return memcmp(a->mv_data, b->mv_data, a->mv_size); return memcmp(a->mv_data, b->mv_data, a->mv_size);
#endif /* BYTE_ORDER */ #endif /* BYTE_ORDER */
#endif /* MISALIGNED_OK */ #endif /* MISALIGNED_OK */
} }
/** Compare two items lexically */ /** Compare two items lexically */
static long __hot static int __hot
mdb_cmp_memn(const MDB_val *a, const MDB_val *b) mdb_cmp_memn(const MDB_val *a, const MDB_val *b)
{ {
long diff; size_t minlen = (a->mv_size < b->mv_size) ? a->mv_size : b->mv_size;
ssize_t len_diff; int diff = memcmp(a->mv_data, b->mv_data, minlen);
unsigned len; return likely(diff) ? diff : mdbx_cmp2int(a->mv_size, b->mv_size);
len = a->mv_size;
len_diff = (ssize_t) a->mv_size - (ssize_t) b->mv_size;
if (len_diff > 0) {
len = b->mv_size;
len_diff = 1;
}
diff = memcmp(a->mv_data, b->mv_data, len);
return diff ? diff : len_diff<0 ? -1 : len_diff;
} }
/** Compare two items in reverse byte order */ /** Compare two items in reverse byte order */
static long __hot static int __hot
mdb_cmp_memnr(const MDB_val *a, const MDB_val *b) mdb_cmp_memnr(const MDB_val *a, const MDB_val *b)
{ {
const unsigned char *p1, *p2, *p1_lim; const uint8_t *pa, *pb, *end;
ssize_t len_diff;
long diff;
p1_lim = (const unsigned char *)a->mv_data; pa = (const uint8_t *)a->mv_data + a->mv_size;
p1 = (const unsigned char *)a->mv_data + a->mv_size; pb = (const uint8_t *)b->mv_data + b->mv_size;
p2 = (const unsigned char *)b->mv_data + b->mv_size; size_t minlen = (a->mv_size < b->mv_size) ? a->mv_size : b->mv_size;
end = pa - minlen;
len_diff = (ssize_t) a->mv_size - (ssize_t) b->mv_size; while (pa != end) {
if (len_diff > 0) { int diff = *--pa - *--pb;
p1_lim += len_diff; if (likely(diff))
len_diff = 1;
}
while (p1 > p1_lim) {
diff = *--p1 - *--p2;
if (unlikely(diff))
return diff; return diff;
} }
return len_diff<0 ? -1 : len_diff; return mdbx_cmp2int(a->mv_size, b->mv_size);
} }
/** Search for key within a page, using binary search. /** Search for key within a page, using binary search.