C++ implementation of a memory efficient hash map and hash set
C++ implementation of a memory efficient hash map and hash set
The sparse-map library is a C++ implementation of a memory efficient hash map and hash set. It uses open-addressing with sparse quadratic probing. The goal of the library is to be the most memory efficient possible, even at low load factor, while keeping reasonable performances. You can find an article of Stephen Merity which explains the idea behind google::sparse_hash_map and this project.
Four classes are provided: tsl::sparse_map, tsl::sparse_set, tsl::sparse_pg_map and tsl::sparse_pg_set. The first two are faster and use a power of two growth policy, the last two use a prime growth policy instead and are able to cope better with a poor hash function. Use the prime version if there is a chance of repeating patterns in the lower bits of your hash (e.g. you are storing pointers with an identity hash function). See GrowthPolicy for details.
A benchmark of tsl::sparse_map against other hash maps may be found here. The benchmark, in its additional tests page, notably includes google::sparse_hash_map and spp::sparse_hash_map to which tsl::sparse_map is an alternative. This page also gives some advices on which hash table structure you should try for your use case (useful if you are a bit lost with the multiple hash tables implementations in the tsl namespace).
tsl::sparse_map exported target from the CMakeLists.txt.find with a type different than Key (e.g. if you have a map that uses std::unique_ptr as key, you can use a foo* or a std::uintptr_t as key parameter to find without constructing a std::unique_ptr, see example).precalculated_hash parameter in API).serialize/deserialize methods in the API for details).Sparsity template parameter. A high sparsity means less memory but longer insertion times, and vice-versa for low sparsity. The default medium sparsity offers a good compromise (see API for details). For reference, with simple 64 bits integers as keys and values, a low sparsity offers ~15% faster insertions times but uses ~12% more memory. Nothing change regarding lookup speed.std::unordered_map and std::unordered_set.std::unordered_maptsl::sparse_map tries to have an interface similar to std::unordered_map, but some differences exist.
rehash is called explicitly) and will occur if the Allocator can't allocate memory (std::bad_alloc) or if the copy constructor (when a nothrow move constructor is not available) throws and exception. This can be avoided by calling reserve beforehand. It is the same guarantee that the one provided by google::sparse_hash_map and spp::sparse_hash_map which don't provide the strong exception guarantee. For more information and if you need the strong exception guarantee, check the ExceptionSafety template parameter (see API for details).operator*() and operator->() return a reference and a pointer to const std::pair instead of std::pair making the value T not modifiable. To modify the value you have to call the value() method of the iterator to get a mutable reference. Example:tsl::sparse_map map = {{1, 1}, {2, 1}, {3, 1}};
for(auto it = map.begin(); it != map.end(); ++it) {
//it->second = 2; // Illegal
it.value() = 2; // Ok
}bucket_size, bucket, ...).These differences also apply between std::unordered_set and tsl::sparse_set.
Thread-safety guarantees are the same as std::unordered_map/set (i.e. possible to have multiple readers with no writer).
The library relies heavily on the popcount operation.
With Clang and GCC, the library uses the __builtin_popcount function which will use the fast CPU instruction POPCNT when the library is compiled with -mpopcnt. Using the POPCNT instruction offers an improvement of ~15% to ~30% on lookups. So if you are compiling your code for a specific architecture that support the operation, don't forget the -mpopcnt (or -march=native) flag of your compiler.
On Windows with MSVC, the detection is done at runtime.
Make sure that your key Key and potential value T have a noexcept move constructor. The library will work without it but insertions will be much slower if the copy constructor is expensive (the structure often needs to move some values around on insertion).
The library supports multiple growth policies through the GrowthPolicy template parameter. Three policies are provided by the library but you can easily implement your own if needed.
tsl::sparse_map/set. This policy keeps the size of the bucket array of the hash table to a power of two. This constraint allows the policy to avoid the usage of the slow modulo operation to map a hash to a bucket, instead of hash % 2n, it uses hash & (2n - 1) (see fast modulo). Fast but this may cause a lot of collisions with a poor hash function as the modulo with a power of two only masks the most significant bits in the end.tsl::sparse_pg_map/set. The policy keeps the size of the bucket array of the hash table to a prime number. When mapping a hash to a bucket, using a prime number as modulo will result in a better distribution of the hash across the buckets even with a poor hash function. To allow the compiler to optimize the modulo operation, the policy use a lookup table with constant primes modulos (see API for details). Slower than tsl::sh::power_of_two_growth_policy but more secure.To implement your own policy, you have to implement the following interface.
…To use sparse-map, just add the include directory to your include path. It is a header-only library.
If you use CMake, you can also use the tsl::sparse_map exported target from the CMakeLists.txt with target_link_libraries.
# Example where the sparse-map project is stored in a third-party directory
add_subdirectory(third-party/sparse-map)
target_link_libraries(your_target PRIVATE tsl::sparse_map) If the project has been installed through make install, you can also use find_package(tsl-sparse-map REQUIRED) instead of add_subdirectory.
The code should work with any C++17 standard-compliant compiler.
To run the tests you will need the Boost Test library and CMake.
git clone https://github.com/Tessil/sparse-map.git
cd sparse-map/tests
mkdir build
cd build
cmake ..
cmake --build .
./tsl_sparse_map_tests The API can be found here.
All methods are not documented yet, but they replicate the behaviour of the ones in std::unordered_map and std::unordered_set, except if specified otherwise.
…c++ #include #include #include #include
struct employee { employee(int id, std::string name) : m_id(id), m_name(std::move(name)) { }
// Either we include the comparators in the class and we use `std::equal_to<>`...
friend bool operator==(const employee& empl, int empl_id) {
return empl.m_id == empl_id;
}
friend bool operator==(int empl_id, const employee& empl) {
return empl_id == empl.m_id;
}
friend bool operator==(const employee& empl1, const employee& empl2) {
return empl1.m_id == empl2.m_id;
}
int m_id;
std::string m_name;
};
// ... or we implement a separate class to compare employees. struct equal_employee { using is_transparent = void;
bool operator()(const employee& empl, int empl_id) const {
return empl.m_id == empl_id;
}
bool operator()(int empl_id, const employee& empl) const {
return empl_id == empl.m_id;
}
bool operator()(const employee& empl1, const employee& empl2) const {
return empl1.m_id == empl2.m_id;
}
};
struct hash_employee { std::size_t operator()(const employee& empl) const { return std::hash()(empl.m_id); }
std::size_t operator()(int id) const {
return std::hash()(id);
}
};
int main() { // Use std::equal_to<> which will automatically deduce and forward the parameters tsl::sparse_map> map; map.insert({employee(1, "John Doe"), 2001}); map.insert({employee(2, "Jane Doe"), 2002}); map.insert({employee(3, "John Smith"), 2003});
// John Smith 2003
auto it = map.find(3);
if(it != map.end()) {
std::cout first.m_name second if a map is used or Key for a set.
template
void operator()(const U& value);
};
```c++
struct deserializer {
// Must support the following types for U: std::uint64_t, float
// and std::pair if a map is used or Key for a set.
template
U operator()();
};Note that the implementation leaves binary compatibility (endianness, float binary representation, size of int, ...) of the types it serializes/deserializes in the hands of the provided function objects if compatibility is required.
More details regarding the serialize and deserialize methods can be found in the API.
No open issues yet, or sync has not completed.