Commit acc71fb9 authored by Clemens Backes's avatar Clemens Backes Committed by V8 LUCI CQ

[wasm] Reduce number of write scopes for publishing

Publishing Wasm code often happens in a loop (in particular for
deserialization), so hold the {CodeSpaceWriteScope} outside that loop to
avoid repeated switching between writable and executable.

R=ahaas@chromium.org

Bug: v8:11974, chromium:1297999
Change-Id: Ic4bf859685e66c4ba297fed968d0df6ae7d24cba
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3468896Reviewed-by: 's avatarThibaud Michaud <thibaudm@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79135}
parent 2f50fc6a
......@@ -53,6 +53,8 @@ class V8_NODISCARD CodeSpaceWriteScope final {
CodeSpaceWriteScope(const CodeSpaceWriteScope&) = delete;
CodeSpaceWriteScope& operator=(const CodeSpaceWriteScope&) = delete;
static bool IsInScope() { return code_space_write_nesting_level_ > 0; }
private:
static thread_local int code_space_write_nesting_level_;
#if defined(DEBUG) && !V8_HAS_PTHREAD_JIT_WRITE_PROTECT
......
......@@ -3690,6 +3690,7 @@ void CompilationStateImpl::SchedulePublishCompilationResults(
}
publisher_running_ = true;
}
CodeSpaceWriteScope code_space_write_scope(native_module_);
while (true) {
PublishCompilationResults(std::move(unpublished_code));
unpublished_code.clear();
......
......@@ -1264,14 +1264,14 @@ WasmCode* NativeModule::PublishCode(std::unique_ptr<WasmCode> code) {
std::vector<WasmCode*> NativeModule::PublishCode(
base::Vector<std::unique_ptr<WasmCode>> codes) {
// Publishing often happens in a loop, so the caller should hold the
// {CodeSpaceWriteScope} outside of such a loop.
DCHECK(CodeSpaceWriteScope::IsInScope());
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("v8.wasm.detailed"),
"wasm.PublishCode", "number", codes.size());
std::vector<WasmCode*> published_code;
published_code.reserve(codes.size());
base::RecursiveMutexGuard lock(&allocation_mutex_);
// Get writable permission already here (and not inside the loop in
// {PatchJumpTablesLocked}), to avoid switching for each {code} individually.
CodeSpaceWriteScope code_space_write_scope(this);
// The published code is put into the top-most surrounding {WasmCodeRefScope}.
for (auto& code : codes) {
published_code.push_back(PublishCodeLocked(std::move(code)));
......
......@@ -623,6 +623,7 @@ class PublishTask : public JobTask {
: deserializer_(deserializer), from_queue_(from_queue) {}
void Run(JobDelegate* delegate) override {
CodeSpaceWriteScope code_space_write_scope(deserializer_->native_module_);
WasmCodeRefScope code_scope;
do {
auto to_publish = from_queue_->PopAll();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment