Commit dce80ce2 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[managed] Clean up {std::shared_ptr} use

1) Pass {std::shared_ptr} by reference if a copy might not be needed.
   This applies both to accessors as well as constructing methods.
   This change often saves one atomic increment and decrement of the
   internal reference counter.
2) Use {std::make_shared} directly to improve memory management of the
   {std::shared_ptr}. This saves one dynamic memory allocation on all
   known implementations.

R=titzer@chromium.org

Bug: v8:8834
Change-Id: I1a951deb135082745885bc981662a8d6d6fb1a71
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1532333Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60425}
parent 8c42bd9e
......@@ -55,8 +55,8 @@ class Managed : public Foreign {
// Get a raw pointer to the C++ object.
V8_INLINE CppType* raw() { return GetSharedPtrPtr()->get(); }
// Get a copy of the shared pointer to the C++ object.
V8_INLINE std::shared_ptr<CppType> get() { return *GetSharedPtrPtr(); }
// Get a reference to the shared pointer to the C++ object.
V8_INLINE const std::shared_ptr<CppType>& get() { return *GetSharedPtrPtr(); }
static Managed cast(Object obj) { return Managed(obj->ptr()); }
static Managed unchecked_cast(Object obj) { return bit_cast<Managed>(obj); }
......@@ -66,9 +66,9 @@ class Managed : public Foreign {
static Handle<Managed<CppType>> Allocate(Isolate* isolate,
size_t estimated_size,
Args&&... args) {
CppType* ptr = new CppType(std::forward<Args>(args)...);
return FromSharedPtr(isolate, estimated_size,
std::shared_ptr<CppType>(ptr));
return FromSharedPtr(
isolate, estimated_size,
std::make_shared<CppType>(std::forward<Args>(args)...));
}
// Create a {Managed<CppType>} from an existing raw {CppType*}. The returned
......@@ -77,7 +77,7 @@ class Managed : public Foreign {
size_t estimated_size,
CppType* ptr) {
return FromSharedPtr(isolate, estimated_size,
std::shared_ptr<CppType>(ptr));
std::shared_ptr<CppType>{ptr});
}
// Create a {Managed<CppType>} from an existing {std::unique_ptr<CppType>}.
......@@ -92,11 +92,11 @@ class Managed : public Foreign {
// Create a {Managed<CppType>} from an existing {std::shared_ptr<CppType>}.
static Handle<Managed<CppType>> FromSharedPtr(
Isolate* isolate, size_t estimated_size,
std::shared_ptr<CppType> shared_ptr) {
const std::shared_ptr<CppType>& shared_ptr) {
reinterpret_cast<v8::Isolate*>(isolate)
->AdjustAmountOfExternalAllocatedMemory(estimated_size);
auto destructor = new ManagedPtrDestructor(
estimated_size, new std::shared_ptr<CppType>(shared_ptr), Destructor);
estimated_size, new std::shared_ptr<CppType>{shared_ptr}, Destructor);
Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
isolate->factory()->NewForeign(reinterpret_cast<Address>(destructor)));
Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
......@@ -118,8 +118,8 @@ class Managed : public Foreign {
destructor->shared_ptr_ptr_);
}
// Called by either isolate shutdown or the {ManagedObjectFinalizer} in
// order to actually delete the shared pointer (i.e. decrement its refcount).
// Called by either isolate shutdown or the {ManagedObjectFinalizer} in order
// to actually delete the shared pointer and decrement the shared refcount.
static void Destructor(void* ptr) {
auto shared_ptr_ptr = reinterpret_cast<std::shared_ptr<CppType>*>(ptr);
delete shared_ptr_ptr;
......
......@@ -97,8 +97,8 @@ OPTIONAL_ACCESSORS(WasmModuleObject, breakpoint_infos, FixedArray,
wasm::NativeModule* WasmModuleObject::native_module() const {
return managed_native_module()->raw();
}
std::shared_ptr<wasm::NativeModule> WasmModuleObject::shared_native_module()
const {
const std::shared_ptr<wasm::NativeModule>&
WasmModuleObject::shared_native_module() const {
return managed_native_module()->get();
}
const wasm::WasmModule* WasmModuleObject::module() const {
......
......@@ -118,7 +118,8 @@ class WasmModuleObject : public JSObject {
DECL_OPTIONAL_ACCESSORS(asm_js_offset_table, ByteArray)
DECL_OPTIONAL_ACCESSORS(breakpoint_infos, FixedArray)
inline wasm::NativeModule* native_module() const;
inline std::shared_ptr<wasm::NativeModule> shared_native_module() const;
inline const std::shared_ptr<wasm::NativeModule>& shared_native_module()
const;
inline const wasm::WasmModule* module() const;
inline void reset_breakpoint_infos();
......
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