| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef vm_ConcurrentDelazification_h |
| 8 |
#define vm_ConcurrentDelazification_h |
| 9 |
|
| 10 |
#include "mozilla/MemoryReporting.h" // mozilla::MallocSizeOf |
| 11 |
|
| 12 |
#include <stddef.h> // size_t |
| 13 |
#include <utility> // std::pair |
| 14 |
|
| 15 |
#include "frontend/CompilationStencil.h" // frontend::{InitialStencilAndDelazifications, CompilationStencil, ScriptStencilRef, CompilationStencilMerger} |
| 16 |
#include "frontend/ScriptIndex.h" // frontend::ScriptIndex |
| 17 |
#include "js/AllocPolicy.h" // SystemAllocPolicy |
| 18 |
#include "js/CompileOptions.h" // JS::PrefableCompileOptions, JS::ReadOnlyCompileOptions |
| 19 |
#include "js/experimental/JSStencil.h" // RefPtrTraits for InitialStencilAndDelazifications |
| 20 |
#include "js/UniquePtr.h" // UniquePtr |
| 21 |
#include "js/Vector.h" // Vector |
| 22 |
|
| 23 |
namespace js { |
| 24 |
|
| 25 |
class FrontendContext; |
| 26 |
|
| 27 |
// Base class for implementing the various strategies to iterate over the |
| 28 |
// functions to be delazified, or to decide when to stop doing any |
| 29 |
// delazification. |
| 30 |
// |
| 31 |
// When created, the `add` function should be called with the top-level |
| 32 |
// ScriptIndex. |
| 33 |
struct DelazifyStrategy { |
| 34 |
using ScriptIndex = frontend::ScriptIndex; |
| 35 |
virtual ~DelazifyStrategy() = default; |
| 36 |
|
| 37 |
// Returns true if no more functions should be delazified. Note, this does not |
| 38 |
// imply that every function got delazified. |
| 39 |
virtual bool done() const = 0; |
| 40 |
|
| 41 |
// Return a function identifier which represent the next function to be |
| 42 |
// delazified. If no more function should be delazified, then return 0. |
| 43 |
virtual ScriptIndex next() = 0; |
| 44 |
|
| 45 |
// Empty the list of functions to be processed next. done() should return true |
| 46 |
// after this call. |
| 47 |
virtual void clear() = 0; |
| 48 |
|
| 49 |
// Insert an index in the container of the delazification strategy. A strategy |
| 50 |
// can choose to ignore the insertion of an index in its queue of function to |
| 51 |
// delazify. Return false only in case of errors while inserting, and true |
| 52 |
// otherwise. |
| 53 |
[[nodiscard]] virtual bool insert(ScriptIndex index, |
| 54 |
frontend::ScriptStencilRef& ref) = 0; |
| 55 |
|
| 56 |
// Add the inner functions of a delazified function. This function should only |
| 57 |
// be called with a function which has some bytecode associated with it, and |
| 58 |
// register functions which parent are already delazified. |
| 59 |
// |
| 60 |
// This function is called with the script index of: |
| 61 |
// - top-level script, when starting the off-thread delazification. |
| 62 |
// - functions added by `add` and delazified by `DelazificationContext`. |
| 63 |
[[nodiscard]] bool add(FrontendContext* fc, |
| 64 |
const frontend::CompilationStencil& stencil, |
| 65 |
ScriptIndex index); |
| 66 |
}; |
| 67 |
|
| 68 |
// Delazify all functions using a Depth First traversal of the function-tree |
| 69 |
// ordered, where each functions is visited in source-order. |
| 70 |
// |
| 71 |
// When `add` is called with the top-level ScriptIndex. This will push all inner |
| 72 |
// functions to a stack such that they are popped in source order. Each |
| 73 |
// function, once delazified, would be used to schedule their inner functions |
| 74 |
// the same way. |
| 75 |
// |
| 76 |
// Hypothesis: This strategy parses all functions in source order, with the |
| 77 |
// expectation that calls will follow the same order, and that helper thread |
| 78 |
// would always be ahead of the execution. |
| 79 |
struct DepthFirstDelazification final : public DelazifyStrategy { |
| 80 |
Vector<ScriptIndex, 0, SystemAllocPolicy> stack; |
| 81 |
|
| 82 |
bool done() const override { return stack.empty(); } |
| 83 |
ScriptIndex next() override { return stack.popCopy(); } |
| 84 |
void clear() override { return stack.clear(); } |
| 85 |
bool insert(ScriptIndex index, frontend::ScriptStencilRef&) override { |
| 86 |
return stack.append(index); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
// Delazify all functions using a traversal which select the largest function |
| 91 |
// first. The intent being that if the main thread races with the helper thread, |
| 92 |
// then the main thread should only have to parse small functions instead of the |
| 93 |
// large ones which would be prioritized by this delazification strategy. |
| 94 |
struct LargeFirstDelazification final : public DelazifyStrategy { |
| 95 |
using SourceSize = uint32_t; |
| 96 |
Vector<std::pair<SourceSize, ScriptIndex>, 0, SystemAllocPolicy> heap; |
| 97 |
|
| 98 |
bool done() const override { return heap.empty(); } |
| 99 |
ScriptIndex next() override; |
| 100 |
void clear() override { return heap.clear(); } |
| 101 |
bool insert(ScriptIndex, frontend::ScriptStencilRef&) override; |
| 102 |
}; |
| 103 |
|
| 104 |
class DelazificationContext { |
| 105 |
const JS::PrefableCompileOptions initialPrefableOptions_; |
| 106 |
|
| 107 |
// Queue of functions to be processed while delazifying. |
| 108 |
UniquePtr<DelazifyStrategy> strategy_; |
| 109 |
|
| 110 |
// Every delazified function is merged back to provide context for delazifying |
| 111 |
// even more functions. |
| 112 |
frontend::CompilationStencilMerger merger_; |
| 113 |
|
| 114 |
RefPtr<frontend::InitialStencilAndDelazifications> stencils_; |
| 115 |
|
| 116 |
// Record any errors happening while parsing or generating bytecode. |
| 117 |
FrontendContext fc_; |
| 118 |
|
| 119 |
size_t stackQuota_; |
| 120 |
|
| 121 |
bool isInterrupted_ = false; |
| 122 |
|
| 123 |
public: |
| 124 |
explicit DelazificationContext( |
| 125 |
const JS::PrefableCompileOptions& initialPrefableOptions, |
| 126 |
size_t stackQuota) |
| 127 |
: initialPrefableOptions_(initialPrefableOptions), |
| 128 |
stackQuota_(stackQuota) {} |
| 129 |
|
| 130 |
bool init(const JS::ReadOnlyCompileOptions& options, |
| 131 |
frontend::InitialStencilAndDelazifications* stencils); |
| 132 |
bool delazify(); |
| 133 |
|
| 134 |
// This function is called by `delazify` function to know whether the |
| 135 |
// delazification should be interrupted. |
| 136 |
// |
| 137 |
// The `delazify` function holds on a thread until all functions iterated |
| 138 |
// over by the strategy. However, as a `delazify` function iterates over |
| 139 |
// multiple functions, it can easily be interrupted at function boundaries. |
| 140 |
// |
| 141 |
// TODO: (Bug 1773683) Plug this with the mozilla::Task::RequestInterrupt |
| 142 |
// function which is wrapping HelperThreads tasks within Mozilla. |
| 143 |
bool isInterrupted() const { return isInterrupted_; } |
| 144 |
void interrupt() { isInterrupted_ = true; } |
| 145 |
|
| 146 |
bool done() const; |
| 147 |
|
| 148 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 149 |
}; |
| 150 |
|
| 151 |
} /* namespace js */ |
| 152 |
|
| 153 |
#endif /* vm_ConcurrentDelazification_h */ |