Hoist responsibility for clearMarks/Measures to NativePerformanceObserver (#36312)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36312

## Changelog:

[Internal] -

`clearMarks` and `clearMeasures` methods are incidental to the `NativePerformance` TurboModule functionality, as in reality this responsibility belongs more on the `NativePerformanceObserver` and `PerformanceEntryReporter` side.

This is something that [the standard indirectly suggests](https://www.w3.org/TR/user-timing/#clearmarks-method) as well (referencing [performance entry buffer](https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer)).

The new implementation should be also a little bit more efficient, as it avoids calling the predicate for each entry.

Finally (and frankly, the main reason for this change, from my perspective), it will simplify mocking/testing the JS part of the PerfAPI code.

Reviewed By: rubennorte

Differential Revision: D43621174

fbshipit-source-id: c4217a0da1d8ecbce797240627f7b4f057d85b97
This commit is contained in:
Ruslan Shestopalyuk
2023-02-28 04:10:32 -08:00
committed by Facebook GitHub Bot
parent 5112bc5dbc
commit 14ab76ac30
10 changed files with 64 additions and 69 deletions
@@ -121,21 +121,27 @@ void PerformanceEntryReporter::mark(
std::nullopt});
}
void PerformanceEntryReporter::clearMarks(
const std::optional<std::string> &markName) {
if (markName) {
PerformanceMark mark{{*markName, 0}};
void PerformanceEntryReporter::clearEntries(
PerformanceEntryType entryType,
const char *entryName) {
if (entryName != nullptr && entryType == PerformanceEntryType::MARK) {
// remove a named mark from the mark/measure registry
PerformanceMark mark{{entryName, 0}};
marksRegistry_.erase(&mark);
clearEntries([&markName](const RawPerformanceEntry &entry) {
return entry.entryType == static_cast<int>(PerformanceEntryType::MARK) &&
entry.name == markName;
});
} else {
marksRegistry_.clear();
clearEntries([](const RawPerformanceEntry &entry) {
return entry.entryType == static_cast<int>(PerformanceEntryType::MARK);
});
}
int lastPos = entries_.size() - 1;
int pos = lastPos;
while (pos >= 0) {
const RawPerformanceEntry &entry = entries_[pos];
if (entry.entryType == static_cast<int32_t>(entryType) &&
(entryName == nullptr || entry.name == entryName)) {
entries_[pos] = entries_[lastPos];
lastPos--;
}
pos--;
}
entries_.resize(lastPos + 1);
}
void PerformanceEntryReporter::measure(
@@ -158,22 +164,6 @@ void PerformanceEntryReporter::measure(
std::nullopt});
}
void PerformanceEntryReporter::clearMeasures(
const std::optional<std::string> &measureName) {
if (measureName) {
clearEntries([&measureName](const RawPerformanceEntry &entry) {
return entry.entryType ==
static_cast<int>(PerformanceEntryType::MEASURE) &&
entry.name == measureName;
});
} else {
marksRegistry_.clear();
clearEntries([](const RawPerformanceEntry &entry) {
return entry.entryType == static_cast<int>(PerformanceEntryType::MEASURE);
});
}
}
double PerformanceEntryReporter::getMarkTime(
const std::string &markName) const {
PerformanceMark mark{{std::move(markName), 0}};
@@ -202,20 +192,6 @@ void PerformanceEntryReporter::event(
interactionId});
}
void PerformanceEntryReporter::clearEntries(
std::function<bool(const RawPerformanceEntry &)> predicate) {
int lastPos = entries_.size() - 1;
int pos = lastPos;
while (pos >= 0) {
if (predicate(entries_[pos])) {
entries_[pos] = entries_[lastPos];
lastPos--;
}
pos--;
}
entries_.resize(lastPos + 1);
}
void PerformanceEntryReporter::scheduleFlushBuffer() {
if (callback_) {
callback_->callWithPriority(SchedulerPriority::IdlePriority);