Add binary dump/load cereal support for flat hash maps - Issue #120

This commit is contained in:
greg
2021-12-26 19:47:45 -05:00
parent f687729170
commit 870e6eb9b5
3 changed files with 203 additions and 99 deletions
+91 -15
View File
@@ -5,8 +5,6 @@
#define USE_CEREAL 0
#if USE_CEREAL
#define PHMAP_DISABLE_DUMP // this is needed because the cereal doesn't like our hash map to have a dump() function
#include "cereal/types/unordered_map.hpp"
#include "cereal/types/memory.hpp"
#include "cereal/types/bitset.hpp"
@@ -53,7 +51,7 @@ public:
{
return permuteQPR((permuteQPR(m_index++) + m_intermediateOffset) ^ 0x5bf03635);
}
};
};
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
@@ -68,15 +66,17 @@ void showtime(const char *name, std::function<void ()> doit)
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
int main()
template <class MapType>
void testMapSerialization(const char *maptype, const char *fname)
{
using MapType = phmap::flat_hash_map<unsigned int, int>;
MapType table;
const int num_items = 100000000;
printf("Building test %s\n", maptype);
// Iterate and add keys and values
// -------------------------------
showtime("build hash", [&table, num_items]() {
showtime("build time", [&table, num_items]() {
unsigned int seed = 76687;
RSU rsu(seed, seed + 1);
@@ -87,12 +87,12 @@ int main()
// cerealize and save data
// -----------------------
showtime("serialize", [&table]() {
showtime("serialize", [&]() {
#if !USE_CEREAL
phmap::BinaryOutputArchive ar_out("./dump.data");
table.dump(ar_out);
phmap::BinaryOutputArchive ar_out(fname);
table.phmap_dump(ar_out);
#else
ofstream os("out.cereal", ios::binary);
ofstream os(fname, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
cereal::BinaryOutputArchive archive(os);
archive(table.size());
archive(table);
@@ -103,12 +103,12 @@ int main()
// deserialize
// -----------
showtime("deserialize", [&table_in]() {
showtime("deserialize", [&]() {
#if !USE_CEREAL
phmap::BinaryInputArchive ar_in("./dump.data");
table_in.load(ar_in);
phmap::BinaryInputArchive ar_in(fname);
table_in.phmap_load(ar_in);
#else
ifstream is("out.cereal", ios::binary);
ifstream is(fname, std::ofstream::in | std::ofstream::binary);
cereal::BinaryInputArchive archive_in(is);
size_t table_size;
@@ -120,9 +120,85 @@ int main()
if (table == table_in)
printf("All checks out, table size: %zu\n", table_in.size());
printf("All checks out, table size: %zu\n\n", table_in.size());
else
printf("FAILURE\n");
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
template <class SetType>
void testSetSerialization(const char *settype, const char *fname)
{
SetType table;
const int num_items = 100000000;
printf("Building test %s\n", settype);
// Iterate and add keys and values
// -------------------------------
showtime("build time", [&]() {
unsigned int seed = 76687;
RSU rsu(seed, seed + 1);
table.reserve(num_items);
for (int i=0; i < num_items; ++i)
table.insert(typename SetType::value_type(rsu.next()));
});
// cerealize and save data
// -----------------------
showtime("serialize", [&]() {
#if !USE_CEREAL
phmap::BinaryOutputArchive ar_out(fname);
table.phmap_dump(ar_out);
#else
ofstream os(fname, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
cereal::BinaryOutputArchive archive(os);
archive(table.size());
archive(table);
#endif
});
SetType table_in;
// deserialize
// -----------
showtime("deserialize", [&]() {
#if !USE_CEREAL
phmap::BinaryInputArchive ar_in(fname);
table_in.phmap_load(ar_in);
#else
ifstream is(fname, std::ofstream::in | std::ofstream::binary);
cereal::BinaryInputArchive archive_in(is);
size_t table_size;
archive_in(table_size);
table_in.reserve(table_size);
archive_in(table_in); // deserialize from file out.cereal into table_in
#endif
});
if (table == table_in)
printf("All checks out, table size: %zu\n\n", table_in.size());
else
printf("FAILURE\n");
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
int main()
{
testSetSerialization<phmap::flat_hash_set<unsigned int>>("flat_hash_set", "dump1.bin");
#if 0
testSetSerialization<phmap::parallel_flat_hash_set<unsigned int>>("parallel_flat_hash_set", "dump1.bin");
testMapSerialization<phmap::flat_hash_map<unsigned int, int>>("flat_hash_map", "dump1.bin");
testMapSerialization<phmap::parallel_flat_hash_map<unsigned int, int>>("parallel_flat_hash_map", "dump1.bin");
#endif
return 0;
}