Skip to content

Commit a8b3f73

Browse files
Report memory size of performance.measure / performance.mark (#16094)
1 parent ee95559 commit a8b3f73

16 files changed

Lines changed: 121 additions & 0 deletions

‎src/bun.js/bindings/JSDOMExceptionHandling.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ JSValue createDOMException(JSGlobalObject* lexicalGlobalObject, ExceptionCode ec
164164
return createRangeError(lexicalGlobalObject, "Bad value"_s);
165165
return createRangeError(lexicalGlobalObject, message);
166166

167+
case ExceptionCode::SyntaxError:
167168
case ExceptionCode::JSSyntaxError:
168169
if (message.isEmpty())
169170
return createSyntaxError(lexicalGlobalObject);

‎src/bun.js/bindings/webcore/JSPerformance.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ JSPerformance::JSPerformance(Structure* structure, JSDOMGlobalObject& globalObje
255255
{
256256
}
257257

258+
size_t JSPerformance::estimatedSize(JSCell* cell, VM& vm)
259+
{
260+
JSPerformance* thisObject = jsCast<JSPerformance*>(cell);
261+
return Base::estimatedSize(cell, vm) + thisObject->wrapped().memoryCost();
262+
}
263+
258264
// static_assert(!std::is_base_of<ActiveDOMObject, Performance>::value, "Interface is not marked as [ActiveDOMObject] even though implementation class subclasses ActiveDOMObject.");
259265

260266
void JSPerformance::finishCreation(VM& vm)

‎src/bun.js/bindings/webcore/JSPerformance.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class JSPerformance : public JSEventTarget {
6060
}
6161
static JSC::GCClient::IsoSubspace* subspaceForImpl(JSC::VM& vm);
6262
static void analyzeHeap(JSCell*, JSC::HeapAnalyzer&);
63+
static size_t estimatedSize(JSC::JSCell* cell, JSC::VM& vm);
6364
Performance& wrapped() const
6465
{
6566
return static_cast<Performance&>(Base::wrapped());

‎src/bun.js/bindings/webcore/Performance.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ Vector<RefPtr<PerformanceEntry>> Performance::getEntriesByType(const String& ent
189189
return entries;
190190
}
191191

192+
size_t Performance::memoryCost() const
193+
{
194+
size_t size = sizeof(Performance);
195+
size += m_resourceTimingBuffer.size() * sizeof(PerformanceResourceTiming);
196+
if (m_userTiming) {
197+
size += m_userTiming->memoryCost();
198+
}
199+
return size;
200+
}
201+
192202
Vector<RefPtr<PerformanceEntry>> Performance::getEntriesByName(const String& name, const String& entryType) const
193203
{
194204
Vector<RefPtr<PerformanceEntry>> entries;

‎src/bun.js/bindings/webcore/Performance.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class Performance final : public RefCounted<Performance>, public ContextDestruct
111111

112112
// void reportFirstContentfulPaint();
113113

114+
size_t memoryCost() const;
115+
114116
void removeAllObservers();
115117
void registerPerformanceObserver(PerformanceObserver&);
116118
void unregisterPerformanceObserver(PerformanceObserver&);

‎src/bun.js/bindings/webcore/PerformanceEntry.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "config.h"
3232
#include "PerformanceEntry.h"
3333

34+
#include "PerformanceMark.h"
35+
#include "PerformanceMeasure.h"
36+
#include "PerformanceResourceTiming.h"
37+
3438
// #include "DeprecatedGlobalSettings.h"
3539

3640
namespace WebCore {
@@ -46,6 +50,28 @@ PerformanceEntry::PerformanceEntry(const String& name, double startTime, double
4650

4751
PerformanceEntry::~PerformanceEntry() = default;
4852

53+
size_t PerformanceEntry::memoryCost() const
54+
{
55+
size_t baseCost = this->m_name.sizeInBytes();
56+
switch (performanceEntryType()) {
57+
case Type::Mark: {
58+
const PerformanceMark* mark = static_cast<const PerformanceMark*>(this);
59+
return mark->memoryCost() + baseCost;
60+
}
61+
case Type::Measure: {
62+
const PerformanceMeasure* measure = static_cast<const PerformanceMeasure*>(this);
63+
return measure->memoryCost() + baseCost;
64+
}
65+
case Type::Resource: {
66+
const PerformanceResourceTiming* resource = static_cast<const PerformanceResourceTiming*>(this);
67+
return resource->memoryCost() + baseCost;
68+
}
69+
default: {
70+
return sizeof(PerformanceEntry) + baseCost;
71+
}
72+
}
73+
}
74+
4975
std::optional<PerformanceEntry::Type> PerformanceEntry::parseEntryTypeString(const String& entryType)
5076
{
5177
if (entryType == "navigation"_s)

‎src/bun.js/bindings/webcore/PerformanceEntry.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class PerformanceEntry : public RefCounted<PerformanceEntry> {
5959
virtual Type performanceEntryType() const = 0;
6060
virtual ASCIILiteral entryType() const = 0;
6161

62+
size_t memoryCost() const;
63+
6264
static std::optional<Type> parseEntryTypeString(const String& entryType);
6365

6466
static bool startTimeCompareLessThan(const RefPtr<PerformanceEntry>& a, const RefPtr<PerformanceEntry>& b)

‎src/bun.js/bindings/webcore/PerformanceMark.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ PerformanceMark::PerformanceMark(const String& name, double startTime, RefPtr<Se
8585

8686
PerformanceMark::~PerformanceMark() = default;
8787

88+
size_t PerformanceMark::memoryCost() const
89+
{
90+
size_t size = sizeof(PerformanceMark);
91+
if (m_serializedDetail) {
92+
size += m_serializedDetail->memoryCost();
93+
}
94+
return size;
95+
}
96+
8897
JSC::JSValue PerformanceMark::detail(JSC::JSGlobalObject& globalObject)
8998
{
9099
if (!m_serializedDetail) {

‎src/bun.js/bindings/webcore/PerformanceMark.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class PerformanceMark final : public PerformanceEntry {
4444

4545
JSC::JSValue detail(JSC::JSGlobalObject&);
4646

47+
size_t memoryCost() const;
48+
4749
private:
4850
PerformanceMark(const String& name, double startTime, RefPtr<SerializedScriptValue>&&);
4951
~PerformanceMark();

‎src/bun.js/bindings/webcore/PerformanceMeasure.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030

3131
namespace WebCore {
3232

33+
size_t PerformanceMeasure::memoryCost() const
34+
{
35+
size_t size = sizeof(PerformanceMeasure);
36+
if (m_serializedDetail) {
37+
size += m_serializedDetail->memoryCost();
38+
}
39+
return size;
40+
}
41+
3342
ExceptionOr<Ref<PerformanceMeasure>> PerformanceMeasure::create(const String& name, double startTime, double endTime, RefPtr<SerializedScriptValue>&& serializedDetail)
3443
{
3544
return adoptRef(*new PerformanceMeasure(name, startTime, endTime, WTFMove(serializedDetail)));

0 commit comments

Comments
 (0)