UUID has been updated to use a better hash and no unnecessary allocations (Big thanks to mosra!)

This commit is contained in:
2026-01-31 13:45:15 +02:00
parent ff062567d8
commit edb0055224
2 changed files with 17 additions and 54 deletions

View File

@@ -6,18 +6,18 @@
* 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_TYPES_H
#define GUARD_TOURMALINE_TYPES_H
#include "TourmalineExternal/random/xoshiro.h"
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
namespace Tourmaline::Type {
class UUID {
public:
constexpr static uint8_t BitLength = 128;
constexpr static uint8_t QWORDLength = BitLength / 64;
constexpr static uint8_t ByteLength = BitLength / 8;
[[nodiscard]]
std::string asString() const;
bool operator==(const UUID &rhs) const;
@@ -25,14 +25,8 @@ public:
UUID(uint64_t firstHalf, uint64_t secondHalf);
UUID(const std::string &uuid);
UUID(const UUID &uuid);
UUID(UUID &&uuid) noexcept;
UUID &operator=(const UUID &uuid);
UUID &operator=(UUID &&uuid);
~UUID() = default;
private:
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(QWORDLength);
uint64_t firstHalf = 0, secondHalf = 0;
friend struct std::hash<Tourmaline::Type::UUID>;
};
} // namespace Tourmaline::Type
@@ -40,10 +34,9 @@ private:
namespace std {
template <> struct hash<Tourmaline::Type::UUID> {
size_t operator()(const Tourmaline::Type::UUID &uuid) const noexcept {
const auto data = uuid.data.get();
size_t h1 = std::hash<uint64_t>{}(data[0]),
h2 = std::hash<uint64_t>{}(data[1]);
return h1 ^ (h2 << 1);
uint64_t hash = Xoshiro::splitmix64(uuid.firstHalf);
hash += uuid.secondHalf;
return Xoshiro::splitmix64(hash);
}
};