Title: [292714] trunk/Source
Revision
292714
Author
[email protected]
Date
2022-04-11 13:10:44 -0700 (Mon, 11 Apr 2022)

Log Message

[JSC] Reduce use of unnecessary cryptographicallyRandom numbers
https://bugs.webkit.org/show_bug.cgi?id=239026

Reviewed by Saam Barati.

Source/_javascript_Core:

This patch removes cryptographically random calls in some of super hot critical path.
MarkedBlock's use is very hot and it appears on Speedometer2 artrace. But this is just
a random shuffling of freelist, and WeakRandom is enough for that. This patch replaces
them with WeakRandom. It offers 0.3% improvement in Speedometer2.

* assembler/AbstractMacroAssembler.cpp:
(JSC::AbstractMacroAssemblerBase::initializeRandom):
(WTF::printInternal):
* assembler/AbstractMacroAssembler.h:
(JSC::AbstractMacroAssemblerBase::random):
(JSC::AbstractMacroAssembler::AbstractMacroAssembler):
(JSC::AbstractMacroAssembler::random): Deleted.
* b3/air/AirCode.cpp:
(JSC::B3::Air::Code::Code):
* b3/air/AirCode.h:
(JSC::B3::Air::Code::weakRandom): Deleted.
* heap/MarkedBlockInlines.h:
(JSC::MarkedBlock::Handle::specializedSweep):
* runtime/VM.cpp:
(JSC::VM::VM):
* runtime/VM.h:
(JSC::VM::heapRandom):

Source/WebCore:

We use createVersion4UUIDStringWeak since there is no need to use cryptographically random numbers for KeyframeEffect names.

* animation/KeyframeEffect.cpp:
(WebCore::KeyframeEffect::copyPropertiesFromSource):
(WebCore::KeyframeEffect::updateBlendingKeyframes):
(WebCore::KeyframeEffect::computeCSSTransitionBlendingKeyframes):

Source/WTF:

We add createVersion4UUIDStringWeak, which can generate UUID with WeakRandom numbers.

* wtf/UUID.cpp:
(WTF::convertRandomUInt128ToUUIDVersion4):
(WTF::generateCryptographicallyRandomUUIDVersion4):
(WTF::generateWeakRandomUUIDVersion4):
(WTF::UUID::UUID):
(WTF::createVersion4UUIDStringWeak):
* wtf/UUID.h:
* wtf/WeakRandom.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (292713 => 292714)


--- trunk/Source/_javascript_Core/ChangeLog	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/ChangeLog	2022-04-11 20:10:44 UTC (rev 292714)
@@ -1,5 +1,35 @@
 2022-04-11  Yusuke Suzuki  <[email protected]>
 
+        [JSC] Reduce use of unnecessary cryptographicallyRandom numbers
+        https://bugs.webkit.org/show_bug.cgi?id=239026
+
+        Reviewed by Saam Barati.
+
+        This patch removes cryptographically random calls in some of super hot critical path.
+        MarkedBlock's use is very hot and it appears on Speedometer2 artrace. But this is just
+        a random shuffling of freelist, and WeakRandom is enough for that. This patch replaces
+        them with WeakRandom. It offers 0.3% improvement in Speedometer2.
+
+        * assembler/AbstractMacroAssembler.cpp:
+        (JSC::AbstractMacroAssemblerBase::initializeRandom):
+        (WTF::printInternal):
+        * assembler/AbstractMacroAssembler.h:
+        (JSC::AbstractMacroAssemblerBase::random):
+        (JSC::AbstractMacroAssembler::AbstractMacroAssembler):
+        (JSC::AbstractMacroAssembler::random): Deleted.
+        * b3/air/AirCode.cpp:
+        (JSC::B3::Air::Code::Code):
+        * b3/air/AirCode.h:
+        (JSC::B3::Air::Code::weakRandom): Deleted.
+        * heap/MarkedBlockInlines.h:
+        (JSC::MarkedBlock::Handle::specializedSweep):
+        * runtime/VM.cpp:
+        (JSC::VM::VM):
+        * runtime/VM.h:
+        (JSC::VM::heapRandom):
+
+2022-04-11  Yusuke Suzuki  <[email protected]>
+
         Unreviewed, use std::forward instead of WTFMove since it becomes template typename Vector&&
         https://bugs.webkit.org/show_bug.cgi?id=239025
 

