Skip to content

Commit

Permalink
[JSC] Make CachedCall faster
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264246
rdar://117989641

Reviewed by Michael Saboff.

This patch makes CachedCall 2x faster. We integrate CallLinkInfo / PolymorphicCallNode's CodeBlock linking mechanism into CachedCall.

1. This patch unifies CallLinkInfo and PolymorphicCallNode's linking mechanism into CallLinkInfoBase. Now both class has CallLinkInfoBase
   and CodeBlock handles both via linkIncomingCall in the same way.
2. Extend this for CachedCall too.

The key is that, previously, we are repeatedly checking CodeBlock's update for CachedCall. But now, it is notified by the CodeBlock itself.
So we do not need to check this costly thing frequently. This is exactly the same mechanism to CallLinkInfo and PolymorphicCallNode.

This improves CachedCall by 2x.

    cpp-to-js-cached-call       54.9806+-0.0144     ^     21.1277+-0.0073        ^ definitely 2.6023x faster

* JSTests/microbenchmarks/js-to-js-cached-call.js: Added.
(test):
(cachedCallFromJS):
* Source/JavaScriptCore/CMakeLists.txt:
* Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj:
* Source/JavaScriptCore/Sources.txt:
* Source/JavaScriptCore/bytecode/CallLinkInfo.cpp:
(JSC::CallLinkInfo::~CallLinkInfo):
(JSC::CallLinkInfo::unlinkImpl):
(JSC::CallLinkInfo::unlink): Deleted.
* Source/JavaScriptCore/bytecode/CallLinkInfo.h:
(JSC::CallLinkInfo::CallLinkInfo):
* Source/JavaScriptCore/bytecode/CallLinkInfoBase.cpp: Added.
(JSC::CallLinkInfoBase::unlink):
* Source/JavaScriptCore/bytecode/CallLinkInfoBase.h: Added.
(JSC::CallLinkInfoBase::CallLinkInfoBase):
(JSC::CallLinkInfoBase::~CallLinkInfoBase):
(JSC::CallLinkInfoBase::callSiteType const):
* Source/JavaScriptCore/bytecode/CallLinkStatus.cpp:
(JSC::CallLinkStatus::computeFromCallLinkInfo):
* Source/JavaScriptCore/bytecode/CodeBlock.cpp:
(JSC::CodeBlock::linkIncomingCall):
(JSC::CodeBlock::unlinkIncomingCalls):
(JSC::CodeBlock::linkIncomingPolymorphicCall): Deleted.
* Source/JavaScriptCore/bytecode/CodeBlock.h:
* Source/JavaScriptCore/interpreter/CachedCall.h:
(JSC::CachedCall::CachedCall):
(JSC::CachedCall::~CachedCall):
(JSC::CachedCall::unlinkImpl):
(JSC::CachedCall::relink):
* Source/JavaScriptCore/interpreter/Interpreter.cpp:
(JSC::Interpreter::prepareForCachedCall):
* Source/JavaScriptCore/interpreter/Interpreter.h:
* Source/JavaScriptCore/interpreter/InterpreterInlines.h:
(JSC::Interpreter::executeCachedCall):
* Source/JavaScriptCore/jit/PolymorphicCallStubRoutine.cpp:
(JSC::PolymorphicCallNode::unlinkImpl):
(JSC::PolymorphicCallStubRoutine::PolymorphicCallStubRoutine):
(JSC::CallLinkInfoBase::unlink):
(JSC::PolymorphicCallNode::~PolymorphicCallNode): Deleted.
(JSC::PolymorphicCallNode::unlink): Deleted.
* Source/JavaScriptCore/jit/PolymorphicCallStubRoutine.h:
(JSC::PolymorphicCallNode::PolymorphicCallNode): Deleted.
(JSC::PolymorphicCallNode::hasCallLinkInfo): Deleted.

Canonical link: https://commits.webkit.org/270347@main
  • Loading branch information
Constellation committed Nov 7, 2023
1 parent 6dd6fa0 commit 52bceb7
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 77 deletions.
11 changes: 11 additions & 0 deletions JSTests/microbenchmarks/js-to-js-cached-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function test() { }
noInline(test);

function cachedCallFromJS(func, times)
{
for (var i = 0; i < times; ++i)
func();
}
noInline(cachedCallFromJS);

