Skip to content

[Experimental] Measure the space used by vtable pointers #7487

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

Open
wants to merge 1 commit into
base: heap-snapshot-analysis
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/passes/HeapSnapshotAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,44 @@ struct HeapSnapshotAnalysis : Pass {
auto [name, allocs] = topTypesByAllocations[i];
std::cout << " " << name << ": " << allocs << " objects\n";
}

// Measure the savings of removing vtable pointers. Assume any object that
// contains an edge to another object that contains an edge to a "system /
// WasmFuncRef" contains a vtable pointer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How precise do you think this is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be fairly precise. I don't know of any other patterns involving function references, do you? I have samples with name sections so I can do another analysis where I just look for "vtable" in the type name and compare the results to make sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I know of one, but I remember talk about caching function pointers in some cases... Good idea to compare to types with "vtable" in the name, yeah, that would give a lot more confidence.

IString funcrefName = "system / WasmFuncRef";
std::unordered_set<Index> vtableIndices;
for (Index i = 0; i < nodes.size(); ++i) {
for (Index j = 0; j < nodes[i].edges.size(); ++j) {
auto dest = nodes[i].edges[j];
if (dest >= nodes.size()) {
Fatal() << "Unexpected out-of-bounds edge (" << dest
<< " >= " << nodes.size() << ")";
}
if (nodes[dest].name == funcrefName) {
vtableIndices.insert(i);
break;
}
}
}
Index objectsWithVtableCount = 0;
for (Index i = 0; i < nodes.size(); ++i) {
for (Index j = 0; j < nodes[i].edges.size(); ++j) {
auto dest = nodes[i].edges[j];
assert(dest < nodes.size());
if (vtableIndices.count(dest)) {
++objectsWithVtableCount;
break;
}
}
}

Index vtableSavings = objectsWithVtableCount * 4;
std::cout << "\nRemoving vtable fields would save " << vtableSavings
<< " bytes ("
<< (double(vtableSavings) / double(totalWasmHeapSize) * 100)
<< "% wasm heap, "
<< (double(vtableSavings) / double(totalHeapSize) * 100)
<< "% total heap)\n";
}

void validateSnapshotMeta(json::Value& snapshotRoot) {
Expand Down
Loading