GeezerVison is showing github.com — src: remove custom tracking for SharedArrayBuffers · cheza24/node@2bdeb88
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: node.gyp -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 564 | 564 | 'src/node_zlib.cc', | 565 | 565 | 'src/pipe_wrap.cc', | 566 | 566 | 'src/process_wrap.cc', | 567 | | - 'src/sharedarraybuffer_metadata.cc', | 568 | 567 | 'src/signal_wrap.cc', | 569 | 568 | 'src/spawn_sync.cc', | 570 | 569 | 'src/stream_base.cc', | | 642 | 641 | 'src/pipe_wrap.h', | 643 | 642 | 'src/req_wrap.h', | 644 | 643 | 'src/req_wrap-inl.h', | 645 | | - 'src/sharedarraybuffer_metadata.h', | 646 | 644 | 'src/spawn_sync.h', | 647 | 645 | 'src/stream_base.h', | 648 | 646 | 'src/stream_base-inl.h', | |
Collapse file Copy file name to clipboardExpand all lines: src/api/environment.cc +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -279,6 +279,14 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator, | 279 | 279 | return NewIsolate(¶ms, event_loop, platform); | 280 | 280 | } | 281 | 281 |
| | 282 | +Isolate* NewIsolate(std::shared_ptr<ArrayBufferAllocator> allocator, | | 283 | + uv_loop_t* event_loop, | | 284 | + MultiIsolatePlatform* platform) { | | 285 | + Isolate::CreateParams params; | | 286 | + if (allocator) params.array_buffer_allocator_shared = allocator; | | 287 | + return NewIsolate(¶ms, event_loop, platform); | | 288 | +} | | 289 | + | 282 | 290 | IsolateData* CreateIsolateData(Isolate* isolate, | 283 | 291 | uv_loop_t* loop, | 284 | 292 | MultiIsolatePlatform* platform, | |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc -15Lines changed: 0 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1038,21 +1038,6 @@ char* Environment::Reallocate(char* data, size_t old_size, size_t size) { | 1038 | 1038 | return new_data; | 1039 | 1039 | } | 1040 | 1040 |
| 1041 | | -void Environment::AddArrayBufferAllocatorToKeepAliveUntilIsolateDispose( | 1042 | | - std::shared_ptr<v8::ArrayBuffer::Allocator> allocator) { | 1043 | | - if (keep_alive_allocators_ == nullptr) { | 1044 | | - MultiIsolatePlatform* platform = isolate_data()->platform(); | 1045 | | - CHECK_NOT_NULL(platform); | 1046 | | - | 1047 | | - keep_alive_allocators_ = new ArrayBufferAllocatorList(); | 1048 | | - platform->AddIsolateFinishedCallback(isolate(), [](void* data) { | 1049 | | - delete static_cast<ArrayBufferAllocatorList*>(data); | 1050 | | - }, static_cast<void*>(keep_alive_allocators_)); | 1051 | | - } | 1052 | | - | 1053 | | - keep_alive_allocators_->insert(allocator); | 1054 | | -} | 1055 | | - | 1056 | 1041 | bool Environment::RunWeakRefCleanup() { | 1057 | 1042 | isolate()->ClearKeptObjects(); | 1058 | 1043 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env.h -9Lines changed: 0 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -156,7 +156,6 @@ constexpr size_t kFsStatsBufferLength = | 156 | 156 | V(contextify_global_private_symbol, "node:contextify:global") \ | 157 | 157 | V(decorated_private_symbol, "node:decorated") \ | 158 | 158 | V(napi_wrapper, "node:napi:wrapper") \ | 159 | | - V(sab_lifetimepartner_symbol, "node:sharedArrayBufferLifetimePartner") \ | 160 | 159 |
| 161 | 160 | // Symbols are per-isolate primitives but Environment proxies them | 162 | 161 | // for the sake of convenience. | @@ -1250,10 +1249,6 @@ class Environment : public MemoryRetainer { | 1250 | 1249 |
| 1251 | 1250 | #endif // HAVE_INSPECTOR | 1252 | 1251 |
| 1253 | | - // Only available if a MultiIsolatePlatform is in use. | 1254 | | - void AddArrayBufferAllocatorToKeepAliveUntilIsolateDispose( | 1255 | | - std::shared_ptr<v8::ArrayBuffer::Allocator>); | 1256 | | - | 1257 | 1252 | private: | 1258 | 1253 | template <typename Fn> | 1259 | 1254 | inline void CreateImmediate(Fn&& cb, | @@ -1433,10 +1428,6 @@ class Environment : public MemoryRetainer { | 1433 | 1428 | // Used by embedders to shutdown running Node instance. | 1434 | 1429 | AsyncRequest thread_stopper_; | 1435 | 1430 |
| 1436 | | - typedef std::unordered_set<std::shared_ptr<v8::ArrayBuffer::Allocator>> | 1437 | | - ArrayBufferAllocatorList; | 1438 | | - ArrayBufferAllocatorList* keep_alive_allocators_ = nullptr; | 1439 | | - | 1440 | 1431 | template <typename T> | 1441 | 1432 | void ForEachBaseObject(T&& iterator); | 1442 | 1433 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/memory_tracker-inl.h +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,16 @@ void MemoryTracker::TrackField(const char* edge_name, | 109 | 109 | TrackField(edge_name, value.get(), node_name); | 110 | 110 | } | 111 | 111 |
| | 112 | +template <typename T> | | 113 | +void MemoryTracker::TrackField(const char* edge_name, | | 114 | + const std::shared_ptr<T>& value, | | 115 | + const char* node_name) { | | 116 | + if (value.get() == nullptr) { | | 117 | + return; | | 118 | + } | | 119 | + TrackField(edge_name, value.get(), node_name); | | 120 | +} | | 121 | + | 112 | 122 | template <typename T, typename Iterator> | 113 | 123 | void MemoryTracker::TrackField(const char* edge_name, | 114 | 124 | const T& value, | @@ -206,6 +216,12 @@ void MemoryTracker::TrackField(const char* edge_name, | 206 | 216 | TrackFieldWithSize(edge_name, value.size, "MallocedBuffer"); | 207 | 217 | } | 208 | 218 |
| | 219 | +void MemoryTracker::TrackField(const char* edge_name, | | 220 | + const v8::BackingStore* value, | | 221 | + const char* node_name) { | | 222 | + TrackFieldWithSize(edge_name, value->ByteLength(), "BackingStore"); | | 223 | +} | | 224 | + | 209 | 225 | void MemoryTracker::TrackField(const char* name, | 210 | 226 | const uv_buf_t& value, | 211 | 227 | const char* node_name) { | |
Collapse file Copy file name to clipboardExpand all lines: src/memory_tracker.h +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,6 +138,10 @@ class MemoryTracker { | 138 | 138 | inline void TrackField(const char* edge_name, | 139 | 139 | const std::unique_ptr<T>& value, | 140 | 140 | const char* node_name = nullptr); | | 141 | + template <typename T> | | 142 | + inline void TrackField(const char* edge_name, | | 143 | + const std::shared_ptr<T>& value, | | 144 | + const char* node_name = nullptr); | 141 | 145 |
| 142 | 146 | // For containers, the elements will be graphed as grandchildren nodes | 143 | 147 | // if the container is not empty. | @@ -197,6 +201,9 @@ class MemoryTracker { | 197 | 201 | inline void TrackField(const char* edge_name, | 198 | 202 | const MallocedBuffer<T>& value, | 199 | 203 | const char* node_name = nullptr); | | 204 | + inline void TrackField(const char* edge_name, | | 205 | + const v8::BackingStore* value, | | 206 | + const char* node_name = nullptr); | 200 | 207 | // We do not implement CleanupHookCallback as MemoryRetainer | 201 | 208 | // but instead specialize the method here to avoid the cost of | 202 | 209 | // virtual pointers. | |
Collapse file Copy file name to clipboardExpand all lines: src/node.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -328,6 +328,10 @@ NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator, | 328 | 328 | NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator, | 329 | 329 | struct uv_loop_s* event_loop, | 330 | 330 | MultiIsolatePlatform* platform); | | 331 | +NODE_EXTERN v8::Isolate* NewIsolate( | | 332 | + std::shared_ptr<ArrayBufferAllocator> allocator, | | 333 | + struct uv_loop_s* event_loop, | | 334 | + MultiIsolatePlatform* platform); | 331 | 335 |
| 332 | 336 | // Creates a new context with Node.js-specific tweaks. | 333 | 337 | NODE_EXTERN v8::Local<v8::Context> NewContext( | |
Collapse file Copy file name to clipboardExpand all lines: src/node_messaging.cc +16-49Lines changed: 16 additions & 49 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | using node::contextify::ContextifyContext; | 13 | 13 | using v8::Array; | 14 | 14 | using v8::ArrayBuffer; | 15 | | -using v8::ArrayBufferCreationMode; | | 15 | +using v8::BackingStore; | 16 | 16 | using v8::Context; | 17 | 17 | using v8::EscapableHandleScope; | 18 | 18 | using v8::Exception; | @@ -123,10 +123,9 @@ MaybeLocal<Value> Message::Deserialize(Environment* env, | 123 | 123 | std::vector<Local<SharedArrayBuffer>> shared_array_buffers; | 124 | 124 | // Attach all transferred SharedArrayBuffers to their new Isolate. | 125 | 125 | for (uint32_t i = 0; i < shared_array_buffers_.size(); ++i) { | 126 | | - Local<SharedArrayBuffer> sab; | 127 | | - if (!shared_array_buffers_[i]->GetSharedArrayBuffer(env, context) | 128 | | - .ToLocal(&sab)) | 129 | | - return MaybeLocal<Value>(); | | 126 | + Local<SharedArrayBuffer> sab = | | 127 | + SharedArrayBuffer::New(env->isolate(), | | 128 | + std::move(shared_array_buffers_[i])); | 130 | 129 | shared_array_buffers.push_back(sab); | 131 | 130 | } | 132 | 131 | shared_array_buffers_.clear(); | @@ -141,30 +140,12 @@ MaybeLocal<Value> Message::Deserialize(Environment* env, | 141 | 140 | delegate.deserializer = &deserializer; | 142 | 141 |
| 143 | 142 | // Attach all transferred ArrayBuffers to their new Isolate. | 144 | | - for (uint32_t i = 0; i < array_buffer_contents_.size(); ++i) { | 145 | | - if (!env->isolate_data()->uses_node_allocator()) { | 146 | | - // We don't use Node's allocator on the receiving side, so we have | 147 | | - // to create the ArrayBuffer from a copy of the memory. | 148 | | - AllocatedBuffer buf = | 149 | | - env->AllocateManaged(array_buffer_contents_[i].size); | 150 | | - memcpy(buf.data(), | 151 | | - array_buffer_contents_[i].data, | 152 | | - array_buffer_contents_[i].size); | 153 | | - deserializer.TransferArrayBuffer(i, buf.ToArrayBuffer()); | 154 | | - continue; | 155 | | - } | 156 | | - | 157 | | - env->isolate_data()->node_allocator()->RegisterPointer( | 158 | | - array_buffer_contents_[i].data, array_buffer_contents_[i].size); | 159 | | - | | 143 | + for (uint32_t i = 0; i < array_buffers_.size(); ++i) { | 160 | 144 | Local<ArrayBuffer> ab = | 161 | | - ArrayBuffer::New(env->isolate(), | 162 | | - array_buffer_contents_[i].release(), | 163 | | - array_buffer_contents_[i].size, | 164 | | - ArrayBufferCreationMode::kInternalized); | | 145 | + ArrayBuffer::New(env->isolate(), std::move(array_buffers_[i])); | 165 | 146 | deserializer.TransferArrayBuffer(i, ab); | 166 | 147 | } | 167 | | - array_buffer_contents_.clear(); | | 148 | + array_buffers_.clear(); | 168 | 149 |
| 169 | 150 | if (deserializer.ReadHeader(context).IsNothing()) | 170 | 151 | return MaybeLocal<Value>(); | @@ -173,8 +154,8 @@ MaybeLocal<Value> Message::Deserialize(Environment* env, | 173 | 154 | } | 174 | 155 |
| 175 | 156 | void Message::AddSharedArrayBuffer( | 176 | | - const SharedArrayBufferMetadataReference& reference) { | 177 | | - shared_array_buffers_.push_back(reference); | | 157 | + std::shared_ptr<BackingStore> backing_store) { | | 158 | + shared_array_buffers_.emplace_back(std::move(backing_store)); | 178 | 159 | } | 179 | 160 |
| 180 | 161 | void Message::AddMessagePort(std::unique_ptr<MessagePortData>&& data) { | @@ -249,16 +230,9 @@ class SerializerDelegate : public ValueSerializer::Delegate { | 249 | 230 | } | 250 | 231 | } | 251 | 232 |
| 252 | | - auto reference = SharedArrayBufferMetadata::ForSharedArrayBuffer( | 253 | | - env_, | 254 | | - context_, | 255 | | - shared_array_buffer); | 256 | | - if (!reference) { | 257 | | - return Nothing<uint32_t>(); | 258 | | - } | 259 | 233 | seen_shared_array_buffers_.emplace_back( | 260 | 234 | Global<SharedArrayBuffer> { isolate, shared_array_buffer }); | 261 | | - msg_->AddSharedArrayBuffer(reference); | | 235 | + msg_->AddSharedArrayBuffer(shared_array_buffer->GetBackingStore()); | 262 | 236 | return Just(i); | 263 | 237 | } | 264 | 238 |
| @@ -386,18 +360,12 @@ Maybe<bool> Message::Serialize(Environment* env, | 386 | 360 | } | 387 | 361 |
| 388 | 362 | for (Local<ArrayBuffer> ab : array_buffers) { | 389 | | - // If serialization succeeded, we want to take ownership of | 390 | | - // (a.k.a. externalize) the underlying memory region and render | 391 | | - // it inaccessible in this Isolate. | 392 | | - ArrayBuffer::Contents contents = ab->Externalize(); | | 363 | + // If serialization succeeded, we render it inaccessible in this Isolate. | | 364 | + std::shared_ptr<BackingStore> backing_store = ab->GetBackingStore(); | | 365 | + ab->Externalize(backing_store); | 393 | 366 | ab->Detach(); | 394 | 367 |
| 395 | | - CHECK(env->isolate_data()->uses_node_allocator()); | 396 | | - env->isolate_data()->node_allocator()->UnregisterPointer( | 397 | | - contents.Data(), contents.ByteLength()); | 398 | | - | 399 | | - array_buffer_contents_.emplace_back(MallocedBuffer<char>{ | 400 | | - static_cast<char*>(contents.Data()), contents.ByteLength()}); | | 368 | + array_buffers_.emplace_back(std::move(backing_store)); | 401 | 369 | } | 402 | 370 |
| 403 | 371 | delegate.Finish(); | @@ -411,9 +379,8 @@ Maybe<bool> Message::Serialize(Environment* env, | 411 | 379 | } | 412 | 380 |
| 413 | 381 | void Message::MemoryInfo(MemoryTracker* tracker) const { | 414 | | - tracker->TrackField("array_buffer_contents", array_buffer_contents_); | 415 | | - tracker->TrackFieldWithSize("shared_array_buffers", | 416 | | - shared_array_buffers_.size() * sizeof(shared_array_buffers_[0])); | | 382 | + tracker->TrackField("array_buffers_", array_buffers_); | | 383 | + tracker->TrackField("shared_array_buffers", shared_array_buffers_); | 417 | 384 | tracker->TrackField("message_ports", message_ports_); | 418 | 385 | } | 419 | 386 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_messaging.h +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | #include "env.h" | 7 | 7 | #include "node_mutex.h" | 8 | | -#include "sharedarraybuffer_metadata.h" | 9 | 8 | #include <list> | 10 | 9 |
| 11 | 10 | namespace node { | @@ -52,7 +51,7 @@ class Message : public MemoryRetainer { | 52 | 51 |
| 53 | 52 | // Internal method of Message that is called when a new SharedArrayBuffer | 54 | 53 | // object is encountered in the incoming value's structure. | 55 | | - void AddSharedArrayBuffer(const SharedArrayBufferMetadataReference& ref); | | 54 | + void AddSharedArrayBuffer(std::shared_ptr<v8::BackingStore> backing_store); | 56 | 55 | // Internal method of Message that is called once serialization finishes | 57 | 56 | // and that transfers ownership of `data` to this message. | 58 | 57 | void AddMessagePort(std::unique_ptr<MessagePortData>&& data); | @@ -74,8 +73,8 @@ class Message : public MemoryRetainer { | 74 | 73 |
| 75 | 74 | private: | 76 | 75 | MallocedBuffer<char> main_message_buf_; | 77 | | - std::vector<MallocedBuffer<char>> array_buffer_contents_; | 78 | | - std::vector<SharedArrayBufferMetadataReference> shared_array_buffers_; | | 76 | + std::vector<std::shared_ptr<v8::BackingStore>> array_buffers_; | | 77 | + std::vector<std::shared_ptr<v8::BackingStore>> shared_array_buffers_; | 79 | 78 | std::vector<std::unique_ptr<MessagePortData>> message_ports_; | 80 | 79 | std::vector<v8::WasmModuleObject::TransferrableModule> wasm_modules_; | 81 | 80 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_worker.cc +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,7 +51,6 @@ Worker::Worker(Environment* env, | 51 | 51 | per_isolate_opts_(per_isolate_opts), | 52 | 52 | exec_argv_(exec_argv), | 53 | 53 | platform_(env->isolate_data()->platform()), | 54 | | - array_buffer_allocator_(ArrayBufferAllocator::Create()), | 55 | 54 | start_profiler_idle_notifier_(env->profiler_idle_notifier_started()), | 56 | 55 | thread_id_(Environment::AllocateThreadId()), | 57 | 56 | env_vars_(env->env_vars()) { | @@ -95,10 +94,6 @@ bool Worker::is_stopped() const { | 95 | 94 | return stopped_; | 96 | 95 | } | 97 | 96 |
| 98 | | -std::shared_ptr<ArrayBufferAllocator> Worker::array_buffer_allocator() { | 99 | | - return array_buffer_allocator_; | 100 | | -} | 101 | | - | 102 | 97 | void Worker::UpdateResourceConstraints(ResourceConstraints* constraints) { | 103 | 98 | constraints->set_stack_limit(reinterpret_cast<uint32_t*>(stack_base_)); | 104 | 99 |
| @@ -138,9 +133,11 @@ class WorkerThreadData { | 138 | 133 | : w_(w) { | 139 | 134 | CHECK_EQ(uv_loop_init(&loop_), 0); | 140 | 135 |
| | 136 | + std::shared_ptr<ArrayBufferAllocator> allocator = | | 137 | + ArrayBufferAllocator::Create(); | 141 | 138 | Isolate::CreateParams params; | 142 | 139 | SetIsolateCreateParamsForNode(¶ms); | 143 | | - params.array_buffer_allocator = w->array_buffer_allocator_.get(); | | 140 | + params.array_buffer_allocator_shared = allocator; | 144 | 141 |
| 145 | 142 | w->UpdateResourceConstraints(¶ms.constraints); | 146 | 143 |
| @@ -164,7 +161,7 @@ class WorkerThreadData { | 164 | 161 | isolate_data_.reset(CreateIsolateData(isolate, | 165 | 162 | &loop_, | 166 | 163 | w_->platform_, | 167 | | - w->array_buffer_allocator_.get())); | | 164 | + allocator.get())); | 168 | 165 | CHECK(isolate_data_); | 169 | 166 | if (w_->per_isolate_opts_) | 170 | 167 | isolate_data_->set_options(std::move(w_->per_isolate_opts_)); | |
|