Title: [293057] branches/safari-613-branch/Source
Revision
293057
Author
ysuz...@apple.com
Date
2022-04-19 19:39:37 -0700 (Tue, 19 Apr 2022)

Log Message

Cherry-pick r292714, rdar://91584856

    [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:

    Canonical link: https://commits.webkit.org/249504@main
    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@292714 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Modified Paths

Diff

Modified: branches/safari-613-branch/Source/_javascript_Core/ChangeLog (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/ChangeLog	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/ChangeLog	2022-04-20 02:39:37 UTC (rev 293057)
@@ -1,3 +1,33 @@
+2022-04-11  Yusuke Suzuki  <ysuz...@apple.com>
+
+        [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-10  Yusuke Suzuki  <ysuz...@apple.com>
 
         [JSC] DFG / FTL should be aware of JSString's String replacement

Modified: branches/safari-613-branch/Source/_javascript_Core/assembler/AbstractMacroAssembler.cpp (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/assembler/AbstractMacroAssembler.cpp	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/assembler/AbstractMacroAssembler.cpp	2022-04-20 02:39:37 UTC (rev 293057)
@@ -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: branches/safari-613-branch/Source/_javascript_Core/assembler/AbstractMacroAssembler.h (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/assembler/AbstractMacroAssembler.h	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/assembler/AbstractMacroAssembler.h	2022-04-20 02:39:37 UTC (rev 293057)
@@ -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>
@@ -1013,23 +1026,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: branches/safari-613-branch/Source/_javascript_Core/b3/air/AirCode.cpp (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/b3/air/AirCode.cpp	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/b3/air/AirCode.cpp	2022-04-20 02:39:37 UTC (rev 293057)
@@ -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: branches/safari-613-branch/Source/_javascript_Core/b3/air/AirCode.h (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/b3/air/AirCode.h	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/b3/air/AirCode.h	2022-04-20 02:39:37 UTC (rev 293057)
@@ -355,8 +355,6 @@
     RegisterSet mutableFPRs();
     RegisterSet pinnedRegisters() const { return m_pinnedRegs; }
     
-    WeakRandom& weakRandom() { return m_weakRandom; }
-
     void emitDefaultPrologue(CCallHelpers&);
     void emitEpilogue(CCallHelpers&);
 
@@ -381,7 +379,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: branches/safari-613-branch/Source/_javascript_Core/heap/MarkedBlockInlines.h (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/heap/MarkedBlockInlines.h	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/heap/MarkedBlockInlines.h	2022-04-20 02:39:37 UTC (rev 293057)
@@ -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: branches/safari-613-branch/Source/_javascript_Core/runtime/VM.cpp (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/runtime/VM.cpp	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/runtime/VM.cpp	2022-04-20 02:39:37 UTC (rev 293057)
@@ -206,6 +206,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)
     , vmType(vmType)

Modified: branches/safari-613-branch/Source/_javascript_Core/runtime/VM.h (293056 => 293057)


--- branches/safari-613-branch/Source/_javascript_Core/runtime/VM.h	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/_javascript_Core/runtime/VM.h	2022-04-20 02:39:37 UTC (rev 293057)
@@ -286,6 +286,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; }
@@ -329,6 +330,7 @@
     Ref<WTF::RunLoop> m_runLoop;
 
     WeakRandom m_random;
+    WeakRandom m_heapRandom;
     Integrity::Random m_integrityRandom;
 
 public:

Modified: branches/safari-613-branch/Source/WTF/ChangeLog (293056 => 293057)


--- branches/safari-613-branch/Source/WTF/ChangeLog	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/WTF/ChangeLog	2022-04-20 02:39:37 UTC (rev 293057)
@@ -1,3 +1,21 @@
+2022-04-11  Yusuke Suzuki  <ysuz...@apple.com>
+
+        [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-18  Kocsen Chung  <kocsen_ch...@apple.com>
 
         Cherry-pick r292609. rdar://problem/90882766

Modified: branches/safari-613-branch/Source/WTF/wtf/UUID.cpp (293056 => 293057)


--- branches/safari-613-branch/Source/WTF/wtf/UUID.cpp	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/WTF/wtf/UUID.cpp	2022-04-20 02:39:37 UTC (rev 293057)
@@ -35,6 +35,9 @@
 #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)
 #include <sys/sysctl.h>
@@ -42,6 +45,20 @@
 
 namespace WTF {
 
+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 buffer;
+}
+
 UUID::UUID()
 {
     static_assert(sizeof(m_data) == 16);
@@ -74,6 +91,28 @@
     );
 }
 
+String createVersion4UUIDStringWeak()
+{
+    UInt128 data = ""
+    unsigned* randomData = reinterpret_cast<unsigned*>(&data);
+    static_assert(sizeof(data) == sizeof(unsigned) * 4);
+
+    // Format as Version 4 UUID.
+    return makeString(
+        hex(randomData[0], 8, Lowercase),
+        '-',
+        hex(randomData[1] >> 16, 4, Lowercase),
+        "-4",
+        hex(randomData[1] & 0x00000fff, 3, Lowercase),
+        '-',
+        hex((randomData[2] >> 30) | 0x8, 1, Lowercase),
+        hex((randomData[2] >> 16) & 0x00000fff, 3, Lowercase),
+        '-',
+        hex(randomData[2] & 0x0000ffff, 4, Lowercase),
+        hex(randomData[3], 8, Lowercase)
+    );
+}
+
 String bootSessionUUIDString()
 {
 #if OS(DARWIN)

Modified: branches/safari-613-branch/Source/WTF/wtf/UUID.h (293056 => 293057)


--- branches/safari-613-branch/Source/WTF/wtf/UUID.h	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/WTF/wtf/UUID.h	2022-04-20 02:39:37 UTC (rev 293057)
@@ -134,6 +134,7 @@
 // 9, A, or B for y.
 
 WTF_EXPORT_PRIVATE String createCanonicalUUIDString();
+WTF_EXPORT_PRIVATE String createVersion4UUIDStringWeak();
 
 WTF_EXPORT_PRIVATE String bootSessionUUIDString();
 WTF_EXPORT_PRIVATE bool isVersion4UUID(StringView);
@@ -142,4 +143,5 @@
 
 using WTF::UUID;
 using WTF::createCanonicalUUIDString;
+using WTF::createVersion4UUIDStringWeak;
 using WTF::bootSessionUUIDString;

Modified: branches/safari-613-branch/Source/WTF/wtf/WeakRandom.h (293056 => 293057)


--- branches/safari-613-branch/Source/WTF/wtf/WeakRandom.h	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/WTF/wtf/WeakRandom.h	2022-04-20 02:39:37 UTC (rev 293057)
@@ -85,6 +85,11 @@
         }
     }
 
+    uint64_t getUint64()
+    {
+        return advance();
+    }
+
     bool returnTrueWithProbability(double probability)
     {
         ASSERT(0.0 <= probability && probability <= 1.0);

Modified: branches/safari-613-branch/Source/WebCore/ChangeLog (293056 => 293057)


--- branches/safari-613-branch/Source/WebCore/ChangeLog	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/WebCore/ChangeLog	2022-04-20 02:39:37 UTC (rev 293057)
@@ -1,3 +1,17 @@
+2022-04-11  Yusuke Suzuki  <ysuz...@apple.com>
+
+        [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-10  Yusuke Suzuki  <ysuz...@apple.com>
 
         [JSC] DFG / FTL should be aware of JSString's String replacement

Modified: branches/safari-613-branch/Source/WebCore/animation/KeyframeEffect.cpp (293056 => 293057)


--- branches/safari-613-branch/Source/WebCore/animation/KeyframeEffect.cpp	2022-04-20 02:32:43 UTC (rev 293056)
+++ branches/safari-613-branch/Source/WebCore/animation/KeyframeEffect.cpp	2022-04-20 02:39:37 UTC (rev 293057)
@@ -605,7 +605,7 @@
     setIterationDuration(source->iterationDuration());
     updateStaticTimingProperties();
 
-    KeyframeList keyframeList("keyframe-effect-" + createCanonicalUUIDString());
+    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDStringWeak());
     keyframeList.copyKeyframes(source->m_blendingKeyframes);
     setBlendingKeyframes(keyframeList);
 }
@@ -920,7 +920,7 @@
     if (!m_blendingKeyframes.isEmpty() || !m_target)
         return;
 
-    KeyframeList keyframeList("keyframe-effect-" + createCanonicalUUIDString());
+    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDStringWeak());
     auto& styleResolver = m_target->styleResolver();
 
     for (auto& keyframe : m_parsedKeyframes) {
@@ -1168,7 +1168,7 @@
     if (m_target)
         Style::loadPendingResources(*toStyle, *document(), m_target.get());
 
-    KeyframeList keyframeList("keyframe-effect-" + createCanonicalUUIDString());
+    KeyframeList keyframeList("keyframe-effect-" + createVersion4UUIDStringWeak());
     keyframeList.addProperty(property);
 
     KeyframeValue fromKeyframeValue(0, RenderStyle::clonePtr(*oldStyle));
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to