Skip to content

Commit

Permalink
src: track memory size of WebAssembly streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Apr 17, 2022
1 parent 8689723 commit 3185f41
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/node_wasm_web_api.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "node_wasm_web_api.h"

#include "memory_tracker-inl.h"
#include "node_errors.h"

namespace node {
Expand Down Expand Up @@ -32,6 +33,13 @@ void WasmStreamingObject::RegisterExternalReferences(
registry->Register(Abort);
}

void WasmStreamingObject::MemoryInfo(MemoryTracker* tracker) const {
// v8::WasmStreaming is opaque. We assume that the size of the WebAssembly
// module that is being compiled is roughly what V8 allocates (as in, off by
// only a small factor).
tracker->TrackFieldWithSize("streaming", wasm_size_);
}

v8::MaybeLocal<v8::Object> WasmStreamingObject::Create(
Environment* env, std::shared_ptr<v8::WasmStreaming> streaming) {
v8::Local<v8::Function> ctor = Initialize(env);
Expand All @@ -45,6 +53,7 @@ v8::MaybeLocal<v8::Object> WasmStreamingObject::Create(
WasmStreamingObject* ptr = Unwrap<WasmStreamingObject>(obj);
CHECK_NOT_NULL(ptr);
ptr->streaming_ = streaming;
ptr->wasm_size_ = 0;
return obj;
}

Expand Down Expand Up @@ -88,6 +97,7 @@ void WasmStreamingObject::Push(
// Forward the data to V8. Internally, V8 will make a copy.
obj->streaming_->OnBytesReceived(
static_cast<const uint8_t*>(bytes) + offset, size);
obj->wasm_size_ += size;
}

void WasmStreamingObject::Finish(
Expand Down
3 changes: 2 additions & 1 deletion src/node_wasm_web_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WasmStreamingObject final : public BaseObject {

static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

void MemoryInfo(MemoryTracker* tracker) const override {}
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(WasmStreamingObject)
SET_SELF_SIZE(WasmStreamingObject)

Expand All @@ -38,6 +38,7 @@ class WasmStreamingObject final : public BaseObject {
static void Abort(const v8::FunctionCallbackInfo<v8::Value>& args);

std::shared_ptr<v8::WasmStreaming> streaming_;
size_t wasm_size_;
};

// This is a v8::WasmStreamingCallback implementation that must be passed to
Expand Down

0 comments on commit 3185f41

Please sign in to comment.