Commit a8007145 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Add a fast path to {FindJumpTablesForRegion}

This method is called in the critical section in {PublishCode}, hence
performance is important here. Since most modules will only have a
single code space anyway, we can use the main jump table in the vast
majority of cases, and avoid taking a lock and iterating another data
structure.

R=ahaas@chromium.org

Bug: v8:10330
Change-Id: I18cbd3b127172963ccc9ec576a0985e874da7865
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2104891
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66721}
parent f26c7b75
......@@ -1275,7 +1275,13 @@ void NativeModule::AddCodeSpace(
WasmCode::kRuntimeStubCount, num_function_slots);
}
if (is_first_code_space) main_jump_table_ = jump_table;
if (is_first_code_space) {
// This can be updated and accessed without locks, since the addition of the
// first code space happens during initialization of the {NativeModule},
// where no concurrent accesses are possible.
main_jump_table_ = jump_table;
main_far_jump_table_ = far_jump_table;
}
base::MutexGuard guard(&allocation_mutex_);
code_space_data_.push_back(CodeSpaceData{region, jump_table, far_jump_table});
......@@ -1364,6 +1370,18 @@ NativeModule::JumpTablesRef NativeModule::FindJumpTablesForRegion(
table_end > code_region.begin() ? table_end - code_region.begin() : 0);
return max_distance < kMaxWasmCodeSpaceSize;
};
// Fast path: Try to use {main_jump_table_} and {main_far_jump_table_}.
// Access to these fields is possible without locking, since these fields are
// initialized on construction of the {NativeModule}.
if (main_far_jump_table_ && jump_table_usable(main_far_jump_table_) &&
(main_jump_table_ == nullptr || jump_table_usable(main_jump_table_))) {
return {
main_jump_table_ ? main_jump_table_->instruction_start() : kNullAddress,
main_far_jump_table_->instruction_start()};
}
// Otherwise, take the mutex and look for another suitable jump table.
base::MutexGuard guard(&allocation_mutex_);
for (auto& code_space_data : code_space_data_) {
DCHECK_IMPLIES(code_space_data.jump_table, code_space_data.far_jump_table);
......
......@@ -679,10 +679,14 @@ class V8_EXPORT_PRIVATE NativeModule final {
// {WireBytesStorage}, held by background compile tasks.
std::shared_ptr<OwnedVector<const uint8_t>> wire_bytes_;
// Jump table used by external calls (from JS). Wasm calls use one of the jump
// tables stored in {code_space_data_}.
// The first allocated jump table. Always used by external calls (from JS).
// Wasm calls might use one of the other jump tables stored in
// {code_space_data_}.
WasmCode* main_jump_table_ = nullptr;
// The first allocated far jump table.
WasmCode* main_far_jump_table_ = nullptr;
// Lazy compile stub table, containing entries to jump to the
// {WasmCompileLazy} builtin, passing the function index.
WasmCode* lazy_compile_table_ = nullptr;
......
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