Commit 21ddd20f authored by Ted Campbell's avatar Ted Campbell
Browse files

Bug 1565720 - Use UniqueChars in MemoryMetrics.{cpp,h}. r=jwalden,froydnj

Avoid manual allocations and use default constructors when possible to
simplify code.

Differential Revision: https://phabricator.services.mozilla.com/D37946

--HG--
extra : moz-landing-system : lando
parent 5cb0d4bf
Loading
Loading
Loading
Loading
+16 −28
Original line number Diff line number Diff line
@@ -265,17 +265,13 @@ struct ShapeInfo {
 * holds a copy of the filename.
 */
struct NotableClassInfo : public ClassInfo {
  NotableClassInfo();
  NotableClassInfo(const char* className, const ClassInfo& info);
  NotableClassInfo(NotableClassInfo&& info);
  NotableClassInfo& operator=(NotableClassInfo&& info);

  ~NotableClassInfo() { js_free(className_); }
  NotableClassInfo() = default;
  NotableClassInfo(NotableClassInfo&&) = default;
  NotableClassInfo(const NotableClassInfo& info) = delete;

  char* className_;
  NotableClassInfo(const char* className, const ClassInfo& info);

 private:
  NotableClassInfo(const NotableClassInfo& info) = delete;
  UniqueChars className_ = nullptr;
};

/** Data for tracking JIT-code memory usage. */
@@ -385,18 +381,14 @@ struct StringInfo {
struct NotableStringInfo : public StringInfo {
  static const size_t MAX_SAVED_CHARS = 1024;

  NotableStringInfo();
  NotableStringInfo(JSString* str, const StringInfo& info);
  NotableStringInfo(NotableStringInfo&& info);
  NotableStringInfo& operator=(NotableStringInfo&& info);
  NotableStringInfo() = default;
  NotableStringInfo(NotableStringInfo&&) = default;
  NotableStringInfo(const NotableStringInfo&) = delete;

  ~NotableStringInfo() { js_free(buffer); }

  char* buffer;
  size_t length;
  NotableStringInfo(JSString* str, const StringInfo& info);

 private:
  NotableStringInfo(const NotableStringInfo& info) = delete;
  UniqueChars buffer = nullptr;
  size_t length = 0;
};

/**
@@ -446,17 +438,13 @@ struct ScriptSourceInfo {
 * class holds a copy of the filename.
 */
struct NotableScriptSourceInfo : public ScriptSourceInfo {
  NotableScriptSourceInfo();
  NotableScriptSourceInfo(const char* filename, const ScriptSourceInfo& info);
  NotableScriptSourceInfo(NotableScriptSourceInfo&& info);
  NotableScriptSourceInfo& operator=(NotableScriptSourceInfo&& info);
  NotableScriptSourceInfo() = default;
  NotableScriptSourceInfo(NotableScriptSourceInfo&&) = default;
  NotableScriptSourceInfo(const NotableScriptSourceInfo&) = delete;

  ~NotableScriptSourceInfo() { js_free(filename_); }

  char* filename_;
  NotableScriptSourceInfo(const char* filename, const ScriptSourceInfo& info);

 private:
  NotableScriptSourceInfo(const NotableScriptSourceInfo& info) = delete;
  UniqueChars filename_ = nullptr;
};

struct HelperThreadStats {
+9 −66
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
#include "gc/PublicIterators.h"
#include "jit/BaselineJIT.h"
#include "jit/Ion.h"
#include "util/Text.h"
#include "vm/ArrayObject.h"
#include "vm/BigIntType.h"
#include "vm/HelperThreads.h"
@@ -118,8 +119,6 @@ bool InefficientNonFlatteningStringHashPolicy::match(const JSString* const& k,

namespace JS {

NotableStringInfo::NotableStringInfo() : StringInfo(), buffer(0), length(0) {}

template <typename CharT>
static void StoreStringChars(char* buffer, size_t bufferSize, JSString* str) {
  const CharT* chars;
@@ -145,82 +144,33 @@ static void StoreStringChars(char* buffer, size_t bufferSize, JSString* str) {
NotableStringInfo::NotableStringInfo(JSString* str, const StringInfo& info)
    : StringInfo(info), length(str->length()) {
  size_t bufferSize = Min(str->length() + 1, size_t(MAX_SAVED_CHARS));
  buffer = js_pod_malloc<char>(bufferSize);
  buffer.reset(js_pod_malloc<char>(bufferSize));
  if (!buffer) {
    MOZ_CRASH("oom");
  }

  if (str->hasLatin1Chars()) {
    StoreStringChars<Latin1Char>(buffer, bufferSize, str);
    StoreStringChars<Latin1Char>(buffer.get(), bufferSize, str);
  } else {
    StoreStringChars<char16_t>(buffer, bufferSize, str);
  }
    StoreStringChars<char16_t>(buffer.get(), bufferSize, str);
  }

NotableStringInfo::NotableStringInfo(NotableStringInfo&& info)
    : StringInfo(std::move(info)), length(info.length) {
  buffer = info.buffer;
  info.buffer = nullptr;
}

NotableStringInfo& NotableStringInfo::operator=(NotableStringInfo&& info) {
  MOZ_ASSERT(this != &info, "self-move assignment is prohibited");
  this->~NotableStringInfo();
  new (this) NotableStringInfo(std::move(info));
  return *this;
}

NotableClassInfo::NotableClassInfo() : ClassInfo(), className_(nullptr) {}

NotableClassInfo::NotableClassInfo(const char* className, const ClassInfo& info)
    : ClassInfo(info) {
  size_t bytes = strlen(className) + 1;
  className_ = js_pod_malloc<char>(bytes);
  className_ = DuplicateString(className);
  if (!className_) {
    MOZ_CRASH("oom");
  }
  PodCopy(className_, className, bytes);
}

NotableClassInfo::NotableClassInfo(NotableClassInfo&& info)
    : ClassInfo(std::move(info)) {
  className_ = info.className_;
  info.className_ = nullptr;
}

NotableClassInfo& NotableClassInfo::operator=(NotableClassInfo&& info) {
  MOZ_ASSERT(this != &info, "self-move assignment is prohibited");
  this->~NotableClassInfo();
  new (this) NotableClassInfo(std::move(info));
  return *this;
}

NotableScriptSourceInfo::NotableScriptSourceInfo()
    : ScriptSourceInfo(), filename_(nullptr) {}

NotableScriptSourceInfo::NotableScriptSourceInfo(const char* filename,
                                                 const ScriptSourceInfo& info)
    : ScriptSourceInfo(info) {
  size_t bytes = strlen(filename) + 1;
  filename_ = js_pod_malloc<char>(bytes);
  filename_ = DuplicateString(filename);
  if (!filename_) {
    MOZ_CRASH("oom");
  }
  PodCopy(filename_, filename, bytes);
}

NotableScriptSourceInfo::NotableScriptSourceInfo(NotableScriptSourceInfo&& info)
    : ScriptSourceInfo(std::move(info)) {
  filename_ = info.filename_;
  info.filename_ = nullptr;
}

NotableScriptSourceInfo& NotableScriptSourceInfo::operator=(
    NotableScriptSourceInfo&& info) {
  MOZ_ASSERT(this != &info, "self-move assignment is prohibited");
  this->~NotableScriptSourceInfo();
  new (this) NotableScriptSourceInfo(std::move(info));
  return *this;
}

}  // namespace JS
@@ -594,12 +544,10 @@ static bool FindNotableStrings(ZoneStats& zStats) {
      continue;
    }

    if (!zStats.notableStrings.growBy(1)) {
    if (!zStats.notableStrings.emplaceBack(str, info)) {
      return false;
    }

    zStats.notableStrings.back() = NotableStringInfo(str, info);

    // We're moving this string from a non-notable to a notable bucket, so
    // subtract it out of the non-notable tallies.
    zStats.stringInfo.subtract(info);
@@ -627,12 +575,10 @@ static bool FindNotableClasses(RealmStats& realmStats) {
      continue;
    }

    if (!realmStats.notableClasses.growBy(1)) {
    if (!realmStats.notableClasses.emplaceBack(className, info)) {
      return false;
    }

    realmStats.notableClasses.back() = NotableClassInfo(className, info);

    // We're moving this class from a non-notable to a notable bucket, so
    // subtract it out of the non-notable tallies.
    realmStats.classInfo.subtract(info);
@@ -659,13 +605,10 @@ static bool FindNotableScriptSources(JS::RuntimeSizes& runtime) {
      continue;
    }

    if (!runtime.notableScriptSources.growBy(1)) {
    if (!runtime.notableScriptSources.emplaceBack(filename, info)) {
      return false;
    }

    runtime.notableScriptSources.back() =
        NotableScriptSourceInfo(filename, info);

    // We're moving this script source from a non-notable to a notable
    // bucket, so subtract its sizes from the non-notable tallies.
    runtime.scriptSourceInfo.subtract(info);
+3 −3
Original line number Diff line number Diff line
@@ -1518,7 +1518,7 @@ static void ReportZoneStats(const JS::ZoneStats& zStats,
    // required for notable string detection is high.
    MOZ_ASSERT(!anonymize);

    nsDependentCString notableString(info.buffer);
    nsDependentCString notableString(info.buffer.get());

    // Viewing about:memory generates many notable strings which contain
    // "string(length=".  If we report these as notable, then we'll create
@@ -1803,7 +1803,7 @@ static void ReportRealmStats(const JS::RealmStats& realmStats,

    nsCString classPath =
        realmJSPathPrefix +
        nsPrintfCString("classes/class(%s)/", classInfo.className_);
        nsPrintfCString("classes/class(%s)/", classInfo.className_.get());

    ReportClassStats(classInfo, classPath, handleReport, data, gcTotal);
  }
@@ -2027,7 +2027,7 @@ void ReportJSRuntimeExplicitTreeStats(const JS::RuntimeStats& rtStats,
    if (anonymize) {
      escapedFilename.AppendPrintf("<anonymized-source-%d>", int(i));
    } else {
      nsDependentCString filename(scriptSourceInfo.filename_);
      nsDependentCString filename(scriptSourceInfo.filename_.get());
      escapedFilename.Append(filename);
      escapedFilename.ReplaceSubstring("/", "\\");
    }