forked from google/pprof-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 7
Improve heap profile memory usage by lazily loading js objects #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7d2db13
Improve heap profile memoery usage by lazily loading js objects
IlyasShabi d2982ce
format cc files
IlyasShabi 4c77cf6
fix removing file:// prefix from mjs scriptname
IlyasShabi 856f3ab
remove holder and use v8 external and local conversion
IlyasShabi b0caf9a
profile v2 tests
IlyasShabi d9d6377
Merge branch 'main' into ishabi/js-objects-allocations-improvements
IlyasShabi 4b23c6f
Compare memory usage between current and v2 version
IlyasShabi fbcdce8
compare total heap used
IlyasShabi 147ebfe
use callback to get allocations profile lazily
IlyasShabi cee823e
rename function to mapAllocationProfile
IlyasShabi edf3d89
encpsulate getter common part
IlyasShabi cca6f8b
increase timeout
IlyasShabi 0d725e5
use count/size single instance outside allocations loop
IlyasShabi 4a074f4
Merge branch 'main' into ishabi/js-objects-allocations-improvements
IlyasShabi 47416ea
increase timeout of comparison test
IlyasShabi 59827c3
reduce AllocationProfileNodeView allocations by using internal pointe…
IlyasShabi 1a66a02
add warning about v8profilev2 callback usage
IlyasShabi a683217
avoid mutating scriptName on getter only
IlyasShabi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * Copyright 2026 Datadog, Inc | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "allocation-profile-node.hh" | ||
| #include "per-isolate-data.hh" | ||
|
|
||
| using namespace v8; | ||
|
|
||
| namespace dd { | ||
|
|
||
| template <typename F> | ||
| void AllocationProfileNodeView::mapAllocationProfileNode( | ||
| const Nan::PropertyCallbackInfo<Value>& info, F&& mapper) { | ||
| auto* node = static_cast<AllocationProfile::Node*>( | ||
| Nan::GetInternalFieldPointer(info.Holder(), 0)); | ||
| info.GetReturnValue().Set(mapper(node)); | ||
| } | ||
|
|
||
| NAN_MODULE_INIT(AllocationProfileNodeView::Init) { | ||
| Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(); | ||
| tpl->SetClassName(Nan::New("AllocationProfileNode").ToLocalChecked()); | ||
| tpl->InstanceTemplate()->SetInternalFieldCount(1); | ||
|
|
||
| auto inst = tpl->InstanceTemplate(); | ||
| Nan::SetAccessor(inst, Nan::New("name").ToLocalChecked(), GetName); | ||
| Nan::SetAccessor( | ||
| inst, Nan::New("scriptName").ToLocalChecked(), GetScriptName); | ||
| Nan::SetAccessor(inst, Nan::New("scriptId").ToLocalChecked(), GetScriptId); | ||
| Nan::SetAccessor( | ||
| inst, Nan::New("lineNumber").ToLocalChecked(), GetLineNumber); | ||
| Nan::SetAccessor( | ||
| inst, Nan::New("columnNumber").ToLocalChecked(), GetColumnNumber); | ||
| Nan::SetAccessor( | ||
| inst, Nan::New("allocations").ToLocalChecked(), GetAllocations); | ||
| Nan::SetAccessor(inst, Nan::New("children").ToLocalChecked(), GetChildren); | ||
|
|
||
| PerIsolateData::For(Isolate::GetCurrent()) | ||
| ->AllocationNodeConstructor() | ||
| .Reset(Nan::GetFunction(tpl).ToLocalChecked()); | ||
| } | ||
|
|
||
| Local<Object> AllocationProfileNodeView::New(AllocationProfile::Node* node) { | ||
| auto* isolate = Isolate::GetCurrent(); | ||
|
|
||
| Local<Function> constructor = | ||
| Nan::New(PerIsolateData::For(isolate)->AllocationNodeConstructor()); | ||
|
|
||
| Local<Object> obj = Nan::NewInstance(constructor).ToLocalChecked(); | ||
|
|
||
| Nan::SetInternalFieldPointer(obj, 0, node); | ||
|
|
||
| return obj; | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetName) { | ||
| mapAllocationProfileNode( | ||
| info, [](AllocationProfile::Node* node) { return node->name; }); | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetScriptName) { | ||
| mapAllocationProfileNode( | ||
| info, [](AllocationProfile::Node* node) { return node->script_name; }); | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetScriptId) { | ||
| mapAllocationProfileNode( | ||
| info, [](AllocationProfile::Node* node) { return node->script_id; }); | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetLineNumber) { | ||
| mapAllocationProfileNode( | ||
| info, [](AllocationProfile::Node* node) { return node->line_number; }); | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetColumnNumber) { | ||
| mapAllocationProfileNode( | ||
| info, [](AllocationProfile::Node* node) { return node->column_number; }); | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetAllocations) { | ||
| mapAllocationProfileNode(info, [](AllocationProfile::Node* node) { | ||
| auto* isolate = Isolate::GetCurrent(); | ||
| auto context = isolate->GetCurrentContext(); | ||
|
|
||
| const auto& allocations = node->allocations; | ||
| Local<Array> arr = Array::New(isolate, allocations.size()); | ||
| auto sizeBytes = String::NewFromUtf8Literal(isolate, "sizeBytes"); | ||
| auto count = String::NewFromUtf8Literal(isolate, "count"); | ||
|
|
||
| for (size_t i = 0; i < allocations.size(); i++) { | ||
| const auto& alloc = allocations[i]; | ||
| Local<Object> alloc_obj = Object::New(isolate); | ||
| Nan::Set(alloc_obj, | ||
| sizeBytes, | ||
| Number::New(isolate, static_cast<double>(alloc.size))); | ||
| Nan::Set(alloc_obj, | ||
| count, | ||
| Number::New(isolate, static_cast<double>(alloc.count))); | ||
| arr->Set(context, i, alloc_obj).Check(); | ||
| } | ||
| return arr; | ||
| }); | ||
| } | ||
|
|
||
| NAN_GETTER(AllocationProfileNodeView::GetChildren) { | ||
| mapAllocationProfileNode(info, [](AllocationProfile::Node* node) { | ||
| auto* isolate = Isolate::GetCurrent(); | ||
| auto context = isolate->GetCurrentContext(); | ||
|
|
||
| const auto& children = node->children; | ||
| Local<Array> arr = Array::New(isolate, children.size()); | ||
| for (size_t i = 0; i < children.size(); i++) { | ||
| arr->Set(context, i, AllocationProfileNodeView::New(children[i])).Check(); | ||
| } | ||
| return arr; | ||
| }); | ||
| } | ||
|
|
||
| } // namespace dd | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2026 Datadog, Inc | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <nan.h> | ||
| #include <v8-profiler.h> | ||
|
|
||
| namespace dd { | ||
|
|
||
| class AllocationProfileNodeView { | ||
| public: | ||
| static NAN_MODULE_INIT(Init); | ||
|
|
||
| static v8::Local<v8::Object> New(v8::AllocationProfile::Node* node); | ||
|
|
||
| private: | ||
| template <typename F> | ||
| static void mapAllocationProfileNode( | ||
| const Nan::PropertyCallbackInfo<v8::Value>& info, F&& mapper); | ||
|
|
||
| static NAN_GETTER(GetName); | ||
| static NAN_GETTER(GetScriptName); | ||
| static NAN_GETTER(GetScriptId); | ||
| static NAN_GETTER(GetLineNumber); | ||
| static NAN_GETTER(GetColumnNumber); | ||
| static NAN_GETTER(GetAllocations); | ||
| static NAN_GETTER(GetChildren); | ||
| }; | ||
|
|
||
| } // namespace dd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.