Skip to content

Commit

Permalink
Include array length in description for V8 heap snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
190n committed Jan 3, 2025
1 parent 8a403a9 commit 248e65e
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions Source/JavaScriptCore/heap/BunV8HeapSnapshotBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ void BunV8HeapSnapshotBuilder::analyzeNode(JSCell* cell)
}
}


unsigned BunV8HeapSnapshotBuilder::analyzeNodeInternal(JSCell* cell, void* optionalHashId)
{

Expand Down Expand Up @@ -230,7 +229,6 @@ void BunV8HeapSnapshotBuilder::analyzeVariableNameEdge(JSCell* from, JSCell* to,
edge.typeIndex = static_cast<unsigned>(V8EdgeType::Context);
edge.name = String(variableName);


m_edges.append(WTFMove(edge));
}

Expand All @@ -250,9 +248,10 @@ void BunV8HeapSnapshotBuilder::analyzeIndexEdge(JSCell* from, JSCell* to, uint32
}

void BunV8HeapSnapshotBuilder::setOpaqueRootReachabilityReasonForCell(JSCell*, ASCIILiteral) {}
void BunV8HeapSnapshotBuilder::setWrappedObjectForCell(JSCell* cell, void* wrappedObject) {
void BunV8HeapSnapshotBuilder::setWrappedObjectForCell(JSCell* cell, void* wrappedObject)
{
unsigned id = getOrCreateNodeId(cell, wrappedObject);

// TODO: make this one lock instead of two.
Locker locker { m_buildingNodeMutex };
m_nodes[id].id = generateHashID(cell, wrappedObject);
Expand All @@ -277,7 +276,6 @@ unsigned BunV8HeapSnapshotBuilder::getOrCreateNodeId(JSCell* cell, void* optiona
if (it != m_cellToNodeId.end())
return it->value;


unsigned id = analyzeNodeInternal(cell, optionalHashId);
m_cellToNodeId.set(cell, id);
return id;
Expand Down Expand Up @@ -430,7 +428,7 @@ String BunV8HeapSnapshotBuilder::getDetailedNodeType(JSCell* cell)
if (object) {
// For arrays, include the length
if (JSArray* array = jsDynamicCast<JSArray*>(cell)) {
return "Array"_s;
return makeString("Array ("_s, array->length(), ")"_s);
}

// For functions, try to get the display name
Expand Down Expand Up @@ -565,20 +563,19 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()
// First sort by fromNodeId
if (a.fromNodeId != b.fromNodeId)
return a.fromNodeId < b.fromNodeId;

// Then by typeIndex
if (a.typeIndex != b.typeIndex)
return a.typeIndex < b.typeIndex;

// Then by toNodeId
if (a.toNodeId != b.toNodeId)
return a.toNodeId < b.toNodeId;

// For element/hidden edges, compare by index
if (a.typeIndex == static_cast<unsigned>(V8EdgeType::Element) ||
a.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden))
if (a.typeIndex == static_cast<unsigned>(V8EdgeType::Element) || a.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden))
return a.index < b.index;

// For named edges, compare by name
return WTF::codePointCompareLessThan(a.name, b.name);
});
Expand All @@ -593,13 +590,10 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()
const auto& curr = m_edges[readIndex];

// Check if this is a duplicate edge
bool isDuplicate = prev.fromNodeId == curr.fromNodeId &&
prev.toNodeId == curr.toNodeId &&
prev.typeIndex == curr.typeIndex;
bool isDuplicate = prev.fromNodeId == curr.fromNodeId && prev.toNodeId == curr.toNodeId && prev.typeIndex == curr.typeIndex;

if (isDuplicate) {
if (prev.typeIndex == static_cast<unsigned>(V8EdgeType::Element) ||
prev.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden)) {
if (prev.typeIndex == static_cast<unsigned>(V8EdgeType::Element) || prev.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden)) {
isDuplicate = prev.index == curr.index;
} else {
isDuplicate = prev.name == curr.name;
Expand Down Expand Up @@ -702,7 +696,7 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()
json.append("\"edges\":["_s);
for (unsigned i = 0; i < m_edges.size(); ++i) {
const auto& edge = m_edges[i];

// Validate node IDs
ASSERT(edge.fromNodeId < m_nodes.size());
ASSERT(edge.toNodeId < m_nodes.size());
Expand Down Expand Up @@ -768,4 +762,4 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()

} // namespace JSC

#endif // USE(BUN_JSC_ADDITIONS)
#endif // USE(BUN_JSC_ADDITIONS)

0 comments on commit 248e65e

Please sign in to comment.