Bug 1779685 - Factor ChunkedJSONWriteFunc::Length() out of CopyDataIntoLazilyAllocatedBuffer() - r=florian
authorGerald Squelart <gsquelart@mozilla.com>
Wed, 20 Jul 2022 12:52:57 +0000
changeset 624504 42b6aac07798c43312880bd6a2d62d27c0436f94
parent 624503 a5dbb1db6794a38991d5a5536e762ba3bebfecea
child 624505 2b1c4f14a2cfb5ccdd76d24c47ec9f2a4dd3f8bb
push id40007
push usersmolnar@mozilla.com
push dateWed, 20 Jul 2022 21:52:02 +0000
treeherdermozilla-central@16a4302fb1a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian
bugs1779685
milestone104.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1779685 - Factor ChunkedJSONWriteFunc::Length() out of CopyDataIntoLazilyAllocatedBuffer() - r=florian This will be useful to see how big profiles get during gathering, and potentially to avoid making them too big if possible. Differential Revision: https://phabricator.services.mozilla.com/D151901
mozglue/baseprofiler/public/BaseProfileJSONWriter.h
mozglue/tests/TestBaseProfiler.cpp
--- a/mozglue/baseprofiler/public/BaseProfileJSONWriter.h
+++ b/mozglue/baseprofiler/public/BaseProfileJSONWriter.h
@@ -36,16 +36,27 @@ class ChunkedJSONWriteFunc final : publi
   }
 
   bool IsEmpty() const {
     MOZ_ASSERT_IF(!mChunkPtr, !mChunkEnd && mChunkList.length() == 0 &&
                                   mChunkLengths.length() == 0);
     return !mChunkPtr;
   }
 
+  // Length of data written so far, excluding null terminator.
+  size_t Length() const {
+    MOZ_ASSERT(mChunkLengths.length() == mChunkList.length());
+    size_t totalLen = 0;
+    for (size_t i = 0; i < mChunkLengths.length(); i++) {
+      MOZ_ASSERT(strlen(mChunkList[i].get()) == mChunkLengths[i]);
+      totalLen += mChunkLengths[i];
+    }
+    return totalLen;
+  }
+
   void Write(const Span<const char>& aStr) override {
     MOZ_ASSERT(mChunkPtr >= mChunkList.back().get() && mChunkPtr <= mChunkEnd);
     MOZ_ASSERT(mChunkEnd >= mChunkList.back().get() + mChunkLengths.back());
     MOZ_ASSERT(*mChunkPtr == '\0');
 
     // Most strings to be written are small, but subprocess profiles (e.g.,
     // from the content process in e10s) may be huge. If the string is larger
     // than a chunk, allocate its own chunk.
@@ -64,23 +75,17 @@ class ChunkedJSONWriteFunc final : publi
     memcpy(mChunkPtr, aStr.data(), aStr.size());
     *newPtr = '\0';
     mChunkPtr = newPtr;
     mChunkLengths.back() += aStr.size();
   }
   void CopyDataIntoLazilyAllocatedBuffer(
       const std::function<char*(size_t)>& aAllocator) const {
     // Request a buffer for the full content plus a null terminator.
-    MOZ_ASSERT(mChunkLengths.length() == mChunkList.length());
-    size_t totalLen = 1;
-    for (size_t i = 0; i < mChunkLengths.length(); i++) {
-      MOZ_ASSERT(strlen(mChunkList[i].get()) == mChunkLengths[i]);
-      totalLen += mChunkLengths[i];
-    }
-    char* ptr = aAllocator(totalLen);
+    char* ptr = aAllocator(Length() + 1);
 
     if (!ptr) {
       // Failed to allocate memory.
       return;
     }
 
     for (size_t i = 0; i < mChunkList.length(); i++) {
       size_t len = mChunkLengths[i];
--- a/mozglue/tests/TestBaseProfiler.cpp
+++ b/mozglue/tests/TestBaseProfiler.cpp
@@ -4850,16 +4850,18 @@ static void VerifyUniqueStringContents(
     { uniqueStrings.SpliceStringTableElements(writer); }
     writer.EndArray();
   }
   writer.End();
 
   UniquePtr<char[]> jsonString = writer.ChunkedWriteFunc().CopyData();
   MOZ_RELEASE_ASSERT(jsonString);
   std::string_view jsonStringView(jsonString.get());
+  const size_t length = writer.ChunkedWriteFunc().Length();
+  MOZ_RELEASE_ASSERT(length == jsonStringView.length());
   std::string expected = "{\"data\": [";
   expected += aExpectedData;
   expected += "], \"stringTable\": [";
   expected += aExpectedUniqueStrings;
   expected += "]}\n";
   if (jsonStringView != expected) {
     fprintf(stderr,
             "Expected:\n"