[PATCH] D16419: Use std::piecewise_constant_distribution instead of ad-hoc binary search.

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 21 14:37:59 PST 2016


kcc added inline comments.

================
Comment at: lib/Fuzzer/FuzzerLoop.cpp:417
@@ -413,15 +416,3 @@
 size_t Fuzzer::ChooseUnitIdxToMutate() {
-    size_t N = Corpus.size();
-    size_t Total = (N + 1) * N / 2;
-    size_t R = USF.GetRand()(Total);
-    size_t IdxBeg = 0, IdxEnd = N;
-    // Binary search.
-    while (IdxEnd - IdxBeg >= 2) {
-      size_t Idx = IdxBeg + (IdxEnd - IdxBeg) / 2;
-      if (R > (Idx + 1) * Idx / 2)
-        IdxBeg = Idx;
-      else
-        IdxEnd = Idx;
-    }
-    assert(IdxBeg < N);
-    return IdxBeg;
+    size_t Idx = size_t(CorpusDistribution(USF.GetRand()));
+    assert(Idx < Corpus.size());
----------------
please use an explicit cast operator. 
Are you sure it will ever return the corner values (0 and N-1)? 

================
Comment at: lib/Fuzzer/FuzzerLoop.cpp:507
@@ +506,3 @@
+void Fuzzer::UpdateCorpusDistribution() {
+  std::vector<double> Intervals { 0 };
+  std::vector<double> Weights;
----------------
why do you need two vectors here, given that their values are the same? 

please avoid sp many push backs, just create a properly sized vector. 

the following loop looks like std::iota, please replace with such. 


http://reviews.llvm.org/D16419





More information about the llvm-commits mailing list