summaryrefslogtreecommitdiffstats
path: root/mozglue/baseprofiler/core/ProfileJSONWriter.cpp
blob: dc51f6958e6f89adfb12b39bfb4010c8ab628250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/BaseProfileJSONWriter.h"

namespace mozilla::baseprofiler {

UniqueJSONStrings::UniqueJSONStrings(FailureLatch& aFailureLatch)
    : mStringTableWriter(aFailureLatch) {
  mStringTableWriter.StartBareList();
  if (const char* failure = mStringTableWriter.GetFailure(); failure) {
    ClearAndSetFailure(failure);
    return;
  }
}

UniqueJSONStrings::UniqueJSONStrings(FailureLatch& aFailureLatch,
                                     const UniqueJSONStrings& aOther,
                                     ProgressLogger aProgressLogger)
    : mStringTableWriter(aFailureLatch) {
  using namespace mozilla::literals::ProportionValue_literals;  // For `10_pc`.

  if (mStringTableWriter.Failed()) {
    return;
  }

  if (const char* failure = aOther.GetFailure(); failure) {
    ClearAndSetFailure(failure);
    return;
  }

  mStringTableWriter.StartBareList();
  uint32_t count = aOther.mStringHashToIndexMap.count();
  if (count != 0) {
    if (!mStringHashToIndexMap.reserve(count)) {
      ClearAndSetFailure("Cannot reserve UniqueJSONStrings map storage");
      return;
    }
    auto iter = aOther.mStringHashToIndexMap.iter();
    for (auto&& [unusedIndex, progressLogger] :
         aProgressLogger.CreateLoopSubLoggersFromTo(
             10_pc, 90_pc, count, "Copying unique strings...")) {
      (void)unusedIndex;
      if (iter.done()) {
        break;
      }
      mStringHashToIndexMap.putNewInfallible(iter.get().key(),
                                             iter.get().value());
      iter.next();
    }
    aProgressLogger.SetLocalProgress(90_pc, "Copied unique strings");
    mStringTableWriter.CopyAndSplice(
        aOther.mStringTableWriter.ChunkedWriteFunc());
    if (const char* failure = aOther.GetFailure(); failure) {
      ClearAndSetFailure(failure);
    }
    aProgressLogger.SetLocalProgress(100_pc, "Spliced unique strings");
  }
}

UniqueJSONStrings::~UniqueJSONStrings() = default;

void UniqueJSONStrings::SpliceStringTableElements(
    SpliceableJSONWriter& aWriter) {
  aWriter.TakeAndSplice(mStringTableWriter.TakeChunkedWriteFunc());
}

void UniqueJSONStrings::ClearAndSetFailure(std::string aFailure) {
  mStringTableWriter.SetFailure(std::move(aFailure));
  mStringHashToIndexMap.clear();
}

Maybe<uint32_t> UniqueJSONStrings::GetOrAddIndex(const Span<const char>& aStr) {
  if (Failed()) {
    return Nothing{};
  }

  uint32_t count = mStringHashToIndexMap.count();
  HashNumber hash = HashString(aStr.data(), aStr.size());
  auto entry = mStringHashToIndexMap.lookupForAdd(hash);
  if (entry) {
    MOZ_ASSERT(entry->value() < count);
    return Some(entry->value());
  }

  if (!mStringHashToIndexMap.add(entry, hash, count)) {
    ClearAndSetFailure("OOM in UniqueJSONStrings::GetOrAddIndex adding a map");
    return Nothing{};
  }
  mStringTableWriter.StringElement(aStr);
  if (const char* failure = mStringTableWriter.GetFailure(); failure) {
    ClearAndSetFailure(failure);
    return Nothing{};
  }
  return Some(count);
}

}  // namespace mozilla::baseprofiler