mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-29 16:40:39 +08:00
Do nothing when calling clear() on empty hash_map. Cereal does call map.clear() when deserializing. Issue #19.
This commit is contained in:
Vendored
+1
@@ -124,6 +124,7 @@ if (PHMAP_BUILD_EXAMPLES)
|
|||||||
add_executable(ex_basic examples/basic.cc phmap.natvis)
|
add_executable(ex_basic examples/basic.cc phmap.natvis)
|
||||||
add_executable(ex_bench examples/bench.cc phmap.natvis)
|
add_executable(ex_bench examples/bench.cc phmap.natvis)
|
||||||
add_executable(ex_emplace examples/emplace.cc phmap.natvis)
|
add_executable(ex_emplace examples/emplace.cc phmap.natvis)
|
||||||
|
add_executable(ex_serialize examples/serialize.cc phmap.natvis)
|
||||||
add_executable(ex_hash_std examples/hash_std.cc phmap.natvis)
|
add_executable(ex_hash_std examples/hash_std.cc phmap.natvis)
|
||||||
add_executable(ex_hash_value examples/hash_value.cc phmap.natvis)
|
add_executable(ex_hash_value examples/hash_value.cc phmap.natvis)
|
||||||
add_executable(ex_two_files examples/f1.cc examples/f2.cc phmap.natvis)
|
add_executable(ex_two_files examples/f1.cc examples/f2.cc phmap.natvis)
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <bitset>
|
||||||
|
#include <cinttypes>
|
||||||
|
#include "parallel_hashmap/phmap.h"
|
||||||
|
#include "cereal/types/unordered_map.hpp"
|
||||||
|
#include "cereal/types/memory.hpp"
|
||||||
|
#include "cereal/types/bitset.hpp"
|
||||||
|
#include "cereal/archives/binary.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <random>
|
||||||
|
#include <chrono>
|
||||||
|
#include <functional>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
using phmap::flat_hash_map;
|
||||||
|
using namespace std;
|
||||||
|
template <typename T> using milliseconds = std::chrono::duration<T, std::milli>;
|
||||||
|
|
||||||
|
void showtime(const char *name, std::function<void ()> doit)
|
||||||
|
{
|
||||||
|
auto t1 = std::chrono::high_resolution_clock::now();
|
||||||
|
doit();
|
||||||
|
auto t2 = std::chrono::high_resolution_clock::now();
|
||||||
|
auto elapsed = milliseconds<double>(t2 - t1).count();
|
||||||
|
printf("%s: %.3fms\n", name, (int)elapsed / 1000.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using MapType = phmap::parallel_flat_hash_map<bitset<42>, int>;
|
||||||
|
MapType table;
|
||||||
|
const int num_items = 100000000;
|
||||||
|
|
||||||
|
// Iterate and add keys and values
|
||||||
|
// -------------------------------
|
||||||
|
showtime("build hash", [&table, num_items]() {
|
||||||
|
random_device dev;
|
||||||
|
mt19937 rng(dev());
|
||||||
|
uniform_int_distribution<mt19937::result_type> dist6(0,1);
|
||||||
|
table.reserve(num_items);
|
||||||
|
for (int i=0; i < num_items; ++i)
|
||||||
|
{
|
||||||
|
bitset<42> bs;
|
||||||
|
for (int j=0; j<42; ++j)
|
||||||
|
bs[j] = dist6(rng);
|
||||||
|
table[bs] = i;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// cerealize and save data
|
||||||
|
// -----------------------
|
||||||
|
showtime("serialize", [&table]() {
|
||||||
|
ofstream os("z:\out.cereal", ios::binary);
|
||||||
|
#if 0
|
||||||
|
const unsigned int length = 8192;
|
||||||
|
char buffer[length];
|
||||||
|
os.rdbuf()->pubsetbuf(buffer, length);
|
||||||
|
#endif
|
||||||
|
cereal::BinaryOutputArchive archive(os);
|
||||||
|
archive(table.size());
|
||||||
|
archive(table);
|
||||||
|
});
|
||||||
|
|
||||||
|
MapType().swap(table);
|
||||||
|
|
||||||
|
// deserialize
|
||||||
|
// -----------
|
||||||
|
showtime("deserialize", [&table]() {
|
||||||
|
ifstream is("z:\out.cereal", ios::binary);
|
||||||
|
cereal::BinaryInputArchive archive_in(is);
|
||||||
|
size_t table_size;
|
||||||
|
|
||||||
|
archive_in(table_size);
|
||||||
|
table.reserve(table_size);
|
||||||
|
archive_in(table); // deserialize from file out.cereal into variable
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
printf("table size: %zu\n", table.size());
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Vendored
+2
@@ -1202,6 +1202,8 @@ public:
|
|||||||
// compared to destruction of the elements of the container. So we pick the
|
// compared to destruction of the elements of the container. So we pick the
|
||||||
// largest bucket_count() threshold for which iteration is still fast and
|
// largest bucket_count() threshold for which iteration is still fast and
|
||||||
// past that we simply deallocate the array.
|
// past that we simply deallocate the array.
|
||||||
|
if (empty())
|
||||||
|
return;
|
||||||
if (capacity_ > 127) {
|
if (capacity_ > 127) {
|
||||||
destroy_slots();
|
destroy_slots();
|
||||||
} else if (capacity_) {
|
} else if (capacity_) {
|
||||||
|
|||||||
Reference in New Issue
Block a user