Skip to content

Commit d2e044a

Browse files
committed
Refactor Zipfian precomputed zeta handling
1 parent f227973 commit d2e044a

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

core/scrambled_zipfian_generator.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ class ScrambledZipfianGenerator : public Generator<uint64_t> {
2222
public:
2323
ScrambledZipfianGenerator(uint64_t min, uint64_t max, double zipfian_const) :
2424
base_(min), num_items_(max - min + 1),
25-
generator_(zipfian_const == kUsedZipfianConstant ?
26-
ZipfianGenerator(0, kItemCount, zipfian_const, kZetan) :
27-
ZipfianGenerator(0, kItemCount, zipfian_const)) { }
25+
generator_(0, kItemCount - 1, zipfian_const) { }
2826

2927
ScrambledZipfianGenerator(uint64_t min, uint64_t max) :
3028
ScrambledZipfianGenerator(min, max, ZipfianGenerator::kZipfianConst) { }
@@ -36,8 +34,6 @@ class ScrambledZipfianGenerator : public Generator<uint64_t> {
3634
uint64_t Last();
3735

3836
private:
39-
static constexpr double kUsedZipfianConstant = 0.99;
40-
static constexpr double kZetan = 26.46902820178302;
4137
static constexpr uint64_t kItemCount = 10000000000LL;
4238
const uint64_t base_;
4339
const uint64_t num_items_;

core/zipfian_generator.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,33 @@ class ZipfianGenerator : public Generator<uint64_t> {
2323
public:
2424
static constexpr double kZipfianConst = 0.99;
2525
static constexpr uint64_t kMaxNumItems = (UINT64_MAX >> 24);
26+
static constexpr uint64_t kPrecomputedZetaItems = 10000000000LL;
27+
static constexpr double kPrecomputedZetaTheta = 0.99;
28+
static constexpr double kPrecomputedZeta = 26.46902820178302;
2629

2730
ZipfianGenerator(uint64_t num_items) :
2831
ZipfianGenerator(0, num_items - 1) {}
2932

3033
ZipfianGenerator(uint64_t min, uint64_t max, double zipfian_const = kZipfianConst) :
31-
ZipfianGenerator(min, max, zipfian_const, Zeta(max - min + 1, zipfian_const)) {}
34+
items_(max - min + 1), base_(min), theta_(zipfian_const), allow_count_decrease_(false) {
35+
assert(items_ >= 2 && items_ < kMaxNumItems);
36+
Init(ZetaFor(items_, theta_));
37+
}
3238

3339
ZipfianGenerator(uint64_t min, uint64_t max, double zipfian_const, double zeta_n) :
3440
items_(max - min + 1), base_(min), theta_(zipfian_const), allow_count_decrease_(false) {
3541
assert(items_ >= 2 && items_ < kMaxNumItems);
42+
Init(zeta_n);
43+
}
44+
45+
uint64_t Next(uint64_t num_items);
3646

47+
uint64_t Next() { return Next(items_); }
48+
49+
uint64_t Last();
50+
51+
private:
52+
void Init(double zeta_n) {
3753
zeta_2_ = Zeta(2, theta_);
3854

3955
alpha_ = 1.0 / (1.0 - theta_);
@@ -44,13 +60,6 @@ class ZipfianGenerator : public Generator<uint64_t> {
4460
Next();
4561
}
4662

47-
uint64_t Next(uint64_t num_items);
48-
49-
uint64_t Next() { return Next(items_); }
50-
51-
uint64_t Last();
52-
53-
private:
5463
double Eta() {
5564
return (1 - std::pow(2.0 / items_, 1 - theta_)) / (1 - zeta_2_ / zeta_n_);
5665
}
@@ -73,6 +82,13 @@ class ZipfianGenerator : public Generator<uint64_t> {
7382
return Zeta(0, num, theta, 0);
7483
}
7584

85+
static double ZetaFor(uint64_t num, double theta) {
86+
if (num == kPrecomputedZetaItems && theta == kPrecomputedZetaTheta) {
87+
return kPrecomputedZeta;
88+
}
89+
return Zeta(num, theta);
90+
}
91+
7692
uint64_t items_;
7793
uint64_t base_; /// Min number of items to generate
7894

utils/utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ inline uint32_t ThreadLocalRandomInt() {
5454
return rn();
5555
}
5656

57-
inline double ThreadLocalRandomDouble(double min = 0.0, double max = 1.0) {
57+
inline double ThreadLocalRandomDouble() {
5858
static thread_local std::random_device rd;
5959
static thread_local std::minstd_rand rn(rd());
60-
static thread_local std::uniform_real_distribution<double> uniform(min, max);
60+
static thread_local std::uniform_real_distribution<double> uniform(0.0, 1.0);
6161
return uniform(rn);
6262
}
6363

0 commit comments

Comments
 (0)