Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stable sort for performance entries #36998

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ NativePerformance::NativePerformance(std::shared_ptr<CallInvoker> jsInvoker)
void NativePerformance::mark(
jsi::Runtime &rt,
std::string name,
double startTime,
double duration) {
PerformanceEntryReporter::getInstance().mark(name, startTime, duration);
double startTime) {
PerformanceEntryReporter::getInstance().mark(name, startTime);
}

void NativePerformance::measure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class NativePerformance : public NativePerformanceCxxSpec<NativePerformance>,
public:
NativePerformance(std::shared_ptr<CallInvoker> jsInvoker);

void
mark(jsi::Runtime &rt, std::string name, double startTime, double duration);
void mark(jsi::Runtime &rt, std::string name, double startTime);

void measure(
jsi::Runtime &rt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type ReactNativeStartupTiming = {|
|};

export interface Spec extends TurboModule {
+mark: (name: string, startTime: number, duration: number) => void;
+mark: (name: string, startTime: number) => void;
+measure: (
name: string,
startTime: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default class Performance {
const mark = new PerformanceMark(markName, markOptions);

if (NativePerformance?.mark) {
NativePerformance.mark(markName, mark.startTime, mark.duration);
NativePerformance.mark(markName, mark.startTime);
} else {
warnNoNativePerformance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
namespace facebook::react {
EventTag PerformanceEntryReporter::sCurrentEventTag_{0};

static inline double getCurrentTimeStamp() {
return JSExecutor::performanceNow();
}

PerformanceEntryReporter &PerformanceEntryReporter::getInstance() {
static PerformanceEntryReporter instance;
return instance;
Expand Down Expand Up @@ -68,7 +72,7 @@ GetPendingEntriesResult PerformanceEntryReporter::popPendingEntries() {
}

// Sort by starting time (or ending time, if starting times are equal)
std::sort(
std::stable_sort(
res.entries.begin(),
res.entries.end(),
[](const RawPerformanceEntry &lhs, const RawPerformanceEntry &rhs) {
Expand Down Expand Up @@ -134,13 +138,12 @@ void PerformanceEntryReporter::logEntry(const RawPerformanceEntry &entry) {

void PerformanceEntryReporter::mark(
const std::string &name,
double startTime,
double duration) {
const std::optional<double> &startTime) {
logEntry(RawPerformanceEntry{
name,
static_cast<int>(PerformanceEntryType::MARK),
startTime,
duration,
startTime ? *startTime : getCurrentTimeStamp(),
0.0,
std::nullopt,
std::nullopt,
std::nullopt});
Expand Down Expand Up @@ -352,7 +355,7 @@ EventTag PerformanceEntryReporter::onEventStart(const char *name) {
sCurrentEventTag_ = 1;
}

auto timeStamp = JSExecutor::performanceNow();
auto timeStamp = getCurrentTimeStamp();
{
std::lock_guard<std::mutex> lock(eventsInFlightMutex_);
eventsInFlight_.emplace(std::make_pair(
Expand All @@ -365,7 +368,7 @@ void PerformanceEntryReporter::onEventDispatch(EventTag tag) {
if (!isReporting(PerformanceEntryType::EVENT) || tag == 0) {
return;
}
auto timeStamp = JSExecutor::performanceNow();
auto timeStamp = getCurrentTimeStamp();
{
std::lock_guard<std::mutex> lock(eventsInFlightMutex_);
auto it = eventsInFlight_.find(tag);
Expand All @@ -379,7 +382,7 @@ void PerformanceEntryReporter::onEventEnd(EventTag tag) {
if (!isReporting(PerformanceEntryType::EVENT) || tag == 0) {
return;
}
auto timeStamp = JSExecutor::performanceNow();
auto timeStamp = getCurrentTimeStamp();
{
std::lock_guard<std::mutex> lock(eventsInFlightMutex_);
auto it = eventsInFlight_.find(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ class PerformanceEntryReporter : public EventLogger {
return droppedEntryCount_;
}

void mark(const std::string &name, double startTime, double duration);
void mark(
const std::string &name,
const std::optional<double> &startTime = std::nullopt);

void measure(
const std::string &name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import {RawPerformanceEntryTypeValues} from '../RawPerformanceEntry';
const marks: Map<string, number> = new Map();

const NativePerformanceMock: NativePerformance = {
mark: (name: string, startTime: number, duration: number): void => {
mark: (name: string, startTime: number): void => {
NativePerformanceObserver?.logRawEntry({
name,
entryType: RawPerformanceEntryTypeValues.MARK,
startTime,
duration,
duration: 0,
});
marks.set(name, startTime);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestStopReporting) {

reporter.startReporting(PerformanceEntryType::MARK);

reporter.mark("mark0", 0.0, 0.0);
reporter.mark("mark1", 0.0, 0.0);
reporter.mark("mark2", 0.0, 0.0);
reporter.mark("mark0", 0.0);
reporter.mark("mark1", 0.0);
reporter.mark("mark2", 0.0);
reporter.measure("measure0", 0.0, 0.0);

auto res = reporter.popPendingEntries();
Expand All @@ -73,7 +73,7 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestStopReporting) {
reporter.stopReporting(PerformanceEntryType::MARK);
reporter.startReporting(PerformanceEntryType::MEASURE);

reporter.mark("mark3", 0.0, 0.0);
reporter.mark("mark3");
reporter.measure("measure1", 0.0, 0.0);

res = reporter.popPendingEntries();
Expand All @@ -91,9 +91,9 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMarks) {

reporter.startReporting(PerformanceEntryType::MARK);

reporter.mark("mark0", 0.0, 1.0);
reporter.mark("mark1", 1.0, 3.0);
reporter.mark("mark2", 2.0, 4.0);
reporter.mark("mark0", 0.0);
reporter.mark("mark1", 1.0);
reporter.mark("mark2", 2.0);

auto res = reporter.popPendingEntries();
const auto &entries = res.entries;
Expand All @@ -105,21 +105,21 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMarks) {
{"mark0",
static_cast<int>(PerformanceEntryType::MARK),
0.0,
1.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
{"mark1",
static_cast<int>(PerformanceEntryType::MARK),
1.0,
3.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
{"mark2",
static_cast<int>(PerformanceEntryType::MARK),
2.0,
4.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt}};
Expand All @@ -136,16 +136,20 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMeasures) {
reporter.startReporting(PerformanceEntryType::MARK);
reporter.startReporting(PerformanceEntryType::MEASURE);

reporter.mark("mark0", 0.0, 1.0);
reporter.mark("mark1", 1.0, 3.0);
reporter.mark("mark2", 2.0, 4.0);
reporter.mark("mark0", 0.0);
reporter.mark("mark1", 1.0);
reporter.mark("mark2", 2.0);

reporter.measure("measure0", 0.0, 2.0);
reporter.measure("measure1", 0.0, 2.0, 4.0);
reporter.measure("measure2", 0.0, 0.0, std::nullopt, "mark1", "mark2");
reporter.measure("measure3", 0.0, 0.0, 5.0, "mark1");
reporter.measure("measure4", 1.5, 0.0, std::nullopt, std::nullopt, "mark2");

reporter.mark("mark3", 2.0);
reporter.measure("measure5", 2.0, 2.0);
reporter.mark("mark4", 2.0);

auto res = reporter.popPendingEntries();
const auto &entries = res.entries;

Expand All @@ -155,7 +159,7 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMeasures) {
{"mark0",
static_cast<int>(PerformanceEntryType::MARK),
0.0,
1.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
Expand All @@ -173,17 +177,17 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMeasures) {
std::nullopt,
std::nullopt,
std::nullopt},
{"measure2",
static_cast<int>(PerformanceEntryType::MEASURE),
1.0,
{"mark1",
static_cast<int>(PerformanceEntryType::MARK),
1.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
{"mark1",
static_cast<int>(PerformanceEntryType::MARK),
{"measure2",
static_cast<int>(PerformanceEntryType::MEASURE),
1.0,
1.0,
3.0,
std::nullopt,
std::nullopt,
std::nullopt},
Expand All @@ -204,10 +208,32 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestReportMeasures) {
{"mark2",
static_cast<int>(PerformanceEntryType::MARK),
2.0,
4.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt}};
std::nullopt},
{"mark3",
static_cast<int>(PerformanceEntryType::MARK),
2.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
{"mark4",
static_cast<int>(PerformanceEntryType::MARK),
2.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
{"measure5",
static_cast<int>(PerformanceEntryType::MEASURE),
2.0,
0.0,
std::nullopt,
std::nullopt,
std::nullopt},
};

ASSERT_EQ(expected, entries);
}
Expand Down Expand Up @@ -249,9 +275,9 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestGetEntries) {
reporter.startReporting(PerformanceEntryType::MARK);
reporter.startReporting(PerformanceEntryType::MEASURE);

reporter.mark("common_name", 0.0, 1.0);
reporter.mark("mark1", 1.0, 3.0);
reporter.mark("mark2", 2.0, 4.0);
reporter.mark("common_name", 0.0);
reporter.mark("mark1", 1.0);
reporter.mark("mark2", 2.0);

reporter.measure("common_name", 0.0, 2.0);
reporter.measure("measure1", 0.0, 2.0, 4.0);
Expand Down Expand Up @@ -296,9 +322,9 @@ TEST(PerformanceEntryReporter, PerformanceEntryReporterTestClearEntries) {
reporter.startReporting(PerformanceEntryType::MARK);
reporter.startReporting(PerformanceEntryType::MEASURE);

reporter.mark("common_name", 0.0, 1.0);
reporter.mark("mark1", 1.0, 3.0);
reporter.mark("mark2", 2.0, 4.0);
reporter.mark("common_name", 0.0);
reporter.mark("mark1", 1.0);
reporter.mark("mark2", 2.0);

reporter.measure("common_name", 0.0, 2.0);
reporter.measure("measure1", 0.0, 2.0, 4.0);
Expand Down