Skip to content

Commit

Permalink
Add UUID generator based on two MT19937 pseudo-random number generato…
Browse files Browse the repository at this point in the history
…rs seeded by true randomness.
  • Loading branch information
ehpor committed Dec 17, 2024
1 parent 2d6905d commit fcb2140
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
32 changes: 32 additions & 0 deletions benchmarks/uuid_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "UuidGenerator.h"
#include "Timing.h"

#include <iostream>

int main()
{
const size_t N = 100000000;

UuidGenerator generator;

char uuid[16];

std::cout << std::hex;

auto start = GetTimeStamp();

for (size_t i = 0; i < N; ++i)
{
generator.GenerateUuid(uuid);
}

auto end = GetTimeStamp();

std::cout << std::dec;

std::cout << "Time: " << (end - start) / 1e9 << " sec" << std::endl;
std::cout << "Throughput: " << N / ((end - start) / 1e9) << " ops/s" << std::endl;
std::cout << "Time per operation: " << (end - start) / N << " ns" << std::endl;

return 0;
}
1 change: 1 addition & 0 deletions catkit_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_library(catkit_core STATIC
Util.cpp
PoolAllocator.cpp
FreeListAllocator.cpp
UuidGenerator.cpp
proto/core.pb.cc
proto/logging.pb.cc
proto/testbed.pb.cc
Expand Down
21 changes: 21 additions & 0 deletions catkit_core/UuidGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "UuidGenerator.h"

UuidGenerator::UuidGenerator()
{
std::random_device random_device;

for (size_t i = 0; i < 2; ++i)
{
std::seed_seq seed{random_device(), random_device()};
m_Engines[i].seed(seed);
}
}

void UuidGenerator::GenerateUuid(char *uuid)
{
for (size_t i = 0; i < 2; ++i)
{
std::uint64_t value = m_Engines[i]();
*reinterpret_cast<std::uint64_t *>(uuid + i * 8) = value;
}
}
17 changes: 17 additions & 0 deletions catkit_core/UuidGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef UUID_GENERATOR_H
#define UUID_GENERATOR_H

#include <random>

class UuidGenerator
{
public:
UuidGenerator();

void GenerateUuid(char *uuid);

private:
std::mt19937_64 m_Engines[2];
};

#endif // UUID_GENERATOR_H

0 comments on commit fcb2140

Please sign in to comment.