2014-11-09 02:51:28 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2014-11-09 02:51:28 +00:00
|
|
|
|
|
|
|
//#define DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
#define DBMS_HASH_MAP_DEBUG_RESIZES
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2018-12-28 18:15:26 +00:00
|
|
|
#include <Compression/CompressedReadBuffer.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/StringRef.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/HashTable/HashMap.h>
|
|
|
|
#include <Interpreters/AggregationCommon.h>
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_1__
|
2017-04-01 07:20:54 +00:00
|
|
|
#include <smmintrin.h>
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** Do this:
|
2014-11-09 12:43:09 +00:00
|
|
|
for file in MobilePhoneModel PageCharset Params URLDomain UTMSource Referer URL Title; do
|
2018-05-07 02:14:24 +00:00
|
|
|
for size in 30000 100000 300000 1000000 5000000; do
|
|
|
|
echo
|
|
|
|
BEST_METHOD=0
|
|
|
|
BEST_RESULT=0
|
|
|
|
for method in {1..12}; do
|
|
|
|
echo -ne $file $size $method '';
|
|
|
|
TOTAL_ELEMS=0
|
|
|
|
for i in {0..1000}; do
|
|
|
|
TOTAL_ELEMS=$(( $TOTAL_ELEMS + $size ))
|
|
|
|
if [[ $TOTAL_ELEMS -gt 25000000 ]]; then break; fi
|
|
|
|
./hash_map_string_2 $size $method < ${file}.bin 2>&1 |
|
|
|
|
grep HashMap | grep -oE '[0-9\.]+ elem';
|
|
|
|
done | awk -W interactive '{ if ($1 > x) { x = $1 }; printf(".") } END { print x }' | tee /tmp/hash_map_string_2_res;
|
|
|
|
CUR_RESULT=$(cat /tmp/hash_map_string_2_res | tr -d '.')
|
|
|
|
if [[ $CUR_RESULT -gt $BEST_RESULT ]]; then
|
|
|
|
BEST_METHOD=$method
|
|
|
|
BEST_RESULT=$CUR_RESULT
|
|
|
|
fi;
|
|
|
|
done;
|
|
|
|
echo Best: $BEST_METHOD - $BEST_RESULT
|
|
|
|
done;
|
2014-11-09 02:51:28 +00:00
|
|
|
done
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define DefineStringRef(STRUCT) \
|
|
|
|
\
|
|
|
|
struct STRUCT : public StringRef {}; \
|
|
|
|
\
|
|
|
|
namespace ZeroTraits \
|
|
|
|
{ \
|
2017-04-01 07:20:54 +00:00
|
|
|
template <> \
|
2020-03-18 02:02:24 +00:00
|
|
|
inline bool check<STRUCT>(STRUCT x) { return 0 == x.size; } /* NOLINT */ \
|
2014-11-09 02:51:28 +00:00
|
|
|
\
|
2017-04-01 07:20:54 +00:00
|
|
|
template <> \
|
2020-03-18 02:02:24 +00:00
|
|
|
inline void set<STRUCT>(STRUCT & x) { x.size = 0; } /* NOLINT */ \
|
2018-08-26 01:11:58 +00:00
|
|
|
} \
|
2014-11-09 02:51:28 +00:00
|
|
|
\
|
|
|
|
template <> \
|
|
|
|
struct DefaultHash<STRUCT> \
|
|
|
|
{ \
|
2020-03-18 02:02:24 +00:00
|
|
|
size_t operator() (STRUCT x) const /* NOLINT */ \
|
2017-04-01 07:20:54 +00:00
|
|
|
{ \
|
2017-06-21 08:35:38 +00:00
|
|
|
return CityHash_v1_0_2::CityHash64(x.data, x.size); \
|
2017-04-01 07:20:54 +00:00
|
|
|
} \
|
2014-11-09 02:51:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DefineStringRef(StringRef_Compare1_Ptrs)
|
|
|
|
DefineStringRef(StringRef_Compare1_Index)
|
|
|
|
DefineStringRef(StringRef_CompareMemcmp)
|
|
|
|
DefineStringRef(StringRef_Compare8_1_byUInt64)
|
|
|
|
DefineStringRef(StringRef_Compare16_1_byMemcmp)
|
|
|
|
DefineStringRef(StringRef_Compare16_1_byUInt64_logicAnd)
|
|
|
|
DefineStringRef(StringRef_Compare16_1_byUInt64_bitAnd)
|
2016-01-13 21:05:11 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_1__
|
2014-11-09 02:51:28 +00:00
|
|
|
DefineStringRef(StringRef_Compare16_1_byIntSSE)
|
|
|
|
DefineStringRef(StringRef_Compare16_1_byFloatSSE)
|
2014-11-09 07:26:37 +00:00
|
|
|
DefineStringRef(StringRef_Compare16_1_bySSE4)
|
|
|
|
DefineStringRef(StringRef_Compare16_1_bySSE4_wide)
|
|
|
|
DefineStringRef(StringRef_Compare16_1_bySSE_wide)
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-09 08:39:18 +00:00
|
|
|
DefineStringRef(StringRef_CompareAlwaysTrue)
|
|
|
|
DefineStringRef(StringRef_CompareAlmostAlwaysTrue)
|
2014-11-09 02:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_Compare1_Ptrs lhs, StringRef_Compare1_Ptrs rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * pos1 = lhs.data;
|
|
|
|
const char * pos2 = rhs.data;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * end1 = pos1 + lhs.size;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos1 < end1)
|
|
|
|
{
|
|
|
|
if (*pos1 != *pos2)
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++pos1;
|
|
|
|
++pos2;
|
|
|
|
}
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_Compare1_Index lhs, StringRef_Compare1_Index rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < lhs.size; ++i)
|
|
|
|
if (lhs.data[i] != rhs.data[i])
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_CompareMemcmp lhs, StringRef_CompareMemcmp rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0 == memcmp(lhs.data, rhs.data, lhs.size);
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_Compare8_1_byUInt64 lhs, StringRef_Compare8_1_byUInt64 rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * p1 = lhs.data;
|
|
|
|
const char * p2 = rhs.data;
|
|
|
|
size_t size = lhs.size;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * p1_end = p1 + size;
|
|
|
|
const char * p1_end_8 = p1 + size / 8 * 8;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (p1 < p1_end_8)
|
|
|
|
{
|
|
|
|
if (reinterpret_cast<const UInt64 *>(p1)[0] != reinterpret_cast<const UInt64 *>(p2)[0])
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
p1 += 8;
|
|
|
|
p2 += 8;
|
|
|
|
}
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (p1 < p1_end)
|
|
|
|
{
|
|
|
|
if (*p1 != *p2)
|
|
|
|
return false;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++p1;
|
|
|
|
++p2;
|
|
|
|
}
|
2014-11-09 02:51:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool compare_byMemcmp(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0 == memcmp(p1, p2, 16);
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool compare_byUInt64_logicAnd(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return reinterpret_cast<const UInt64 *>(p1)[0] == reinterpret_cast<const UInt64 *>(p2)[0]
|
|
|
|
&& reinterpret_cast<const UInt64 *>(p1)[1] == reinterpret_cast<const UInt64 *>(p2)[1];
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool compare_byUInt64_bitAnd(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return (reinterpret_cast<const UInt64 *>(p1)[0] == reinterpret_cast<const UInt64 *>(p2)[0])
|
|
|
|
& (reinterpret_cast<const UInt64 *>(p1)[1] == reinterpret_cast<const UInt64 *>(p2)[1]);
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_1__
|
2016-01-13 21:05:11 +00:00
|
|
|
|
2014-11-09 02:51:28 +00:00
|
|
|
inline bool compare_byIntSSE(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0xFFFF == _mm_movemask_epi8(_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2))));
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool compare_byFloatSSE(const char * p1, const char * p2)
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
return !_mm_movemask_ps(_mm_cmpneq_ps( /// Looks like incorrect while comparing subnormal floats.
|
2017-04-01 07:20:54 +00:00
|
|
|
_mm_loadu_ps(reinterpret_cast<const float *>(p1)),
|
|
|
|
_mm_loadu_ps(reinterpret_cast<const float *>(p2))));
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-09 02:51:28 +00:00
|
|
|
|
|
|
|
template <bool compare(const char *, const char *)>
|
|
|
|
inline bool memequal(const char * p1, const char * p2, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
// const char * p1_end = p1 + size;
|
|
|
|
const char * p1_end_16 = p1 + size / 16 * 16;
|
|
|
|
|
|
|
|
while (p1 < p1_end_16)
|
|
|
|
{
|
|
|
|
if (!compare(p1, p2))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p1 += 16;
|
|
|
|
p2 += 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* while (p1 < p1_end)
|
|
|
|
{
|
|
|
|
if (*p1 != *p2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++p1;
|
|
|
|
++p2;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
switch (size % 16)
|
|
|
|
{
|
2017-12-03 03:22:25 +00:00
|
|
|
case 15: if (p1[14] != p2[14]) return false; [[fallthrough]];
|
|
|
|
case 14: if (p1[13] != p2[13]) return false; [[fallthrough]];
|
|
|
|
case 13: if (p1[12] != p2[12]) return false; [[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 12: if (reinterpret_cast<const UInt32 *>(p1)[2] == reinterpret_cast<const UInt32 *>(p2)[2]) goto l8; else return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
case 11: if (p1[10] != p2[10]) return false; [[fallthrough]];
|
|
|
|
case 10: if (p1[9] != p2[9]) return false; [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 9: if (p1[8] != p2[8]) return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
l8: [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 8: return reinterpret_cast<const UInt64 *>(p1)[0] == reinterpret_cast<const UInt64 *>(p2)[0];
|
|
|
|
case 7: if (p1[6] != p2[6]) return false; [[fallthrough]];
|
|
|
|
case 6: if (p1[5] != p2[5]) return false; [[fallthrough]];
|
|
|
|
case 5: if (p1[4] != p2[4]) return false; [[fallthrough]];
|
|
|
|
case 4: return reinterpret_cast<const UInt32 *>(p1)[0] == reinterpret_cast<const UInt32 *>(p2)[0];
|
|
|
|
case 3: if (p1[2] != p2[2]) return false; [[fallthrough]];
|
|
|
|
case 2: return reinterpret_cast<const UInt16 *>(p1)[0] == reinterpret_cast<const UInt16 *>(p2)[0];
|
|
|
|
case 1: if (p1[0] != p2[0]) return false; [[fallthrough]];
|
|
|
|
case 0: break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-11-09 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_1__
|
2016-01-13 21:05:11 +00:00
|
|
|
|
2014-11-09 07:26:37 +00:00
|
|
|
inline bool memequal_sse41(const char * p1, const char * p2, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
// const char * p1_end = p1 + size;
|
|
|
|
const char * p1_end_16 = p1 + size / 16 * 16;
|
|
|
|
|
|
|
|
__m128i zero16 = _mm_setzero_si128();
|
|
|
|
|
|
|
|
while (p1 < p1_end_16)
|
|
|
|
{
|
|
|
|
if (!_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p1)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(p2)))))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
p1 += 16;
|
|
|
|
p2 += 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* while (p1 < p1_end)
|
|
|
|
{
|
|
|
|
if (*p1 != *p2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++p1;
|
|
|
|
++p2;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
switch (size % 16)
|
|
|
|
{
|
2017-12-03 03:22:25 +00:00
|
|
|
case 15: if (p1[14] != p2[14]) return false; [[fallthrough]];
|
|
|
|
case 14: if (p1[13] != p2[13]) return false; [[fallthrough]];
|
|
|
|
case 13: if (p1[12] != p2[12]) return false; [[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 12: if (reinterpret_cast<const UInt32 *>(p1)[2] == reinterpret_cast<const UInt32 *>(p2)[2]) goto l8; else return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
case 11: if (p1[10] != p2[10]) return false; [[fallthrough]];
|
|
|
|
case 10: if (p1[9] != p2[9]) return false; [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 9: if (p1[8] != p2[8]) return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
l8: [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 8: return reinterpret_cast<const UInt64 *>(p1)[0] == reinterpret_cast<const UInt64 *>(p2)[0];
|
|
|
|
case 7: if (p1[6] != p2[6]) return false; [[fallthrough]];
|
|
|
|
case 6: if (p1[5] != p2[5]) return false; [[fallthrough]];
|
|
|
|
case 5: if (p1[4] != p2[4]) return false; [[fallthrough]];
|
|
|
|
case 4: return reinterpret_cast<const UInt32 *>(p1)[0] == reinterpret_cast<const UInt32 *>(p2)[0];
|
|
|
|
case 3: if (p1[2] != p2[2]) return false; [[fallthrough]];
|
|
|
|
case 2: return reinterpret_cast<const UInt16 *>(p1)[0] == reinterpret_cast<const UInt16 *>(p2)[0];
|
|
|
|
case 1: if (p1[0] != p2[0]) return false; [[fallthrough]];
|
|
|
|
case 0: break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-11-09 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool memequal_sse41_wide(const char * p1, const char * p2, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
__m128i zero16 = _mm_setzero_si128();
|
|
|
|
// const char * p1_end = p1 + size;
|
|
|
|
|
|
|
|
while (size >= 64)
|
|
|
|
{
|
|
|
|
if (_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[0]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[0])))
|
|
|
|
&& _mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[1]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[1])))
|
|
|
|
&& _mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[2]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[2])))
|
|
|
|
&& _mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[3]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[3]))))
|
|
|
|
{
|
|
|
|
p1 += 64;
|
|
|
|
p2 += 64;
|
|
|
|
size -= 64;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((size % 64) / 16)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
if (!_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[2]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[2]))))
|
|
|
|
return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
[[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 2:
|
|
|
|
if (!_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[1]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[1]))))
|
|
|
|
return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
[[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 1:
|
|
|
|
if (!_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[0]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[0]))))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p1 += (size % 64) / 16 * 16;
|
|
|
|
p2 += (size % 64) / 16 * 16;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size >= 32)
|
|
|
|
{
|
|
|
|
if (_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[0]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[0])))
|
|
|
|
& _mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[1]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[1]))))
|
|
|
|
{
|
|
|
|
p1 += 32;
|
|
|
|
p2 += 32;
|
|
|
|
size -= 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size >= 16)
|
|
|
|
{
|
|
|
|
if (_mm_testc_si128(
|
|
|
|
zero16,
|
|
|
|
_mm_xor_si128(
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p1)[0]),
|
|
|
|
_mm_loadu_si128(&reinterpret_cast<const __m128i *>(p2)[0]))))
|
|
|
|
{
|
|
|
|
p1 += 16;
|
|
|
|
p2 += 16;
|
|
|
|
size -= 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
switch (size % 16)
|
|
|
|
{
|
2017-12-03 03:22:25 +00:00
|
|
|
case 15: if (p1[14] != p2[14]) return false; [[fallthrough]];
|
|
|
|
case 14: if (p1[13] != p2[13]) return false; [[fallthrough]];
|
|
|
|
case 13: if (p1[12] != p2[12]) return false; [[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 12: if (reinterpret_cast<const UInt32 *>(p1)[2] == reinterpret_cast<const UInt32 *>(p2)[2]) goto l8; else return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
case 11: if (p1[10] != p2[10]) return false; [[fallthrough]];
|
|
|
|
case 10: if (p1[9] != p2[9]) return false; [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 9: if (p1[8] != p2[8]) return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
l8: [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 8: return reinterpret_cast<const UInt64 *>(p1)[0] == reinterpret_cast<const UInt64 *>(p2)[0];
|
|
|
|
case 7: if (p1[6] != p2[6]) return false; [[fallthrough]];
|
|
|
|
case 6: if (p1[5] != p2[5]) return false; [[fallthrough]];
|
|
|
|
case 5: if (p1[4] != p2[4]) return false; [[fallthrough]];
|
|
|
|
case 4: return reinterpret_cast<const UInt32 *>(p1)[0] == reinterpret_cast<const UInt32 *>(p2)[0];
|
|
|
|
case 3: if (p1[2] != p2[2]) return false; [[fallthrough]];
|
|
|
|
case 2: return reinterpret_cast<const UInt16 *>(p1)[0] == reinterpret_cast<const UInt16 *>(p2)[0];
|
|
|
|
case 1: if (p1[0] != p2[0]) return false; [[fallthrough]];
|
|
|
|
case 0: break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-11-09 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool memequal_sse_wide(const char * p1, const char * p2, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
while (size >= 64)
|
|
|
|
{
|
|
|
|
if ( compare_byIntSSE(p1, p2)
|
|
|
|
&& compare_byIntSSE(p1 + 16, p2 + 16)
|
|
|
|
&& compare_byIntSSE(p1 + 32, p2 + 32)
|
|
|
|
&& compare_byIntSSE(p1 + 48, p2 + 48))
|
|
|
|
{
|
|
|
|
p1 += 64;
|
|
|
|
p2 += 64;
|
|
|
|
size -= 64;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((size % 64) / 16)
|
|
|
|
{
|
2017-12-03 03:22:25 +00:00
|
|
|
case 3: if (!compare_byIntSSE(p1 + 32, p2 + 32)) return false; [[fallthrough]];
|
|
|
|
case 2: if (!compare_byIntSSE(p1 + 16, p2 + 16)) return false; [[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 1: if (!compare_byIntSSE(p1 , p2 )) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p1 += (size % 64) / 16 * 16;
|
|
|
|
p2 += (size % 64) / 16 * 16;
|
|
|
|
|
|
|
|
switch (size % 16)
|
|
|
|
{
|
2017-12-03 03:22:25 +00:00
|
|
|
case 15: if (p1[14] != p2[14]) return false; [[fallthrough]];
|
|
|
|
case 14: if (p1[13] != p2[13]) return false; [[fallthrough]];
|
|
|
|
case 13: if (p1[12] != p2[12]) return false; [[fallthrough]];
|
2017-04-01 07:20:54 +00:00
|
|
|
case 12: if (reinterpret_cast<const UInt32 *>(p1)[2] == reinterpret_cast<const UInt32 *>(p2)[2]) goto l8; else return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
case 11: if (p1[10] != p2[10]) return false; [[fallthrough]];
|
|
|
|
case 10: if (p1[9] != p2[9]) return false; [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 9: if (p1[8] != p2[8]) return false;
|
2017-12-03 03:22:25 +00:00
|
|
|
l8: [[fallthrough]];
|
2018-11-26 00:56:50 +00:00
|
|
|
case 8: return reinterpret_cast<const UInt64 *>(p1)[0] == reinterpret_cast<const UInt64 *>(p2)[0];
|
|
|
|
case 7: if (p1[6] != p2[6]) return false; [[fallthrough]];
|
|
|
|
case 6: if (p1[5] != p2[5]) return false; [[fallthrough]];
|
|
|
|
case 5: if (p1[4] != p2[4]) return false; [[fallthrough]];
|
|
|
|
case 4: return reinterpret_cast<const UInt32 *>(p1)[0] == reinterpret_cast<const UInt32 *>(p2)[0];
|
|
|
|
case 3: if (p1[2] != p2[2]) return false; [[fallthrough]];
|
|
|
|
case 2: return reinterpret_cast<const UInt16 *>(p1)[0] == reinterpret_cast<const UInt16 *>(p2)[0];
|
|
|
|
case 1: if (p1[0] != p2[0]) return false; [[fallthrough]];
|
|
|
|
case 0: break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-09 02:51:28 +00:00
|
|
|
|
|
|
|
#define Op(METHOD) \
|
|
|
|
inline bool operator==(StringRef_Compare16_1_ ## METHOD lhs, StringRef_Compare16_1_ ## METHOD rhs) \
|
|
|
|
{ \
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size) \
|
|
|
|
return false; \
|
2014-11-09 02:51:28 +00:00
|
|
|
\
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0) \
|
|
|
|
return true; \
|
2014-11-09 02:51:28 +00:00
|
|
|
\
|
2017-04-01 07:20:54 +00:00
|
|
|
return memequal<compare_ ## METHOD>(lhs.data, rhs.data, lhs.size); \
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Op(byMemcmp)
|
|
|
|
Op(byUInt64_logicAnd)
|
|
|
|
Op(byUInt64_bitAnd)
|
2016-01-13 21:05:11 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_1__
|
2016-01-13 21:05:11 +00:00
|
|
|
|
2014-11-09 02:51:28 +00:00
|
|
|
Op(byIntSSE)
|
|
|
|
Op(byFloatSSE)
|
|
|
|
|
|
|
|
|
2014-11-09 07:26:37 +00:00
|
|
|
inline bool operator==(StringRef_Compare16_1_bySSE4 lhs, StringRef_Compare16_1_bySSE4 rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return memequal_sse41(lhs.data, rhs.data, lhs.size);
|
2014-11-09 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_Compare16_1_bySSE4_wide lhs, StringRef_Compare16_1_bySSE4_wide rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return memequal_sse41_wide(lhs.data, rhs.data, lhs.size);
|
2014-11-09 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_Compare16_1_bySSE_wide lhs, StringRef_Compare16_1_bySSE_wide rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
return false;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (lhs.size == 0)
|
|
|
|
return true;
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return memequal_sse_wide(lhs.data, rhs.data, lhs.size);
|
2014-11-09 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
inline bool operator==(StringRef_CompareAlwaysTrue, StringRef_CompareAlwaysTrue)
|
2014-11-09 08:39:18 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2014-11-09 08:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(StringRef_CompareAlmostAlwaysTrue lhs, StringRef_CompareAlmostAlwaysTrue rhs)
|
|
|
|
{
|
2020-03-09 03:38:43 +00:00
|
|
|
return lhs.size == rhs.size;
|
2014-11-09 08:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using Value = UInt64;
|
2014-11-09 02:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Key>
|
|
|
|
void NO_INLINE bench(const std::vector<StringRef> & data, const char * name)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Stopwatch watch;
|
|
|
|
|
|
|
|
using Map = HashMapWithSavedHash<Key, Value, DefaultHash<Key>>;
|
|
|
|
|
|
|
|
Map map;
|
2019-08-20 09:58:44 +00:00
|
|
|
typename Map::LookupResult it;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool inserted;
|
|
|
|
|
2020-03-09 03:38:43 +00:00
|
|
|
for (const auto & value : data)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-03-09 03:38:43 +00:00
|
|
|
map.emplace(static_cast<const Key &>(value), it, inserted);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (inserted)
|
2019-10-29 15:16:51 +00:00
|
|
|
it->getMapped() = 0;
|
|
|
|
++it->getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
std::cerr << std::fixed << std::setprecision(2)
|
|
|
|
<< "HashMap (" << name << "). Size: " << map.size()
|
|
|
|
<< ", elapsed: " << watch.elapsedSeconds()
|
|
|
|
<< " (" << data.size() / watch.elapsedSeconds() << " elem/sec.)"
|
2014-11-09 02:51:28 +00:00
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
2017-04-01 07:20:54 +00:00
|
|
|
<< ", collisions: " << map.getCollisions()
|
2014-11-09 02:51:28 +00:00
|
|
|
#endif
|
2017-04-01 07:20:54 +00:00
|
|
|
<< std::endl;
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-12-02 02:47:12 +00:00
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: program n m\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:27:32 +00:00
|
|
|
size_t n = std::stol(argv[1]);
|
|
|
|
size_t m = std::stol(argv[2]);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DB::Arena pool;
|
|
|
|
std::vector<StringRef> data(n);
|
|
|
|
|
|
|
|
std::cerr << "sizeof(Key) = " << sizeof(StringRef) << ", sizeof(Value) = " << sizeof(Value) << std::endl;
|
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
DB::ReadBufferFromFileDescriptor in1(STDIN_FILENO);
|
|
|
|
DB::CompressedReadBuffer in2(in1);
|
|
|
|
|
|
|
|
std::string tmp;
|
|
|
|
for (size_t i = 0; i < n && !in2.eof(); ++i)
|
|
|
|
{
|
|
|
|
DB::readStringBinary(tmp, in2);
|
|
|
|
data[i] = StringRef(pool.insert(tmp.data(), tmp.size()), tmp.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
std::cerr << std::fixed << std::setprecision(2)
|
|
|
|
<< "Vector. Size: " << n
|
|
|
|
<< ", elapsed: " << watch.elapsedSeconds()
|
|
|
|
<< " (" << n / watch.elapsedSeconds() << " elem/sec.)"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m || m == 1) bench<StringRef_Compare1_Ptrs> (data, "StringRef_Compare1_Ptrs");
|
2017-08-21 21:59:27 +00:00
|
|
|
if (!m || m == 2) bench<StringRef_Compare1_Index> (data, "StringRef_Compare1_Index");
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!m || m == 3) bench<StringRef_CompareMemcmp> (data, "StringRef_CompareMemcmp");
|
2017-08-21 21:59:27 +00:00
|
|
|
if (!m || m == 4) bench<StringRef_Compare8_1_byUInt64> (data, "StringRef_Compare8_1_byUInt64");
|
|
|
|
if (!m || m == 5) bench<StringRef_Compare16_1_byMemcmp> (data, "StringRef_Compare16_1_byMemcmp");
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!m || m == 6) bench<StringRef_Compare16_1_byUInt64_logicAnd>(data, "StringRef_Compare16_1_byUInt64_logicAnd");
|
2017-08-21 21:59:27 +00:00
|
|
|
if (!m || m == 7) bench<StringRef_Compare16_1_byUInt64_bitAnd> (data, "StringRef_Compare16_1_byUInt64_bitAnd");
|
2019-01-04 12:10:00 +00:00
|
|
|
#ifdef __SSE4_1__
|
2017-08-21 21:59:27 +00:00
|
|
|
if (!m || m == 8) bench<StringRef_Compare16_1_byIntSSE> (data, "StringRef_Compare16_1_byIntSSE");
|
|
|
|
if (!m || m == 9) bench<StringRef_Compare16_1_byFloatSSE> (data, "StringRef_Compare16_1_byFloatSSE");
|
|
|
|
if (!m || m == 10) bench<StringRef_Compare16_1_bySSE4> (data, "StringRef_Compare16_1_bySSE4");
|
|
|
|
if (!m || m == 11) bench<StringRef_Compare16_1_bySSE4_wide> (data, "StringRef_Compare16_1_bySSE4_wide");
|
|
|
|
if (!m || m == 12) bench<StringRef_Compare16_1_bySSE_wide> (data, "StringRef_Compare16_1_bySSE_wide");
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
2017-08-21 21:59:27 +00:00
|
|
|
if (!m || m == 100) bench<StringRef_CompareAlwaysTrue> (data, "StringRef_CompareAlwaysTrue");
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!m || m == 101) bench<StringRef_CompareAlmostAlwaysTrue> (data, "StringRef_CompareAlmostAlwaysTrue");
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2021-09-19 19:26:16 +00:00
|
|
|
if (!m || m == 111) bench<StringRef> (data, "StringRef");
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// 10 > 8, 9
|
|
|
|
/// 1, 2, 5 - bad
|
2014-11-09 07:26:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0;
|
2014-11-09 02:51:28 +00:00
|
|
|
}
|