Skip to content

Commit 63b032e

Browse files
authored
fix(rowstorage): SplitMix64 PRNG implementation to replace stdlib MT PRNG that uses /dev/urandom guarded by spinlock (#3050)
1 parent 58e18ee commit 63b032e

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

utils/rowgroup/rowstorage.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -1495,9 +1495,7 @@ RowAggStorage::RowAggStorage(const std::string& tmpDir, RowGroup* rowGroupOut, R
14951495
, fTmpDir(tmpDir)
14961496
, fRowGroupOut(rowGroupOut)
14971497
, fKeysRowGroup(keysRowGroup)
1498-
, fRD()
1499-
, fRandGen(fRD())
1500-
, fRandDistr(0, 100)
1498+
, fRandom(reinterpret_cast<std::uintptr_t>(this))
15011499
{
15021500
char suffix[PATH_MAX];
15031501
snprintf(suffix, sizeof(suffix), "/p%u-t%p/", getpid(), this);
@@ -1683,15 +1681,15 @@ void RowAggStorage::dump()
16831681
break;
16841682
}
16851683

1686-
int64_t totalMem = fMM->getConfigured();
1684+
const int64_t totalMem = fMM->getConfigured();
16871685
// If the generations are allowed and there are less than half of
16881686
// rowgroups in memory, then we start a new generation
16891687
if (fAllowGenerations && fStorage->fLRU->size() < fStorage->fRGDatas.size() / 2 &&
16901688
fStorage->fRGDatas.size() > 10)
16911689
{
16921690
startNewGeneration();
16931691
}
1694-
else if (fAllowGenerations && freeMem < totalMem / 10 * 3 && fRandDistr(fRandGen) < 30)
1692+
else if (fAllowGenerations && freeMem < totalMem / 10 * 3 && nextRandDistib() < 30)
16951693
{
16961694
startNewGeneration();
16971695
}

utils/rowgroup/rowstorage.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ class RowAggStorage
311311
static constexpr uint8_t INIT_INFO_HASH_SHIFT{0};
312312
static constexpr uint16_t MAX_INMEMORY_GENS{4};
313313

314+
// This is SplitMix64 implementation borrowed from here
315+
// https://thompsonsed.co.uk/random-number-generators-for-c-performance-tested
316+
inline uint64_t nextRandom()
317+
{
318+
uint64_t z = (fRandom += UINT64_C(0x9E3779B97F4A7C15));
319+
z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
320+
z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
321+
return z ^ (z >> 31);
322+
}
323+
324+
inline uint64_t nextRandDistib()
325+
{
326+
return nextRandom() % 100;
327+
}
328+
314329
struct Data
315330
{
316331
RowPosHashStoragePtr fHashes;
@@ -349,9 +364,7 @@ class RowAggStorage
349364
bool fInitialized{false};
350365
rowgroup::RowGroup* fRowGroupOut;
351366
rowgroup::RowGroup* fKeysRowGroup;
352-
std::random_device fRD;
353-
std::mt19937 fRandGen;
354-
std::uniform_int_distribution<uint8_t> fRandDistr;
367+
uint64_t fRandom = 0xc4ceb9fe1a85ec53ULL; // initial integer to set PRNG up
355368
};
356369

357370
} // namespace rowgroup

0 commit comments

Comments
 (0)