| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "mozilla/BaseProfileJSONWriter.h" |
| 8 |
|
| 9 |
namespace mozilla::baseprofiler { |
| 10 |
|
| 11 |
UniqueJSONStrings::UniqueJSONStrings(FailureLatch& aFailureLatch) |
| 12 |
: mStringTableWriter(aFailureLatch) { |
| 13 |
mStringTableWriter.StartBareList(); |
| 14 |
if (const char* failure = mStringTableWriter.GetFailure(); failure) { |
| 15 |
ClearAndSetFailure(failure); |
| 16 |
return; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
UniqueJSONStrings::UniqueJSONStrings(FailureLatch& aFailureLatch, |
| 21 |
const UniqueJSONStrings& aOther, |
| 22 |
ProgressLogger aProgressLogger) |
| 23 |
: mStringTableWriter(aFailureLatch) { |
| 24 |
using namespace mozilla::literals::ProportionValue_literals; // For `10_pc`. |
| 25 |
|
| 26 |
if (mStringTableWriter.Failed()) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
if (const char* failure = aOther.GetFailure(); failure) { |
| 31 |
ClearAndSetFailure(failure); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
mStringTableWriter.StartBareList(); |
| 36 |
uint32_t count = aOther.mStringHashToIndexMap.count(); |
| 37 |
if (count != 0) { |
| 38 |
if (!mStringHashToIndexMap.reserve(count)) { |
| 39 |
ClearAndSetFailure("Cannot reserve UniqueJSONStrings map storage"); |
| 40 |
return; |
| 41 |
} |
| 42 |
auto iter = aOther.mStringHashToIndexMap.iter(); |
| 43 |
for (auto&& [unusedIndex, progressLogger] : |
| 44 |
aProgressLogger.CreateLoopSubLoggersFromTo( |
| 45 |
10_pc, 90_pc, count, "Copying unique strings...")) { |
| 46 |
(void)unusedIndex; |
| 47 |
if (iter.done()) { |
| 48 |
break; |
| 49 |
} |
| 50 |
mStringHashToIndexMap.putNewInfallible(iter.get().key(), |
| 51 |
iter.get().value()); |
| 52 |
iter.next(); |
| 53 |
} |
| 54 |
aProgressLogger.SetLocalProgress(90_pc, "Copied unique strings"); |
| 55 |
mStringTableWriter.CopyAndSplice( |
| 56 |
aOther.mStringTableWriter.ChunkedWriteFunc()); |
| 57 |
if (const char* failure = aOther.GetFailure(); failure) { |
| 58 |
ClearAndSetFailure(failure); |
| 59 |
} |
| 60 |
aProgressLogger.SetLocalProgress(100_pc, "Spliced unique strings"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
UniqueJSONStrings::~UniqueJSONStrings() = default; |
| 65 |
|
| 66 |
void UniqueJSONStrings::SpliceStringTableElements( |
| 67 |
SpliceableJSONWriter& aWriter) { |
| 68 |
aWriter.TakeAndSplice(mStringTableWriter.TakeChunkedWriteFunc()); |
| 69 |
} |
| 70 |
|
| 71 |
void UniqueJSONStrings::ClearAndSetFailure(std::string aFailure) { |
| 72 |
mStringTableWriter.SetFailure(std::move(aFailure)); |
| 73 |
mStringHashToIndexMap.clear(); |
| 74 |
} |
| 75 |
|
| 76 |
Maybe<uint32_t> UniqueJSONStrings::GetOrAddIndex(const Span<const char>& aStr) { |
| 77 |
if (Failed()) { |
| 78 |
return Nothing{}; |
| 79 |
} |
| 80 |
|
| 81 |
uint32_t count = mStringHashToIndexMap.count(); |
| 82 |
HashNumber hash = HashString(aStr.data(), aStr.size()); |
| 83 |
auto entry = mStringHashToIndexMap.lookupForAdd(hash); |
| 84 |
if (entry) { |
| 85 |
MOZ_ASSERT(entry->value() < count); |
| 86 |
return Some(entry->value()); |
| 87 |
} |
| 88 |
|
| 89 |
if (!mStringHashToIndexMap.add(entry, hash, count)) { |
| 90 |
ClearAndSetFailure("OOM in UniqueJSONStrings::GetOrAddIndex adding a map"); |
| 91 |
return Nothing{}; |
| 92 |
} |
| 93 |
mStringTableWriter.StringElement(aStr); |
| 94 |
if (const char* failure = mStringTableWriter.GetFailure(); failure) { |
| 95 |
ClearAndSetFailure(failure); |
| 96 |
return Nothing{}; |
| 97 |
} |
| 98 |
return Some(count); |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace mozilla::baseprofiler |