Compare commits

...

1 Commits

Author SHA1 Message Date
cat
2a2c435b88 Added Container Options for fine tuning, fine tuned hashmap for DKM 2026-03-03 00:16:17 +02:00
4 changed files with 51 additions and 19 deletions

View File

@@ -0,0 +1,28 @@
/*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef GUARD_TOURMALINE_CONTAINEROPTIONS_H
#define GUARD_TOURMALINE_CONTAINEROPTIONS_H
#include <cstddef>
#include <cstdint>
namespace Tourmaline::Containers {
struct HashmapOptions {
float loadFactor = 0.75f;
float minimizeFactor = 0.20f;
float leaningFactor = 2.5f;
std::size_t minimumBucketCount = 256;
std::size_t reservedBucketSpace = 4;
};
struct DualKeyMapOptions {
std::uint64_t baseReservation = 2048;
};
} // namespace Tourmaline::Containers
#endif

View File

@@ -10,6 +10,7 @@
#define GUARD_TOURMALINE_DUALKEYMAP_H #define GUARD_TOURMALINE_DUALKEYMAP_H
#include "../Concepts.hpp" #include "../Concepts.hpp"
#include "../Systems/Logging.hpp" #include "../Systems/Logging.hpp"
#include "ContainerOptions.hpp"
#include "Hashmap.hpp" #include "Hashmap.hpp"
#include <algorithm> #include <algorithm>
@@ -28,7 +29,7 @@
namespace Tourmaline::Containers { namespace Tourmaline::Containers {
template <Concepts::Hashable AKey, Concepts::Hashable BKey, typename Value, template <Concepts::Hashable AKey, Concepts::Hashable BKey, typename Value,
uint64_t baseReservation = 2048> DualKeyMapOptions Options = {}>
class DualkeyMap { class DualkeyMap {
public: public:
// Return Types // Return Types
@@ -49,7 +50,7 @@ public:
using Entry = std::tuple<const AKey &, const BKey &, Value &>; using Entry = std::tuple<const AKey &, const BKey &, Value &>;
// Construct/Destruct // Construct/Destruct
DualkeyMap() { hashList.reserve(baseReservation); } DualkeyMap() { hashList.reserve(Options.baseReservation); }
~DualkeyMap() { ~DualkeyMap() {
// I'm sure there is a better way to do this // I'm sure there is a better way to do this
for (DualkeyHash *hash : hashList) { for (DualkeyHash *hash : hashList) {
@@ -301,7 +302,7 @@ private:
OppositeKey *oppositeKey; OppositeKey *oppositeKey;
Containers::Hashmap<OppositeKey, MultiQueryResult<OppositeKey, keyCount>, Containers::Hashmap<OppositeKey, MultiQueryResult<OppositeKey, keyCount>,
8.0f, 2048, 0.01f> // Aggressive hashmap :o {8.0f, 0.01f, 2.5f, 2048, 8}> // Aggressive hashmap :o
queryResults; queryResults;
for (DualkeyHash *hash : hashList) { for (DualkeyHash *hash : hashList) {
@@ -325,7 +326,7 @@ private:
// The code above was done to make this code more uniform // The code above was done to make this code more uniform
for (uint64_t index = 0; index < keyCount; index++) { for (uint64_t index = 0; index < keyCount; index++) {
if (keyHashes[index] == hashToCompare && keys[index] == *keyToCompare) { if (keyHashes[index] == hashToCompare && keys[index] == *keyToCompare) {
if (queryResults.Has(*oppositeKey)) { if (queryResults.Has(*oppositeKey)) [[likely]] {
auto &entry = queryResults.Get(*oppositeKey); auto &entry = queryResults.Get(*oppositeKey);
entry.valueQueryResults[index] = &hash->value; entry.valueQueryResults[index] = &hash->value;
++entry.howManyFound; ++entry.howManyFound;

View File

@@ -10,20 +10,21 @@
#define GUARD_TOURMALINE_HASHMAP_H #define GUARD_TOURMALINE_HASHMAP_H
#include "../Concepts.hpp" #include "../Concepts.hpp"
#include "../Systems/Logging.hpp" #include "../Systems/Logging.hpp"
#include "ContainerOptions.hpp"
#include <cstddef> #include <cstddef>
#include <vector> #include <vector>
namespace Tourmaline::Containers { namespace Tourmaline::Containers {
template <Concepts::Hashable Key, typename Value, float loadFactor = 0.75f, template <Concepts::Hashable Key, typename Value, HashmapOptions Options = {}>
std::size_t minimumBucketCount = 256, float minimizeFactor = 0.20f>
class Hashmap { class Hashmap {
public: public:
Hashmap() { storage.resize(minimumBucketCount); } Hashmap() { storage.resize(Options.minimumBucketCount); }
~Hashmap() { Clear(); } ~Hashmap() { Clear(); }
Value &Insert(Key key, Value value) { Value &Insert(Key key, Value value) {
if (currentLoadFactor >= loadFactor && currentlyRehashing == false) { if (currentLoadFactor >= Options.loadFactor &&
currentlyRehashing == false) {
rehash(); rehash();
} }
std::size_t keyHash = std::hash<Key>{}(key), std::size_t keyHash = std::hash<Key>{}(key),
@@ -35,6 +36,8 @@ public:
Systems::Logging::Log("Trying to insert the same key twice! Throwing...", Systems::Logging::Log("Trying to insert the same key twice! Throwing...",
"Hashmap", Systems::Logging::LogLevel::Error, "Hashmap", Systems::Logging::LogLevel::Error,
Has(key)); Has(key));
} else {
storage[keyHashPosition].reserve(Options.reservedBucketSpace);
} }
storage[keyHashPosition].emplace_back(key, std::move(value), keyHash); storage[keyHashPosition].emplace_back(key, std::move(value), keyHash);
@@ -56,7 +59,7 @@ public:
}); });
currentLoadFactor = (--count) / static_cast<float>(bucketCount); currentLoadFactor = (--count) / static_cast<float>(bucketCount);
if (currentLoadFactor <= minimizeFactor) { if (currentLoadFactor <= Options.minimizeFactor) {
rehash(); rehash();
} }
} }
@@ -113,7 +116,7 @@ public:
} }
count = 0; count = 0;
bucketCount = minimumBucketCount; bucketCount = Options.minimumBucketCount;
std::vector<bucket> newStorage; std::vector<bucket> newStorage;
storage.swap(newStorage); storage.swap(newStorage);
return result; return result;
@@ -134,16 +137,15 @@ private:
// Minimum // Minimum
goalSize = goalSize == 0 ? count : goalSize; goalSize = goalSize == 0 ? count : goalSize;
float wouldBeLoadFactor = goalSize / static_cast<float>(bucketCount); float wouldBeLoadFactor = goalSize / static_cast<float>(bucketCount);
if (wouldBeLoadFactor < loadFactor && wouldBeLoadFactor > minimizeFactor) if (wouldBeLoadFactor < Options.loadFactor &&
[[unlikely]] { wouldBeLoadFactor > Options.minimizeFactor) [[unlikely]] {
return false; // No rehashing is required return false; // No rehashing is required
} }
// Putting it closer to minimizeFactor // Putting it closer to minimizeFactor
std::size_t goalBucketCount = std::size_t goalBucketCount = goalSize / preferredLoadFactor;
goalSize / ((loadFactor + minimizeFactor) / 2.5); // Magic number if (goalBucketCount < Options.minimumBucketCount) [[unlikely]] {
if (goalBucketCount < minimumBucketCount) [[unlikely]] { goalBucketCount = Options.minimumBucketCount;
goalBucketCount = minimumBucketCount;
} }
// No need to reallocate // No need to reallocate
@@ -181,8 +183,10 @@ private:
using bucket = std::vector<hashStorage>; using bucket = std::vector<hashStorage>;
std::vector<bucket> storage; std::vector<bucket> storage;
std::size_t count = 0, bucketCount = minimumBucketCount; std::size_t count = 0, bucketCount = Options.minimumBucketCount;
float currentLoadFactor = 0; float currentLoadFactor = 0,
preferredLoadFactor = (Options.loadFactor + Options.minimizeFactor) /
Options.leaningFactor;
bool currentlyRehashing = false; // Lock for Insert in rehash bool currentlyRehashing = false; // Lock for Insert in rehash
}; };
} // namespace Tourmaline::Containers } // namespace Tourmaline::Containers

View File

@@ -10,7 +10,6 @@
#include <Systems/ECS.hpp> #include <Systems/ECS.hpp>
#include <Systems/ECS/BuiltinComponents.hpp> #include <Systems/ECS/BuiltinComponents.hpp>
#include <Systems/Random.hpp> #include <Systems/Random.hpp>
#include <optional>
using namespace Tourmaline::Systems; using namespace Tourmaline::Systems;
using namespace ECS; using namespace ECS;