From 11275172bd839e4634243cd8220793098f4c00cd Mon Sep 17 00:00:00 2001 From: Clemens Backes Date: Mon, 28 Nov 2022 19:35:42 +0100 Subject: [PATCH] [wasm][cleanup] Use OwnedVector instead of unique_ptr + size Passing a {unique_ptr} plus the size of the array separately makes it easier to pass non-matching values. Using {base::OwnedVector} instead makes all call sites cleaner. R=ahaas@chromium.org Change-Id: I66cf8e756d098837aac71e410b18e08646e512b8 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4061893 Commit-Queue: Clemens Backes Reviewed-by: Andreas Haas Cr-Commit-Position: refs/heads/main@{#84549} --- src/wasm/module-compiler.cc | 13 ++++++------- src/wasm/module-compiler.h | 4 ++-- src/wasm/wasm-engine.cc | 21 ++++++++++----------- src/wasm/wasm-engine.h | 4 ++-- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/wasm/module-compiler.cc b/src/wasm/module-compiler.cc index 13349d7fc2..00e16f31d5 100644 --- a/src/wasm/module-compiler.cc +++ b/src/wasm/module-compiler.cc @@ -2059,7 +2059,7 @@ void RecompileNativeModule(NativeModule* native_module, AsyncCompileJob::AsyncCompileJob( Isolate* isolate, WasmFeatures enabled_features, - std::unique_ptr bytes_copy, size_t length, Handle context, + base::OwnedVector bytes, Handle context, Handle incumbent_context, const char* api_method_name, std::shared_ptr resolver, int compilation_id) : isolate_(isolate), @@ -2067,8 +2067,8 @@ AsyncCompileJob::AsyncCompileJob( enabled_features_(enabled_features), dynamic_tiering_(DynamicTiering{v8_flags.wasm_dynamic_tiering.value()}), start_time_(base::TimeTicks::Now()), - bytes_copy_(std::move(bytes_copy)), - wire_bytes_(bytes_copy_.get(), bytes_copy_.get() + length), + bytes_copy_(std::move(bytes)), + wire_bytes_(bytes_copy_.as_vector()), resolver_(std::move(resolver)), compilation_id_(compilation_id) { TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm.detailed"), @@ -2199,7 +2199,7 @@ void AsyncCompileJob::CreateNativeModule( native_module_ = GetWasmEngine()->NewNativeModule( isolate_, enabled_features_, std::move(module), code_size_estimate); - native_module_->SetWireBytes({std::move(bytes_copy_), wire_bytes_.length()}); + native_module_->SetWireBytes(std::move(bytes_copy_)); native_module_->compilation_state()->set_compilation_id(compilation_id_); } @@ -2961,7 +2961,7 @@ void AsyncStreamingProcessor::OnFinishedStream( } job_->wire_bytes_ = ModuleWireBytes(bytes.as_vector()); - job_->bytes_copy_ = bytes.ReleaseData(); + job_->bytes_copy_ = std::move(bytes); // Record event metrics. auto duration = base::TimeTicks::Now() - job_->start_time_; @@ -3006,8 +3006,7 @@ void AsyncStreamingProcessor::OnFinishedStream( cache_hit = job_->GetOrCreateNativeModule(std::move(result).value(), kCodeSizeEstimate); } else { - job_->native_module_->SetWireBytes( - {std::move(job_->bytes_copy_), job_->wire_bytes_.length()}); + job_->native_module_->SetWireBytes(std::move(job_->bytes_copy_)); } const bool needs_finish = job_->DecrementAndCheckFinisherCount(AsyncCompileJob::kStreamingDecoder); diff --git a/src/wasm/module-compiler.h b/src/wasm/module-compiler.h index 6f07ce4741..fd161fa61a 100644 --- a/src/wasm/module-compiler.h +++ b/src/wasm/module-compiler.h @@ -139,7 +139,7 @@ class WrapperQueue { class AsyncCompileJob { public: AsyncCompileJob(Isolate* isolate, WasmFeatures enabled_features, - std::unique_ptr bytes_copy, size_t length, + base::OwnedVector bytes, Handle context, Handle incumbent_context, const char* api_method_name, std::shared_ptr resolver, @@ -263,7 +263,7 @@ class AsyncCompileJob { base::TimeTicks start_time_; // Copy of the module wire bytes, moved into the {native_module_} on its // creation. - std::unique_ptr bytes_copy_; + base::OwnedVector bytes_copy_; // Reference to the wire bytes (held in {bytes_copy_} or as part of // {native_module_}). ModuleWireBytes wire_bytes_; diff --git a/src/wasm/wasm-engine.cc b/src/wasm/wasm-engine.cc index c99a79fe83..2aa6f0841b 100644 --- a/src/wasm/wasm-engine.cc +++ b/src/wasm/wasm-engine.cc @@ -680,13 +680,12 @@ void WasmEngine::AsyncCompile( } // Make a copy of the wire bytes in case the user program changes them // during asynchronous compilation. - std::unique_ptr copy(new byte[bytes.length()]); - memcpy(copy.get(), bytes.start(), bytes.length()); + base::OwnedVector copy = + base::OwnedVector::Of(bytes.module_bytes()); AsyncCompileJob* job = CreateAsyncCompileJob( - isolate, enabled, std::move(copy), bytes.length(), - handle(isolate->context(), isolate), api_method_name_for_errors, - std::move(resolver), compilation_id); + isolate, enabled, std::move(copy), handle(isolate->context(), isolate), + api_method_name_for_errors, std::move(resolver), compilation_id); job->Start(); } @@ -698,9 +697,9 @@ std::shared_ptr WasmEngine::StartStreamingCompilation( TRACE_EVENT1("v8.wasm", "wasm.StartStreamingCompilation", "id", compilation_id); if (v8_flags.wasm_async_compilation) { - AsyncCompileJob* job = CreateAsyncCompileJob( - isolate, enabled, std::unique_ptr(nullptr), 0, context, - api_method_name, std::move(resolver), compilation_id); + AsyncCompileJob* job = + CreateAsyncCompileJob(isolate, enabled, {}, context, api_method_name, + std::move(resolver), compilation_id); return job->CreateStreamingDecoder(); } return StreamingDecoder::CreateSyncStreamingDecoder( @@ -921,13 +920,13 @@ CodeTracer* WasmEngine::GetCodeTracer() { AsyncCompileJob* WasmEngine::CreateAsyncCompileJob( Isolate* isolate, const WasmFeatures& enabled, - std::unique_ptr bytes_copy, size_t length, Handle context, + base::OwnedVector bytes, Handle context, const char* api_method_name, std::shared_ptr resolver, int compilation_id) { Handle incumbent_context = isolate->GetIncumbentContext(); AsyncCompileJob* job = new AsyncCompileJob( - isolate, enabled, std::move(bytes_copy), length, context, - incumbent_context, api_method_name, std::move(resolver), compilation_id); + isolate, enabled, std::move(bytes), context, incumbent_context, + api_method_name, std::move(resolver), compilation_id); // Pass ownership to the unique_ptr in {async_compile_jobs_}. base::MutexGuard guard(&mutex_); async_compile_jobs_[job] = std::unique_ptr(job); diff --git a/src/wasm/wasm-engine.h b/src/wasm/wasm-engine.h index 9f34f001f5..0c6f296f8b 100644 --- a/src/wasm/wasm-engine.h +++ b/src/wasm/wasm-engine.h @@ -379,8 +379,8 @@ class V8_EXPORT_PRIVATE WasmEngine { AsyncCompileJob* CreateAsyncCompileJob( Isolate* isolate, const WasmFeatures& enabled, - std::unique_ptr bytes_copy, size_t length, - Handle context, const char* api_method_name, + base::OwnedVector bytes, Handle context, + const char* api_method_name, std::shared_ptr resolver, int compilation_id); void TriggerGC(int8_t gc_sequence_index);