cachedCallFromJS(test, 4e6);
1 change: 1 addition & 0 deletions Source/JavaScriptCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ set(JavaScriptCore_PRIVATE_FRAMEWORK_HEADERS
bytecode/BytecodeIntrinsicRegistry.h
bytecode/CallEdge.h
bytecode/CallLinkInfo.h
bytecode/CallLinkInfoBase.h
bytecode/CallMode.h
bytecode/CallVariant.h
bytecode/CallVariantInlines.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,7 @@
E34EDBF71DB5FFC900DC87A5 /* FrameTracers.h in Headers */ = {isa = PBXBuildFile; fileRef = E34EDBF61DB5FFC100DC87A5 /* FrameTracers.h */; settings = {ATTRIBUTES = (Private, ); }; };
E34F930E2322D882002B8DB4 /* JSGenerator.h in Headers */ = {isa = PBXBuildFile; fileRef = E34F930C2322D881002B8DB4 /* JSGenerator.h */; settings = {ATTRIBUTES = (Private, ); }; };
E350708A1DC49BBF0089BCD6 /* DOMJITSignature.h in Headers */ = {isa = PBXBuildFile; fileRef = E35070891DC49BB60089BCD6 /* DOMJITSignature.h */; settings = {ATTRIBUTES = (Private, ); }; };
E352576A2AF97DE400BD4754 /* CallLinkInfoBase.h in Headers */ = {isa = PBXBuildFile; fileRef = E35257682AF97DE300BD4754 /* CallLinkInfoBase.h */; settings = {ATTRIBUTES = (Private, ); }; };
E353C11D24AA4CB7003FBDF3 /* IntlDisplayNames.h in Headers */ = {isa = PBXBuildFile; fileRef = E353C11724AA4CB6003FBDF3 /* IntlDisplayNames.h */; };
E353C11E24AA4CB7003FBDF3 /* IntlDisplayNamesConstructor.h in Headers */ = {isa = PBXBuildFile; fileRef = E353C11824AA4CB6003FBDF3 /* IntlDisplayNamesConstructor.h */; };
E353C12124AA4CB7003FBDF3 /* IntlDisplayNamesPrototype.h in Headers */ = {isa = PBXBuildFile; fileRef = E353C11B24AA4CB7003FBDF3 /* IntlDisplayNamesPrototype.h */; };
Expand Down Expand Up @@ -5604,6 +5605,8 @@
E34F930B2322D881002B8DB4 /* JSGenerator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSGenerator.cpp; sourceTree = "<group>"; };
E34F930C2322D881002B8DB4 /* JSGenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSGenerator.h; sourceTree = "<group>"; };
E35070891DC49BB60089BCD6 /* DOMJITSignature.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DOMJITSignature.h; sourceTree = "<group>"; };
E35257672AF97DE300BD4754 /* CallLinkInfoBase.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CallLinkInfoBase.cpp; sourceTree = "<group>"; };
E35257682AF97DE300BD4754 /* CallLinkInfoBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CallLinkInfoBase.h; sourceTree = "<group>"; };
E353C11624AA4CB5003FBDF3 /* IntlDisplayNames.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntlDisplayNames.cpp; sourceTree = "<group>"; };
E353C11724AA4CB6003FBDF3 /* IntlDisplayNames.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntlDisplayNames.h; sourceTree = "<group>"; };
E353C11824AA4CB6003FBDF3 /* IntlDisplayNamesConstructor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntlDisplayNamesConstructor.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9363,6 +9366,8 @@
0F64B2781A7957B2006E4E66 /* CallEdge.h */,
0F0B83AE14BCF71400885B4F /* CallLinkInfo.cpp */,
0F0B83AF14BCF71400885B4F /* CallLinkInfo.h */,
E35257672AF97DE300BD4754 /* CallLinkInfoBase.cpp */,
E35257682AF97DE300BD4754 /* CallLinkInfoBase.h */,
0F93329314CA7DC10085F3C6 /* CallLinkStatus.cpp */,
0F93329414CA7DC10085F3C6 /* CallLinkStatus.h */,
627673211B680C1E00FD9F2E /* CallMode.cpp */,
Expand Down Expand Up @@ -10411,6 +10416,7 @@
62EC9BB71B7EB07C00303AD1 /* CallFrameShuffleData.h in Headers */,
62D755D71B84FB4A001801FA /* CallFrameShuffler.h in Headers */,
0F0B83B114BCF71800885B4F /* CallLinkInfo.h in Headers */,
E352576A2AF97DE400BD4754 /* CallLinkInfoBase.h in Headers */,
0F93329E14CA7DC50085F3C6 /* CallLinkStatus.h in Headers */,
627673241B680C1E00FD9F2E /* CallMode.h in Headers */,
0F3B7E2B19A11B8000D9BC56 /* CallVariant.h in Headers */,
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ bytecode/BytecodeRewriter.cpp
bytecode/BytecodeUseDef.cpp
bytecode/CallEdge.cpp
bytecode/CallLinkInfo.cpp
bytecode/CallLinkInfoBase.cpp
bytecode/CallLinkStatus.cpp
bytecode/CallMode.cpp
bytecode/CallVariant.cpp
Expand Down
5 changes: 1 addition & 4 deletions Source/JavaScriptCore/bytecode/CallLinkInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ CallLinkInfo::CallType CallLinkInfo::callTypeFor(OpcodeID opcodeID)
CallLinkInfo::~CallLinkInfo()
{
clearStub();

if (isOnList())
remove();
}

void CallLinkInfo::clearStub()
Expand All @@ -92,7 +89,7 @@ void CallLinkInfo::clearStub()
#endif
}

