Optimize locks in PerformanceEntryReporter (#46698)

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

Changelog: [internal]

Small optimization to use readers/writers for locking in `PerformanceEntryReporter`.

Reviewed By: rshest

Differential Revision: D63540236

fbshipit-source-id: 80b3fd313453ad8da4f0af2deaab61a86064b4b7
This commit is contained in:
Rubén Norte
2024-10-02 03:13:24 -07:00
committed by Facebook GitHub Bot
parent 559c6029fd
commit 434decb409
2 changed files with 20 additions and 21 deletions
@@ -51,7 +51,7 @@ PerformanceEntryReporter::getSupportedEntryTypes() {
uint32_t PerformanceEntryReporter::getDroppedEntriesCount(
PerformanceEntryType entryType) const noexcept {
std::lock_guard lock(buffersMutex_);
std::shared_lock lock(buffersMutex_);
return getBuffer(entryType).droppedEntriesCount;
}
@@ -64,7 +64,7 @@ std::vector<PerformanceEntry> PerformanceEntryReporter::getEntries() const {
void PerformanceEntryReporter::getEntries(
std::vector<PerformanceEntry>& dest) const {
std::lock_guard lock(buffersMutex_);
std::shared_lock lock(buffersMutex_);
for (auto entryType : getSupportedEntryTypes()) {
getBuffer(entryType).getEntries(dest);
@@ -81,7 +81,7 @@ std::vector<PerformanceEntry> PerformanceEntryReporter::getEntries(
void PerformanceEntryReporter::getEntries(
std::vector<PerformanceEntry>& dest,
PerformanceEntryType entryType) const {
std::lock_guard lock(buffersMutex_);
std::shared_lock lock(buffersMutex_);
getBuffer(entryType).getEntries(dest);
}
@@ -98,13 +98,13 @@ void PerformanceEntryReporter::getEntries(
std::vector<PerformanceEntry>& dest,
PerformanceEntryType entryType,
const std::string& entryName) const {
std::lock_guard lock(buffersMutex_);
std::shared_lock lock(buffersMutex_);
getBuffer(entryType).getEntries(dest, entryName);
}
void PerformanceEntryReporter::clearEntries() {
std::lock_guard lock(buffersMutex_);
std::unique_lock lock(buffersMutex_);
for (auto entryType : getSupportedEntryTypes()) {
getBufferRef(entryType).clear();
@@ -112,7 +112,7 @@ void PerformanceEntryReporter::clearEntries() {
}
void PerformanceEntryReporter::clearEntries(PerformanceEntryType entryType) {
std::lock_guard lock(buffersMutex_);
std::unique_lock lock(buffersMutex_);
getBufferRef(entryType).clear();
}
@@ -120,7 +120,7 @@ void PerformanceEntryReporter::clearEntries(PerformanceEntryType entryType) {
void PerformanceEntryReporter::clearEntries(
PerformanceEntryType entryType,
const std::string& entryName) {
std::lock_guard lock(buffersMutex_);
std::unique_lock lock(buffersMutex_);
getBufferRef(entryType).clear(entryName);
}
@@ -134,7 +134,7 @@ void PerformanceEntryReporter::reportMark(
.startTime = startTime ? *startTime : getCurrentTimeStamp()};
{
std::lock_guard lock(buffersMutex_);
std::unique_lock lock(buffersMutex_);
markBuffer_.add(entry);
}
@@ -168,7 +168,7 @@ void PerformanceEntryReporter::reportMeasure(
.duration = durationVal};
{
std::lock_guard lock(buffersMutex_);
std::unique_lock lock(buffersMutex_);
measureBuffer_.add(entry);
}
@@ -177,7 +177,7 @@ void PerformanceEntryReporter::reportMeasure(
DOMHighResTimeStamp PerformanceEntryReporter::getMarkTime(
const std::string& markName) const {
std::lock_guard lock(buffersMutex_);
std::shared_lock lock(buffersMutex_);
if (auto it = markBuffer_.find(markName); it) {
return it->startTime;
@@ -195,6 +195,12 @@ void PerformanceEntryReporter::reportEvent(
uint32_t interactionId) {
eventCounts_[name]++;
if (duration < eventBuffer_.durationThreshold) {
// The entries duration is lower than the desired reporting threshold,
// skip
return;
}
const auto entry = PerformanceEntry{
.name = std::move(name),
.entryType = PerformanceEntryType::EVENT,
@@ -205,14 +211,7 @@ void PerformanceEntryReporter::reportEvent(
.interactionId = interactionId};
{
std::lock_guard lock(buffersMutex_);
if (entry.duration < eventBuffer_.durationThreshold) {
// The entries duration is lower than the desired reporting threshold,
// skip
return;
}
std::unique_lock lock(buffersMutex_);
eventBuffer_.add(entry);
}
@@ -229,7 +228,7 @@ void PerformanceEntryReporter::reportLongTask(
.duration = duration};
{
std::lock_guard lock(buffersMutex_);
std::unique_lock lock(buffersMutex_);
longTaskBuffer_.add(entry);
}
@@ -9,8 +9,8 @@
#include <react/timing/primitives.h>
#include <memory>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <vector>
#include "PerformanceEntryCircularBuffer.h"
#include "PerformanceEntryKeyedBuffer.h"
@@ -99,7 +99,7 @@ class PerformanceEntryReporter {
private:
std::unique_ptr<PerformanceObserverRegistry> observerRegistry_;
mutable std::mutex buffersMutex_;
mutable std::shared_mutex buffersMutex_;
PerformanceEntryCircularBuffer eventBuffer_{EVENT_BUFFER_SIZE};
PerformanceEntryCircularBuffer longTaskBuffer_{LONG_TASK_BUFFER_SIZE};
PerformanceEntryKeyedBuffer markBuffer_;