Changeset 40968 in webkit


Ignore:
Timestamp:
Feb 13, 2009 1:18:43 AM (15 years ago)
Author:
oliver@apple.com
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
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r40967 r40968  
     12009-02-13  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Jon Honeycutt.
     4
     5        Math.random is really slow on windows.
     6
     7        Math.random calls WTF::randomNumber which is implemented as
     8        the secure rand_s on windows.  Unfortunately rand_s is an order
     9        of magnitude slower than arc4random.  For this reason I've
     10        added "weakRandomNumber" for use by JavaScript's Math Object.
     11        In the long term we should look at using our own secure PRNG
     12        in place of the system, but this will do for now.
     13
     14        30% win on SunSpider on Windows, resolving most of the remaining
     15        disparity vs. Mac.
     16
     17        * runtime/MathObject.cpp:
     18        (JSC::MathObject::MathObject):
     19        (JSC::mathProtoFuncRandom):
     20        * wtf/RandomNumber.cpp:
     21        (WTF::weakRandomNumber):
     22        (WTF::randomNumber):
     23        * wtf/RandomNumber.h:
     24        * wtf/RandomNumberSeed.h:
     25        (WTF::initializeWeakRandomNumberGenerator):
     26
    1272009-02-12  Mark Rowe  <mrowe@apple.com>
    228
  • trunk/JavaScriptCore/runtime/MathObject.cpp

    r40046 r40968  
    2828#include <wtf/MathExtras.h>
    2929#include <wtf/RandomNumber.h>
     30#include <wtf/RandomNumberSeed.h>
    3031
    3132namespace JSC {
     
    9697    putDirectWithoutTransition(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
    9798    putDirectWithoutTransition(Identifier(exec, "SQRT2"), jsNumber(exec, sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
     99    WTF::initializeWeakRandomNumberGenerator();
    98100}
    99101
     
    212214JSValuePtr mathProtoFuncRandom(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
    213215{
    214     return jsNumber(exec, WTF::randomNumber());
     216    return jsNumber(exec, WTF::weakRandomNumber());
    215217}
    216218
  • 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.