3939
4040namespace JSC {
4141
42+ namespace {
43+
44+ class FileOutputStingBuilder {
45+ WTF_MAKE_NONCOPYABLE (FileOutputStingBuilder);
46+
47+ FileSystem::PlatformFileHandle fileHandle;
48+ StringBuilder builder;
49+
50+ public:
51+ FileOutputStingBuilder (FileSystem::PlatformFileHandle fileHandle)
52+ : fileHandle(WTFMove(fileHandle))
53+ {
54+ }
55+
56+ ~FileOutputStingBuilder ()
57+ {
58+ flush (true );
59+ }
60+
61+ template <typename ... StringTypes> void append (StringTypes... fragment)
62+ {
63+ builder.append (fragment...);
64+ flush ();
65+ }
66+
67+ void appendQuotedJSONString (const String& string)
68+ {
69+ builder.appendQuotedJSONString (string);
70+ flush ();
71+ }
72+
73+ void flush (bool force = false )
74+ {
75+ constexpr size_t kBufferLengthLimit = 4 * WTF::KB;
76+ if ((force && !builder.isEmpty ()) || builder.length () >= kBufferLengthLimit )
77+ {
78+ CString utf8String = builder.toStringPreserveCapacity ().utf8 ();
79+ FileSystem::writeToFile (fileHandle, utf8String.data (), utf8String.length ());
80+ builder.clear ();
81+ builder.reserveCapacity (kBufferLengthLimit );
82+ }
83+ }
84+ };
85+
86+ } // namespace
87+
4288NodeIdentifier HeapSnapshotBuilder::nextAvailableObjectIdentifier = 1 ;
4389NodeIdentifier HeapSnapshotBuilder::getNextObjectIdentifier () { return nextAvailableObjectIdentifier++; }
4490void HeapSnapshotBuilder::resetNextAvailableObjectIdentifier () { HeapSnapshotBuilder::nextAvailableObjectIdentifier = 1 ; }
@@ -329,6 +375,20 @@ String HeapSnapshotBuilder::descriptionForCell(JSCell *cell) const
329375}
330376
331377String HeapSnapshotBuilder::json (Function<bool (const HeapSnapshotNode&)> allowNodeCallback)
378+ {
379+ StringBuilder json;
380+ writeJson (WTFMove (allowNodeCallback), json);
381+ return json.toString ();
382+ }
383+
384+ void HeapSnapshotBuilder::writeJsonToFile (FileSystem::PlatformFileHandle fileHandle)
385+ {
386+ FileOutputStingBuilder json (fileHandle);
387+ writeJson ([] (const HeapSnapshotNode&) { return true ; }, json);
388+ }
389+
390+ template <typename OutputStingBuilder>
391+ void HeapSnapshotBuilder::writeJson (Function<bool (const HeapSnapshotNode&)>&& allowNodeCallback, OutputStingBuilder &json)
332392{
333393 VM& vm = m_profiler.vm ();
334394 DeferGCForAWhile deferGC (vm);
@@ -350,7 +410,6 @@ String HeapSnapshotBuilder::json(Function<bool (const HeapSnapshotNode&)> allowN
350410 HashMap<UniquedStringImpl*, unsigned > edgeNameIndexes;
351411 unsigned nextEdgeNameIndex = 0 ;
352412
353- StringBuilder json;
354413
355414 auto appendNodeJSON = [&] (const HeapSnapshotNode& node) {
356415 // Let the client decide if they want to allow or disallow certain nodes.
@@ -615,7 +674,6 @@ String HeapSnapshotBuilder::json(Function<bool (const HeapSnapshotNode&)> allowN
615674 }
616675
617676 json.append (' }' );
618- return json.toString ();
619677}
620678
621679} // namespace JSC
0 commit comments