Modified: trunk/Source/_javascript_Core/assembler/AbstractMacroAssembler.cpp (292713 => 292714)


--- trunk/Source/_javascript_Core/assembler/AbstractMacroAssembler.cpp	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/assembler/AbstractMacroAssembler.cpp	2022-04-11 20:10:44 UTC (rev 292714)
@@ -30,17 +30,31 @@
 
 #include <wtf/PrintStream.h>
 
+namespace JSC {
+
+void AbstractMacroAssemblerBase::initializeRandom()
+{
+    // No strong cryptographic characteristics are necessary.
+    static std::once_flag onceKey;
+    static uint32_t globalCounter;
+    std::call_once(onceKey, [&] {
+        globalCounter = cryptographicallyRandomNumber();
+    });
+    ASSERT(!m_randomSource);
+    m_randomSource.emplace(globalCounter++);
+}
+
+}
+
 namespace WTF {
 
-using namespace JSC;
-
-void printInternal(PrintStream& out, AbstractMacroAssemblerBase::StatusCondition condition)
+void printInternal(PrintStream& out, JSC::AbstractMacroAssemblerBase::StatusCondition condition)
 {
     switch (condition) {
-    case AbstractMacroAssemblerBase::Success:
+    case JSC::AbstractMacroAssemblerBase::Success:
         out.print("Success");
         return;
-    case AbstractMacroAssemblerBase::Failure:
+    case JSC::AbstractMacroAssemblerBase::Failure:
         out.print("Failure");
         return;
     }

Modified: trunk/Source/_javascript_Core/assembler/AbstractMacroAssembler.h (292713 => 292714)


--- trunk/Source/_javascript_Core/assembler/AbstractMacroAssembler.h	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/assembler/AbstractMacroAssembler.h	2022-04-11 20:10:44 UTC (rev 292714)
@@ -74,6 +74,19 @@
         RELEASE_ASSERT_NOT_REACHED();
         return Success;
     }
+
+protected:
+    uint32_t random()
+    {
+        if (!m_randomSource)
+            initializeRandom();
+        return m_randomSource->getUint32();
+    }
+
+private:
+    JS_EXPORT_PRIVATE void initializeRandom();
+
+    std::optional<WeakRandom> m_randomSource;
 };
 
 template <class AssemblerType>
@@ -1007,23 +1020,11 @@
 
 protected:
     AbstractMacroAssembler()
-        : m_randomSource(0)
-        , m_assembler()
+        : m_assembler()
     {
         invalidateAllTempRegisters();
     }
 
-    uint32_t random()
-    {
-        if (!m_randomSourceIsInitialized) {
-            m_randomSourceIsInitialized = true;
-            m_randomSource.setSeed(cryptographicallyRandomNumber());
-        }
-        return m_randomSource.getUint32();
-    }
-
-    bool m_randomSourceIsInitialized { false };
-    WeakRandom m_randomSource;
 public:
     AssemblerType m_assembler;
 protected:

Modified: trunk/Source/_javascript_Core/b3/air/AirCode.cpp (292713 => 292714)


