#pragma once
#include "CachedTypes.h"
#include "ExecutableInfo.h"
#include "JSCInlines.h"
#include "Parser.h"
#include "ParserModes.h"
#include "SourceCodeKey.h"
#include "Strong.h"
#include "StrongInlines.h"
#include "UnlinkedCodeBlock.h"
#include "UnlinkedEvalCodeBlock.h"
#include "UnlinkedFunctionCodeBlock.h"
#include "UnlinkedModuleProgramCodeBlock.h"
#include "UnlinkedProgramCodeBlock.h"
namespace JSC {
class EvalExecutable;
class IndirectEvalExecutable;
class Identifier;
class DirectEvalExecutable;
class ModuleProgramExecutable;
class ParserError;
class ProgramExecutable;
class SourceCode;
class VM;
class VariableEnvironment;
namespace CodeCacheInternal {
static constexpr bool verbose = false;
}
struct SourceCodeValue {
SourceCodeValue()
{
}
SourceCodeValue(VM& vm, JSCell* cell, int64_t age)
: cell(vm, cell)
, age(age)
{
}
Strong<JSCell> cell;
int64_t age;
};
class CodeCacheMap {
public:
typedef HashMap<SourceCodeKey, SourceCodeValue, SourceCodeKey::Hash, SourceCodeKey::HashTraits> MapType;
typedef MapType::iterator iterator;
typedef MapType::AddResult AddResult;
CodeCacheMap()
: m_size(0)
, m_sizeAtLastPrune(0)
, m_timeAtLastPrune(MonotonicTime::now())
, m_minCapacity(0)
, m_capacity(0)
, m_age(0)
{
}
iterator begin() { return m_map.begin(); }
iterator end() { return m_map.end(); }
template<typename UnlinkedCodeBlockType>
UnlinkedCodeBlockType* findCacheAndUpdateAge(VM& vm, const SourceCodeKey& key)
{
prune();
iterator findResult = m_map.find(key);
if (findResult == m_map.end())
return fetchFromDisk<UnlinkedCodeBlockType>(vm, key);
int64_t age = m_age - findResult->value.age;
if (age > m_capacity) {
m_capacity += recencyBias * oldObjectSamplingMultiplier * key.length();
} else if (age < m_capacity / 2) {
m_capacity -= recencyBias * key.length();
if (m_capacity < m_minCapacity)
m_capacity = m_minCapacity;
}
findResult->value.age = m_age;
m_age += key.length();
return jsCast<UnlinkedCodeBlockType*>(findResult->value.cell.get());
}
AddResult addCache(const SourceCodeKey& key, const SourceCodeValue& value)
{
prune();
AddResult addResult = m_map.add(key, value);
ASSERT(addResult.isNewEntry);
m_size += key.length();
m_age += key.length();
return addResult;
}
void remove(iterator it)
{
m_size -= it->key.length();
m_map.remove(it);
}
void clear()
{
m_size = 0;
m_age = 0;
m_map.clear();
}
int64_t age() { return m_age; }
private:
template<typename UnlinkedCodeBlockType>
UnlinkedCodeBlockType* fetchFromDiskImpl(VM& vm, const SourceCodeKey& key)
{
RefPtr<CachedBytecode> cachedBytecode = key.source().provider().cachedBytecode();
if (!cachedBytecode || !cachedBytecode->size())
return nullptr;
return decodeCodeBlock<UnlinkedCodeBlockType>(vm, key, *cachedBytecode);
}
template<typename UnlinkedCodeBlockType>
std::enable_if_t<std::is_base_of<UnlinkedCodeBlock, UnlinkedCodeBlockType>::value && !std::is_same<UnlinkedCodeBlockType, UnlinkedEvalCodeBlock>::value, UnlinkedCodeBlockType*>
fetchFromDisk(VM& vm, const SourceCodeKey& key)
{
UnlinkedCodeBlockType* codeBlock = fetchFromDiskImpl<UnlinkedCodeBlockType>(vm, key);
if (UNLIKELY(Options::forceDiskCache()))
RELEASE_ASSERT(codeBlock);
return codeBlock;
}
template<typename T>
std::enable_if_t<!std::is_base_of<UnlinkedCodeBlock, T>::value || std::is_same<T, UnlinkedEvalCodeBlock>::value, T*>
fetchFromDisk(VM&, const SourceCodeKey&) { return nullptr; }
static constexpr Seconds workingSetTime = 10_s;
static constexpr int64_t workingSetMaxBytes = 16000000;
static constexpr size_t workingSetMaxEntries = 2000;
static constexpr int64_t recencyBias = 4;
static constexpr int64_t oldObjectSamplingMultiplier = 32;
size_t numberOfEntries() const { return static_cast<size_t>(m_map.size()); }
bool canPruneQuickly() const { return numberOfEntries() < workingSetMaxEntries; }
void pruneSlowCase();
void prune()
{
if (m_size <= m_capacity && canPruneQuickly())
return;
if (MonotonicTime::now() - m_timeAtLastPrune < workingSetTime
&& m_size - m_sizeAtLastPrune < workingSetMaxBytes
&& canPruneQuickly())
return;
pruneSlowCase();
}
MapType m_map;
int64_t m_size;
int64_t m_sizeAtLastPrune;
MonotonicTime m_timeAtLastPrune;
int64_t m_minCapacity;
int64_t m_capacity;
int64_t m_age;
};
class CodeCache {
WTF_MAKE_FAST_ALLOCATED;
public:
UnlinkedProgramCodeBlock* getUnlinkedProgramCodeBlock(VM&, ProgramExecutable*, const SourceCode&, JSParserStrictMode, OptionSet<CodeGenerationMode>, ParserError&);
UnlinkedEvalCodeBlock* getUnlinkedEvalCodeBlock(VM&, IndirectEvalExecutable*, const SourceCode&, JSParserStrictMode, OptionSet<CodeGenerationMode>, ParserError&, EvalContextType);
UnlinkedModuleProgramCodeBlock* getUnlinkedModuleProgramCodeBlock(VM&, ModuleProgramExecutable*, const SourceCode&, OptionSet<CodeGenerationMode>, ParserError&);
UnlinkedFunctionExecutable* getUnlinkedGlobalFunctionExecutable(VM&, const Identifier&, const SourceCode&, OptionSet<CodeGenerationMode>, Optional<int> functionConstructorParametersEndPosition, ParserError&);
void updateCache(const UnlinkedFunctionExecutable*, const SourceCode&, CodeSpecializationKind, const UnlinkedFunctionCodeBlock*);
void clear() { m_sourceCode.clear(); }
JS_EXPORT_PRIVATE void write(VM&);
private:
template <class UnlinkedCodeBlockType, class ExecutableType>
UnlinkedCodeBlockType* getUnlinkedGlobalCodeBlock(VM&, ExecutableType*, const SourceCode&, JSParserStrictMode, JSParserScriptMode, OptionSet<CodeGenerationMode>, ParserError&, EvalContextType);
CodeCacheMap m_sourceCode;
};
template <typename T> struct CacheTypes { };
template <> struct CacheTypes<UnlinkedProgramCodeBlock> {
typedef JSC::ProgramNode RootNode;
static constexpr SourceCodeType codeType = SourceCodeType::ProgramType;
static constexpr SourceParseMode parseMode = SourceParseMode::ProgramMode;
};
template <> struct CacheTypes<UnlinkedEvalCodeBlock> {
typedef JSC::EvalNode RootNode;
static constexpr SourceCodeType codeType = SourceCodeType::EvalType;
static constexpr SourceParseMode parseMode = SourceParseMode::ProgramMode;
};
template <> struct CacheTypes<UnlinkedModuleProgramCodeBlock> {
typedef JSC::ModuleProgramNode RootNode;
static constexpr SourceCodeType codeType = SourceCodeType::ModuleType;
static constexpr SourceParseMode parseMode = SourceParseMode::ModuleEvaluateMode;
};
UnlinkedEvalCodeBlock* generateUnlinkedCodeBlockForDirectEval(VM&, DirectEvalExecutable*, const SourceCode&, JSParserStrictMode, JSParserScriptMode, OptionSet<CodeGenerationMode>, ParserError&, EvalContextType, const VariableEnvironment* variablesUnderTDZ);
UnlinkedProgramCodeBlock* recursivelyGenerateUnlinkedCodeBlockForProgram(VM&, const SourceCode&, JSParserStrictMode, JSParserScriptMode, OptionSet<CodeGenerationMode>, ParserError&, EvalContextType, const VariableEnvironment* variablesUnderTDZ);
UnlinkedModuleProgramCodeBlock* recursivelyGenerateUnlinkedCodeBlockForModuleProgram(VM&, const SourceCode&, JSParserStrictMode, JSParserScriptMode, OptionSet<CodeGenerationMode>, ParserError&, EvalContextType, const VariableEnvironment* variablesUnderTDZ);
void writeCodeBlock(VM&, const SourceCodeKey&, const SourceCodeValue&);
RefPtr<CachedBytecode> serializeBytecode(VM&, UnlinkedCodeBlock*, const SourceCode&, SourceCodeType, JSParserStrictMode, JSParserScriptMode, FileSystem::PlatformFileHandle fd, BytecodeCacheError&, OptionSet<CodeGenerationMode>);
SourceCodeKey sourceCodeKeyForSerializedProgram(VM&, const SourceCode&);
SourceCodeKey sourceCodeKeyForSerializedModule(VM&, const SourceCode&);
}