RESOLVED FIXED 170045
[JSC] Use WeakRandom for SamplingProfiler interval fluctuation
https://bugs.webkit.org/show_bug.cgi?id=170045
Summary [JSC] Use WeakRandom for SamplingProfiler interval fluctuation
Yusuke Suzuki
Reported 2017-03-23 22:05:54 PDT
[JSC] Use WeakRandom for SamplingProfiler interval fluctuation
Attachments
Patch (3.45 KB, patch)
2017-03-23 22:07 PDT, Yusuke Suzuki
mark.lam: review+
Yusuke Suzuki
Comment 1 2017-03-23 22:07:01 PDT
Mark Lam
Comment 2 2017-03-23 22:18:10 PDT
Comment on attachment 305264 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=305264&action=review r=me with comment. > Source/JavaScriptCore/runtime/SamplingProfiler.cpp:279 > + , m_weakRandom(static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0))) Why the + 1.0? I don't think it is needed. I'm guessing you're trying to ensure a non-zero seed, but: 1. this does guarantee a non-zero seed because std::numeric_limits<unsigned>::max() + 1 is still 0, and 2. WeakRandom's setSeed() already guards against a 0 seed.
Yusuke Suzuki
Comment 3 2017-03-23 22:22:26 PDT
Comment on attachment 305264 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=305264&action=review Thanks! >> Source/JavaScriptCore/runtime/SamplingProfiler.cpp:279 >> + , m_weakRandom(static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0))) > > Why the + 1.0? I don't think it is needed. I'm guessing you're trying to ensure a non-zero seed, but: > 1. this does guarantee a non-zero seed because std::numeric_limits<unsigned>::max() + 1 is still 0, and > 2. WeakRandom's setSeed() already guards against a 0 seed. Originally, randomNumber() is created by dividing the result (uint32_t) of cryptographicallyRandomNumber() by `std::numeric_limits<unsigned>::max() + 1.0`. Thus, to recover it to uint32_t range, we need to multiply it by `std::numeric_limits<unsigned>::max() + 1.0`. But, maybe, using cryptographicallyRandomNumber() would be good. Why we divide cryptographicallyRandomNumber() with `std::numeric_limits<unsigned>::max() + 1.0` in `randomNumber()` is to make the double value uniformly distributed in [0, 1.0). It does not include 1.0, thus, we should not divide it with `std::numeric_limits<unsigned>::max()`. If cryptographicallyRandomNumber() produces UINT32_MAX, it produces 1.0. I'll change this code to use cryptographicallyRandomNumber() directly.
Yusuke Suzuki
Comment 4 2017-03-23 22:24:43 PDT
Comment on attachment 305264 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=305264&action=review >>> Source/JavaScriptCore/runtime/SamplingProfiler.cpp:279 >>> + , m_weakRandom(static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0))) >> >> Why the + 1.0? I don't think it is needed. I'm guessing you're trying to ensure a non-zero seed, but: >> 1. this does guarantee a non-zero seed because std::numeric_limits<unsigned>::max() + 1 is still 0, and >> 2. WeakRandom's setSeed() already guards against a 0 seed. > > Originally, randomNumber() is created by dividing the result (uint32_t) of cryptographicallyRandomNumber() by `std::numeric_limits<unsigned>::max() + 1.0`. > Thus, to recover it to uint32_t range, we need to multiply it by `std::numeric_limits<unsigned>::max() + 1.0`. > But, maybe, using cryptographicallyRandomNumber() would be good. > > Why we divide cryptographicallyRandomNumber() with `std::numeric_limits<unsigned>::max() + 1.0` in `randomNumber()` is to make the double value uniformly distributed in [0, 1.0). > It does not include 1.0, thus, we should not divide it with `std::numeric_limits<unsigned>::max()`. If cryptographicallyRandomNumber() produces UINT32_MAX, it produces 1.0. > > I'll change this code to use cryptographicallyRandomNumber() directly. Ah, instead, we just construct in a form `m_weakRandom()`. It will automatically use cryptographicallyRandomNumber() as its seed.
Yusuke Suzuki
Comment 5 2017-03-23 22:31:58 PDT
Note You need to log in before you can comment on or make changes to this bug.