--- trunk/Source/_javascript_Core/b3/air/AirCode.cpp	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/b3/air/AirCode.cpp	2022-04-11 20:10:44 UTC (rev 292714)
@@ -61,8 +61,11 @@
     , m_defaultPrologueGenerator(createSharedTask<PrologueGeneratorFunction>(&defaultPrologueGenerator))
 {
     // Come up with initial orderings of registers. The user may replace this with something else.
+    std::optional<WeakRandom> weakRandom;
+    if (Options::airRandomizeRegs())
+        weakRandom.emplace();
     forEachBank(
-        [&] (Bank bank) {
+        [&](Bank bank) {
             Vector<Reg> volatileRegs;
             Vector<Reg> calleeSaveRegs;
             RegisterSet all = bank == GP ? RegisterSet::allGPRs() : RegisterSet::allFPRs();
@@ -80,7 +83,7 @@
                         calleeSaveRegs.append(reg);
                 });
             if (Options::airRandomizeRegs()) {
-                WeakRandom random(Options::airRandomizeRegsSeed() ? Options::airRandomizeRegsSeed() : m_weakRandom.getUint32());
+                WeakRandom random(Options::airRandomizeRegsSeed() ? Options::airRandomizeRegsSeed() : weakRandom->getUint32());
                 shuffleVector(volatileRegs, [&] (unsigned limit) { return random.getUint32(limit); });
                 shuffleVector(calleeSaveRegs, [&] (unsigned limit) { return random.getUint32(limit); });
             }

Modified: trunk/Source/_javascript_Core/b3/air/AirCode.h (292713 => 292714)


--- trunk/Source/_javascript_Core/b3/air/AirCode.h	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/b3/air/AirCode.h	2022-04-11 20:10:44 UTC (rev 292714)
@@ -355,8 +355,6 @@
     RegisterSet mutableFPRs();
     RegisterSet pinnedRegisters() const { return m_pinnedRegs; }
     
-    WeakRandom& weakRandom() { return m_weakRandom; }
-
     void emitDefaultPrologue(CCallHelpers&);
     void emitEpilogue(CCallHelpers&);
 
@@ -384,7 +382,6 @@
         ASSERT_NOT_REACHED();
     }
 
-    WeakRandom m_weakRandom;
     Procedure& m_proc; // Some meta-data, like byproducts, is stored in the Procedure.
     Vector<Reg> m_gpRegsInPriorityOrder;
     Vector<Reg> m_fpRegsInPriorityOrder;

Modified: trunk/Source/_javascript_Core/heap/MarkedBlockInlines.h (292713 => 292714)


--- trunk/Source/_javascript_Core/heap/MarkedBlockInlines.h	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/heap/MarkedBlockInlines.h	2022-04-11 20:10:44 UTC (rev 292714)
@@ -308,8 +308,7 @@
     // order of the free list.
     FreeCell* head = nullptr;
     size_t count = 0;
-    uintptr_t secret;
-    cryptographicallyRandomValues(&secret, sizeof(uintptr_t));
+    uintptr_t secret = static_cast<uintptr_t>(vm.heapRandom().getUint64());
     bool isEmpty = true;
     Vector<size_t> deadCells;
     auto handleDeadCell = [&] (size_t i) {

Modified: trunk/Source/_javascript_Core/runtime/VM.cpp (292713 => 292714)


--- trunk/Source/_javascript_Core/runtime/VM.cpp	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/runtime/VM.cpp	2022-04-11 20:10:44 UTC (rev 292714)
@@ -205,6 +205,7 @@
     , m_apiLock(adoptRef(new JSLock(this)))
     , m_runLoop(runLoop ? *runLoop : WTF::RunLoop::current())
     , m_random(Options::seedOfVMRandomForFuzzer() ? Options::seedOfVMRandomForFuzzer() : cryptographicallyRandomNumber())
+    , m_heapRandom(Options::seedOfVMRandomForFuzzer() ? Options::seedOfVMRandomForFuzzer() : cryptographicallyRandomNumber())
     , m_integrityRandom(*this)
     , heap(*this, heapType)
     , clientHeap(heap)

Modified: trunk/Source/_javascript_Core/runtime/VM.h (292713 => 292714)


--- trunk/Source/_javascript_Core/runtime/VM.h	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/_javascript_Core/runtime/VM.h	2022-04-11 20:10:44 UTC (rev 292714)
@@ -294,6 +294,7 @@
     JS_EXPORT_PRIVATE JSGlobalObject* deprecatedVMEntryGlobalObject(JSGlobalObject*) const;
 
     WeakRandom& random() { return m_random; }
+    WeakRandom& heapRandom() { return m_heapRandom; }
     Integrity::Random& integrityRandom() { return m_integrityRandom; }
 
     bool terminationInProgress() const { return m_terminationInProgress; }
@@ -337,6 +338,7 @@
     Ref<WTF::RunLoop> m_runLoop;
 
     WeakRandom m_random;
+    WeakRandom m_heapRandom;
     Integrity::Random m_integrityRandom;
 
 public:

Modified: trunk/Source/WTF/ChangeLog (292713 => 292714)


--- trunk/Source/WTF/ChangeLog	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/WTF/ChangeLog	2022-04-11 20:10:44 UTC (rev 292714)
@@ -1,3 +1,21 @@
+2022-04-11  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Reduce use of unnecessary cryptographicallyRandom numbers
+        https://bugs.webkit.org/show_bug.cgi?id=239026
+
+        Reviewed by Saam Barati.
+
+        We add createVersion4UUIDStringWeak, which can generate UUID with WeakRandom numbers.
+
+        * wtf/UUID.cpp:
+        (WTF::convertRandomUInt128ToUUIDVersion4):
+        (WTF::generateCryptographicallyRandomUUIDVersion4):
+        (WTF::generateWeakRandomUUIDVersion4):
+        (WTF::UUID::UUID):
+        (WTF::createVersion4UUIDStringWeak):
+        * wtf/UUID.h:
+        * wtf/WeakRandom.h:
+
 2022-04-10  Chris Dumez  <[email protected]>
 
         Unreviewed Windows build fix after r292696.

Modified: trunk/Source/WTF/wtf/UUID.cpp (292713 => 292714)


--- trunk/Source/WTF/wtf/UUID.cpp	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/WTF/wtf/UUID.cpp	2022-04-11 20:10:44 UTC (rev 292714)
@@ -35,6 +35,8 @@
 #include <wtf/ASCIICType.h>
 #include <wtf/CryptographicallyRandomNumber.h>
 #include <wtf/HexNumber.h>
+#include <wtf/Lock.h>
+#include <wtf/WeakRandom.h>
 #include <wtf/text/StringToIntegerConversion.h>
 
 #if OS(DARWIN)
@@ -43,20 +45,42 @@
 
 namespace WTF {
 
-UUID::UUID()
+static ALWAYS_INLINE UInt128 convertRandomUInt128ToUUIDVersion4(UInt128 buffer)
 {
-    static_assert(sizeof(m_data) == 16);
-    auto* data = "" char*>(&m_data);
+    // By default, we generate a v4 UUID value, as per https://datatracker.ietf.org/doc/html/rfc4122#section-4.4.
+    auto high = static_cast<uint64_t>((buffer >> 64) & 0xffffffffffff0fff) | 0x4000;
+    auto low = static_cast<uint64_t>(buffer & 0x3fffffffffffffff) | 0x8000000000000000;
 
-    cryptographicallyRandomValues(data, 16);
+    return (static_cast<UInt128>(high) << 64) | low;
+}
 
-    // By default, we generate a v4 UUID value, as per https://datatracker.ietf.org/doc/html/rfc4122#section-4.4.
-    auto high = static_cast<uint64_t>((m_data >> 64) & 0xffffffffffff0fff) | 0x4000;
-    auto low = static_cast<uint64_t>(m_data & 0x3fffffffffffffff) | 0x8000000000000000;
+static UInt128 generateCryptographicallyRandomUUIDVersion4()
+{
+    UInt128 buffer { };
+    static_assert(sizeof(buffer) == 16);
+    cryptographicallyRandomValues(reinterpret_cast<unsigned char*>(&buffer), 16);
+    return convertRandomUInt128ToUUIDVersion4(buffer);
+}
 
-    m_data = (static_cast<UInt128>(high) << 64) | low;
+static UInt128 generateWeakRandomUUIDVersion4()
+{
+    static Lock lock;
+    UInt128 buffer { 0 };
+    {
+        Locker locker { lock };
+        static std::optional<WeakRandom> weakRandom;
+        if (!weakRandom)
+            weakRandom.emplace();
+        buffer = static_cast<UInt128>(weakRandom->getUint64()) << 64 | weakRandom->getUint64();
+    }
+    return convertRandomUInt128ToUUIDVersion4(buffer);
 }
 
+UUID::UUID()
+    : m_data(generateCryptographicallyRandomUUIDVersion4())
+{
+}
+
 String UUID::toString() const
 {
     auto high = static_cast<uint64_t>(m_data >> 64);
@@ -142,6 +166,11 @@
     return UUID::createVersion4().toString();
 }
 
+String createVersion4UUIDStringWeak()
+{
+    return UUID(generateWeakRandomUUIDVersion4()).toString();
+}
+
 String bootSessionUUIDString()
 {
 #if OS(DARWIN)

Modified: trunk/Source/WTF/wtf/UUID.h (292713 => 292714)


--- trunk/Source/WTF/wtf/UUID.h	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/WTF/wtf/UUID.h	2022-04-11 20:10:44 UTC (rev 292714)
@@ -148,6 +148,7 @@
 // 9, A, or B for y.
 
 WTF_EXPORT_PRIVATE String createVersion4UUIDString();
+WTF_EXPORT_PRIVATE String createVersion4UUIDStringWeak();
 
 WTF_EXPORT_PRIVATE String bootSessionUUIDString();
 WTF_EXPORT_PRIVATE bool isVersion4UUID(StringView);
@@ -156,4 +157,5 @@
 
 using WTF::UUID;
 using WTF::createVersion4UUIDString;
+using WTF::createVersion4UUIDStringWeak;
 using WTF::bootSessionUUIDString;

Modified: trunk/Source/WTF/wtf/WeakRandom.h (292713 => 292714)


--- trunk/Source/WTF/wtf/WeakRandom.h	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/WTF/wtf/WeakRandom.h	2022-04-11 20:10:44 UTC (rev 292714)
@@ -85,6 +85,11 @@
         }
     }
 
+    uint64_t getUint64()
+    {
+        return advance();
+    }
+
     bool returnTrueWithProbability(double probability)
     {
         ASSERT(0.0 <= probability && probability <= 1.0);

Modified: trunk/Source/WebCore/ChangeLog (292713 => 292714)


--- trunk/Source/WebCore/ChangeLog	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/WebCore/ChangeLog	2022-04-11 20:10:44 UTC (rev 292714)
@@ -1,3 +1,17 @@
+2022-04-11  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Reduce use of unnecessary cryptographicallyRandom numbers
+        https://bugs.webkit.org/show_bug.cgi?id=239026
+
+        Reviewed by Saam Barati.
+
+        We use createVersion4UUIDStringWeak since there is no need to use cryptographically random numbers for KeyframeEffect names.
+
+        * animation/KeyframeEffect.cpp:
+        (WebCore::KeyframeEffect::copyPropertiesFromSource):
+        (WebCore::KeyframeEffect::updateBlendingKeyframes):
+        (WebCore::KeyframeEffect::computeCSSTransitionBlendingKeyframes):
+
 2022-04-11  Wenson Hsieh  <[email protected]>
 
         [Mail Compose] Preserve attachment identifiers when cloning attachment-backed images

Modified: trunk/Source/WebCore/animation/KeyframeEffect.cpp (292713 => 292714)


--- trunk/Source/WebCore/animation/KeyframeEffect.cpp	2022-04-11 20:04:18 UTC (rev 292713)
+++ trunk/Source/WebCore/animation/KeyframeEffect.cpp	2022-04-11 20:10:44 UTC (rev 292714)
@@ -585,7 +585,7 @@
     setIterationDuration(source->iterationDuration());
     updateStaticTimingProperties();
 
-    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDString());
+    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDStringWeak());
     keyframeList.copyKeyframes(source->m_blendingKeyframes);
     setBlendingKeyframes(keyframeList);
 }
@@ -811,7 +811,7 @@
     if (!m_blendingKeyframes.isEmpty() || !m_target)
         return;
 
-    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDString());
+    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDStringWeak());
     auto& styleResolver = m_target->styleResolver();
 
     for (auto& keyframe : m_parsedKeyframes) {
@@ -1037,7 +1037,7 @@
     if (m_target)
         Style::loadPendingResources(*toStyle, *document(), m_target.get());
 
-    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDString());
+    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDStringWeak());
     keyframeList.addProperty(property);
 
     KeyframeValue fromKeyframeValue(0, RenderStyle::clonePtr(*oldStyle));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to