Changeset 40968 in webkit
- Timestamp:
- Feb 13, 2009 1:18:43 AM (15 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/ChangeLog
r40967 r40968 1 2009-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 1 27 2009-02-12 Mark Rowe <mrowe@apple.com> 2 28 -
trunk/JavaScriptCore/runtime/MathObject.cpp
r40046 r40968 28 28 #include <wtf/MathExtras.h> 29 29 #include <wtf/RandomNumber.h> 30 #include <wtf/RandomNumberSeed.h> 30 31 31 32 namespace JSC { … … 96 97 putDirectWithoutTransition(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly); 97 98 putDirectWithoutTransition(Identifier(exec, "SQRT2"), jsNumber(exec, sqrt(2.0)), DontDelete | DontEnum | ReadOnly); 99 WTF::initializeWeakRandomNumberGenerator(); 98 100 } 99 101 … … 212 214 JSValuePtr mathProtoFuncRandom(ExecState* exec, JSObject*, JSValuePtr, const ArgList&) 213 215 { 214 return jsNumber(exec, WTF:: randomNumber());216 return jsNumber(exec, WTF::weakRandomNumber()); 215 217 } 216 218 -
trunk/JavaScriptCore/wtf/RandomNumber.cpp
r40967 r40968 36 36 37 37 namespace WTF { 38 39 double 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 } 38 48 39 49 double randomNumber() -
trunk/JavaScriptCore/wtf/RandomNumber.h
r39432 r40968 29 29 namespace WTF { 30 30 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 32 33 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(); 33 39 34 40 } -
trunk/JavaScriptCore/wtf/RandomNumberSeed.h
r39558 r40968 58 58 } 59 59 60 inline 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 } 60 69 } 61 70
Note: See TracChangeset
for help on using the changeset viewer.