void CallLinkInfo::unlink(VM& vm)
void CallLinkInfo::unlinkImpl(VM& vm)
{
// We could be called even if we're not linked anymore because of how polymorphic calls
// work. Each callsite within the polymorphic call stub may separately ask us to unlink().
Expand Down
29 changes: 16 additions & 13 deletions Source/JavaScriptCore/bytecode/CallLinkInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "BaselineJITRegisters.h"
#include "CallFrameShuffleData.h"
#include "CallLinkInfoBase.h"
#include "CallMode.h"
#include "CodeLocation.h"
#include "CodeOrigin.h"
Expand Down Expand Up @@ -55,7 +56,7 @@ struct BaselineUnlinkedCallLinkInfo;

using CompileTimeCallLinkInfo = std::variant<OptimizingCallLinkInfo*, BaselineUnlinkedCallLinkInfo*, DFG::UnlinkedCallLinkInfo*>;

class CallLinkInfo : public PackedRawSentinelNode<CallLinkInfo> {
class CallLinkInfo : public CallLinkInfoBase {
public:
friend class LLIntOffsetsExtractor;

Expand Down Expand Up @@ -178,7 +179,7 @@ class CallLinkInfo : public PackedRawSentinelNode<CallLinkInfo> {
}

bool isLinked() const { return stub() || m_calleeOrCodeBlock; }
void unlink(VM&);
void unlinkImpl(VM&);

enum class UseDataIC : bool { No, Yes };

Expand Down Expand Up @@ -369,7 +370,8 @@ class CallLinkInfo : public PackedRawSentinelNode<CallLinkInfo> {

protected:
CallLinkInfo(Type type, UseDataIC useDataIC)
: m_hasSeenShouldRepatch(false)
: CallLinkInfoBase(CallSiteType::CallLinkInfo)
, m_hasSeenShouldRepatch(false)
, m_hasSeenClosure(false)
, m_clearedByGC(false)
, m_clearedByVirtual(false)
Expand All @@ -382,7 +384,16 @@ class CallLinkInfo : public PackedRawSentinelNode<CallLinkInfo> {
ASSERT(useDataIC == this->useDataIC());
}

uint32_t m_slowPathCount { 0 };
bool m_hasSeenShouldRepatch : 1;
bool m_hasSeenClosure : 1;
bool m_clearedByGC : 1;
bool m_clearedByVirtual : 1;
bool m_allowStubs : 1;
unsigned m_callType : 4; // CallType
unsigned m_useDataIC : 1; // UseDataIC
unsigned m_type : 1; // Type
uint8_t m_maxArgumentCountIncludingThisForVarargs { 0 }; // For varargs: the profiled maximum number of arguments. For direct: the number of stack slots allocated for arguments.

CodeLocationLabel<JSInternalPtrTag> m_doneLocation;
CodePtr<JSEntryPtrTag> m_slowPathCallDestination;
union UnionType {
Expand All @@ -406,15 +417,7 @@ class CallLinkInfo : public PackedRawSentinelNode<CallLinkInfo> {
#if ENABLE(JIT)
RefPtr<PolymorphicCallStubRoutine> m_stub;
#endif
bool m_hasSeenShouldRepatch : 1;
bool m_hasSeenClosure : 1;
bool m_clearedByGC : 1;
bool m_clearedByVirtual : 1;
bool m_allowStubs : 1;
unsigned m_callType : 4; // CallType
unsigned m_useDataIC : 1; // UseDataIC
unsigned m_type : 1; // Type
uint8_t m_maxArgumentCountIncludingThisForVarargs { 0 }; // For varargs: the profiled maximum number of arguments. For direct: the number of stack slots allocated for arguments.
uint32_t m_slowPathCount { 0 };
};

class BaselineCallLinkInfo final : public CallLinkInfo {
Expand Down
52 changes: 52 additions & 0 deletions Source/JavaScriptCore/bytecode/CallLinkInfoBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "CallLinkInfoBase.h"

#include "CachedCall.h"
#include "CallLinkInfo.h"
#include "PolymorphicCallStubRoutine.h"

namespace JSC {

void CallLinkInfoBase::unlink(VM& vm)
{
switch (callSiteType()) {
case CallSiteType::CallLinkInfo:
static_cast<CallLinkInfo*>(this)->unlinkImpl(vm);
break;
#if ENABLE(JIT)
case CallSiteType::PolymorphicCallNode:
static_cast<PolymorphicCallNode*>(this)->unlinkImpl(vm);
break;
#endif
case CallSiteType::CachedCall:
static_cast<CachedCall*>(this)->unlinkImpl(vm);
break;
}
}

} // namespace JSC
63 changes: 63 additions & 0 deletions Source/JavaScriptCore/bytecode/CallLinkInfoBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <wtf/SentinelLinkedList.h>

namespace JSC {

class VM;

class CallLinkInfoBase : public PackedRawSentinelNode<CallLinkInfoBase> {
public:
enum class CallSiteType : uint8_t {
CallLinkInfo,
#if ENABLE(JIT)
PolymorphicCallNode,
#endif
CachedCall,
};

explicit CallLinkInfoBase(CallSiteType callSiteType)
: m_callSiteType(callSiteType)
{
}

~CallLinkInfoBase()
{
if (isOnList())
remove();
}

CallSiteType callSiteType() const { return m_callSiteType; }

void unlink(VM&);

private:
CallSiteType m_callSiteType;
};

} // namespace JSC
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/bytecode/CallLinkStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ CallLinkStatus CallLinkStatus::computeFromCallLinkInfo(

// Note that despite requiring that the locker is held, this code is racy with respect
// to the CallLinkInfo: it may get cleared while this code runs! This is because
// CallLinkInfo::unlink() may be called from a different CodeBlock than the one that owns
// CallLinkInfoBase::unlink() may be called from a different CodeBlock than the one that owns
// the CallLinkInfo and currently we save space by not having CallLinkInfos know who owns
// them. So, there is no way for either the caller of CallLinkInfo::unlock() or unlock()
// itself to figure out which lock to lock.
Expand Down
17 changes: 3 additions & 14 deletions Source/JavaScriptCore/bytecode/CodeBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,28 +2096,17 @@ void CodeBlock::shrinkToFit(const ConcurrentJSLocker&, ShrinkMode shrinkMode)
#endif
}

#if ENABLE(JIT)
void CodeBlock::linkIncomingPolymorphicCall(CallFrame* callerFrame, PolymorphicCallNode* incoming)
void CodeBlock::linkIncomingCall(CallFrame* callerFrame, CallLinkInfoBase* incoming)
{
noticeIncomingCall(callerFrame);
m_incomingPolymorphicCalls.push(incoming);
}
#endif // ENABLE(JIT)

void CodeBlock::linkIncomingCall(CallFrame* callerFrame, CallLinkInfo* incoming)
{
noticeIncomingCall(callerFrame);
if (callerFrame)
noticeIncomingCall(callerFrame);
m_incomingCalls.push(incoming);
}

void CodeBlock::unlinkIncomingCalls()
{
while (!m_incomingCalls.isEmpty())
m_incomingCalls.begin()->unlink(vm());
#if ENABLE(JIT)
while (!m_incomingPolymorphicCalls.isEmpty())
m_incomingPolymorphicCalls.begin()->unlink(vm());
#endif
}

CodeBlock* CodeBlock::newReplacement()
Expand Down
10 changes: 2 additions & 8 deletions Source/JavaScriptCore/bytecode/CodeBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ class CodeBlock : public JSCell {
#endif // ENABLE(JIT)

void unlinkIncomingCalls();
void linkIncomingCall(CallFrame* callerFrame, CallLinkInfo*);
#if ENABLE(JIT)
void linkIncomingPolymorphicCall(CallFrame* callerFrame, PolymorphicCallNode*);
#endif // ENABLE(JIT)
void linkIncomingCall(CallFrame* callerFrame, CallLinkInfoBase*);

const JSInstruction* outOfLineJumpTarget(const JSInstruction* pc);
int outOfLineJumpOffset(JSInstructionStream::Offset offset)
Expand Down Expand Up @@ -955,10 +952,7 @@ class CodeBlock : public JSCell {
VM* const m_vm;

const void* const m_instructionsRawPointer { nullptr };
SentinelLinkedList<CallLinkInfo, PackedRawSentinelNode<CallLinkInfo>> m_incomingCalls;
#if ENABLE(JIT)
SentinelLinkedList<PolymorphicCallNode, PackedRawSentinelNode<PolymorphicCallNode>> m_incomingPolymorphicCalls;
#endif
SentinelLinkedList<CallLinkInfoBase, PackedRawSentinelNode<CallLinkInfoBase>> m_incomingCalls;
StructureWatchpointMap m_llintGetByIdWatchpointMap;
RefPtr<JITCode> m_jitCode;
#if ENABLE(JIT)
Expand Down
Loading

0 comments on commit 52bceb7

Please sign in to comment.