ClickHouse/dbms/src/Interpreters/tests/hash_map3.cpp
Amos Bird 26ab5dd7a7 A Proper lookup table that uses HashTable's API
This is the first step of allowing heterogeneous cells in hash tables.

performance test results are

```

1. HashMap<UInt16, UInt8, TrivialHash, HashTableFixedGrower<16>>;
2. NewLookupMap<UInt16, UInt8>

ResolutionWidth 30000 1 .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................223550276.46
ResolutionWidth 30000 2 .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................248772721.24
Best: 2 - 24877272124

ResolutionWidth 100000 1 ..........................................................................................................................................................................................................................................................238498413.99
ResolutionWidth 100000 2 ..........................................................................................................................................................................................................................................................261808889.98
Best: 2 - 26180888998

ResolutionWidth 300000 1 ...................................................................................239307348.81
ResolutionWidth 300000 2 ...................................................................................257592761.30
Best: 2 - 25759276130

ResolutionWidth 1000000 1 .........................240144759.26
ResolutionWidth 1000000 2 .........................257093531.91
Best: 2 - 25709353191

ResolutionWidth 5000000 1 .....241573260.35
ResolutionWidth 5000000 2 .....259314162.79
Best: 2 - 25931416279

ResolutionDepth 30000 1 .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................217108119.84
ResolutionDepth 30000 2 .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................249459504.41
Best: 2 - 24945950441

ResolutionDepth 100000 1 ..........................................................................................................................................................................................................................................................229065162.17
ResolutionDepth 100000 2 ..........................................................................................................................................................................................................................................................253769105.64
Best: 2 - 25376910564

ResolutionDepth 300000 1 ...................................................................................233079225.18
ResolutionDepth 300000 2 ...................................................................................256316273.78
Best: 2 - 25631627378

ResolutionDepth 1000000 1 .........................234184633.51
ResolutionDepth 1000000 2 .........................261100491.57
Best: 2 - 26110049157

ResolutionDepth 5000000 1 .....233118795.66
ResolutionDepth 5000000 2 .....252436160.41
Best: 2 - 25243616041

```
2019-03-01 16:47:13 +08:00

92 lines
2.0 KiB
C++

#include <iostream>
#define DBMS_HASH_MAP_DEBUG_RESIZES
#define DBMS_HASH_MAP_COUNT_COLLISIONS
#include <string.h>
#include <cstdlib>
#include <utility>
#include <Core/Types.h>
#include <Common/Exception.h>
#include <IO/ReadHelpers.h>
#include <common/StringRef.h>
#include <Common/HashTable/HashMap.h>
template
<
typename Key,
typename Mapped,
typename Hash = DefaultHash<Key>,
typename Grower = HashTableGrower<>,
typename Allocator = HashTableAllocator
>
class HashMapWithDump : public HashMap<Key, Mapped, Hash, Grower, Allocator>
{
public:
void dump() const
{
for (size_t i = 0; i < this->grower.bufSize(); ++i)
{
if (this->buf[i].isZero(*this))
std::cerr << "[ ]";
else
std::cerr << '[' << this->buf[i].getValue().getFirst().data << ", " << this->buf[i].getValue().getSecond() << ']';
}
std::cerr << std::endl;
}
};
struct SimpleHash
{
size_t operator() (UInt64 x) const { return x; }
size_t operator() (StringRef x) const { return DB::parse<UInt64>(x.data); }
};
struct Grower : public HashTableGrower<2>
{
void increaseSize()
{
++size_degree;
}
};
int main(int, char **)
{
using Map = HashMapWithDump<
StringRef,
UInt64,
SimpleHash,
Grower,
HashTableAllocatorWithStackMemory<4 * 24>>;
Map map;
map.dump();
std::cerr << "size: " << map.size() << std::endl;
map[StringRef("1", 1)] = 1;
map.dump();
std::cerr << "size: " << map.size() << std::endl;
map[StringRef("9", 1)] = 1;
map.dump();
std::cerr << "size: " << map.size() << std::endl;
std::cerr << "Collisions: " << map.getCollisions() << std::endl;
map[StringRef("3", 1)] = 2;
map.dump();
std::cerr << "size: " << map.size() << std::endl;
std::cerr << "Collisions: " << map.getCollisions() << std::endl;
for (auto x : map)
std::cerr << x.getFirst().toString() << " -> " << x.getSecond() << std::endl;
return 0;
}