Changeset 40968 in webkit for trunk/JavaScriptCore/wtf


Ignore:
Timestamp:
Feb 13, 2009, 1:18:43 AM (16 years ago)
Author:
[email protected]
Message:

Math.random is really slow on windows.

Reviewed by Jon Honeycutt.

Math.random calls WTF::randomNumber which is implemented as
the secure rand_s on windows. Unfortunately rand_s is an order
of magnitude slower than arc4random. For this reason I've
added "weakRandomNumber" for use by JavaScript's Math Object.
In the long term we should look at using our own secure PRNG
in place of the system, but this will do for now.

30% win on SunSpider on Windows, resolving most of the remaining
disparity vs. Mac.

Location:
trunk/JavaScriptCore/wtf
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/RandomNumber.cpp

    r40967 r40968  
    3636
    3737namespace WTF {
     38
     39double weakRandomNumber()
     40{
     41#if COMPILER(MSVC) && defined(_CRT_RAND_S)
     42    // rand_s is incredibly slow on windows so we fall back on rand for Math.random
     43    return (rand() + (rand() / (RAND_MAX + 1.0))) / (RAND_MAX + 1.0);
     44#else
     45    return randomNumber();
     46#endif
     47}
    3848
    3949double randomNumber()
  • trunk/JavaScriptCore/wtf/RandomNumber.h

    r39432 r40968  
    2929namespace WTF {
    3030
    31     // Returns a pseudo-random number in the range [0, 1)
     31    // Returns a pseudo-random number in the range [0, 1), attempts to be
     32    // cryptographically secure if possible on the target platform
    3233    double randomNumber();
     34
     35    // Returns a pseudo-random number in the range [0, 1), attempts to
     36    // produce a reasonable "random" number fast.
     37    // We only need this because rand_s is so slow on windows.
     38    double weakRandomNumber();
    3339
    3440}
  • trunk/JavaScriptCore/wtf/RandomNumberSeed.h

    r39558 r40968  
    5858}
    5959
     60inline void initializeWeakRandomNumberGenerator()
     61{
     62#if COMPILER(MSVC) && defined(_CRT_RAND_S)
     63    // We need to initialise windows rand() explicitly for Math.random
     64    unsigned seed = 0;
     65    rand_s(&seed);
     66    srand(seed);
     67#endif
     68}
    6069}
    6170
Note: See TracChangeset for help on using the changeset viewer.