Skip to content

Commit 90e944d

Browse files
authored
Merge pull request microsoft#41253 from microsoft/nativePerformanceHooks2
Fix dependency order and observer registration
2 parents 3517af8 + 0847d85 commit 90e944d

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/compiler/performance.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace ts.performance {
44
let perfHooks: PerformanceHooks | undefined;
55
let perfObserver: PerformanceObserver | undefined;
6-
let perfEntryList: PerformanceObserverEntryList | undefined;
76
// when set, indicates the implementation of `Performance` to use for user timing.
87
// when unset, indicates user timing is unavailable or disabled.
98
let performanceImpl: Performance | undefined;
@@ -42,6 +41,8 @@ namespace ts.performance {
4241
}
4342

4443
export const nullTimer: Timer = { enter: noop, exit: noop };
44+
const counts = new Map<string, number>();
45+
const durations = new Map<string, number>();
4546

4647
/**
4748
* Marks a performance event.
@@ -71,7 +72,7 @@ namespace ts.performance {
7172
* @param markName The name of the mark.
7273
*/
7374
export function getCount(markName: string) {
74-
return perfEntryList?.getEntriesByName(markName, "mark").length || 0;
75+
return counts.get(markName) || 0;
7576
}
7677

7778
/**
@@ -80,7 +81,7 @@ namespace ts.performance {
8081
* @param measureName The name of the measure whose durations should be accumulated.
8182
*/
8283
export function getDuration(measureName: string) {
83-
return perfEntryList?.getEntriesByName(measureName, "measure").reduce((a, entry) => a + entry.duration, 0) || 0;
84+
return durations.get(measureName) || 0;
8485
}
8586

8687
/**
@@ -89,7 +90,7 @@ namespace ts.performance {
8990
* @param cb The action to perform for each measure
9091
*/
9192
export function forEachMeasure(cb: (measureName: string, duration: number) => void) {
92-
perfEntryList?.getEntriesByType("measure").forEach(({ name, duration }) => { cb(name, duration); });
93+
durations.forEach((duration, measureName) => cb(measureName, duration));
9394
}
9495

9596
/**
@@ -104,7 +105,7 @@ namespace ts.performance {
104105
if (!performanceImpl) {
105106
perfHooks ||= tryGetNativePerformanceHooks();
106107
if (!perfHooks) return false;
107-
perfObserver ||= new perfHooks.PerformanceObserver(list => perfEntryList = list);
108+
perfObserver ||= new perfHooks.PerformanceObserver(updateStatisticsFromList);
108109
perfObserver.observe({ entryTypes: ["mark", "measure"] });
109110
performanceImpl = perfHooks.performance;
110111
}
@@ -115,5 +116,16 @@ namespace ts.performance {
115116
export function disable() {
116117
perfObserver?.disconnect();
117118
performanceImpl = undefined;
119+
counts.clear();
120+
durations.clear();
121+
}
122+
123+
function updateStatisticsFromList(list: PerformanceObserverEntryList) {
124+
for (const mark of list.getEntriesByType("mark")) {
125+
counts.set(mark.name, (counts.get(mark.name) || 0) + 1);
126+
}
127+
for (const measure of list.getEntriesByType("measure")) {
128+
durations.set(measure.name, (durations.get(measure.name) || 0) + measure.duration);
129+
}
118130
}
119131
}

src/compiler/performanceCore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace ts {
7171
// optional `start` and `end` arguments for `performance.measure`.
7272
// See https://github.com/nodejs/node/pull/32651 for more information.
7373
const version = new Version(process.versions.node);
74-
const range = new VersionRange("<12 || 13 <13.13");
74+
const range = new VersionRange("<12.16.3 || 13 <13.13");
7575
if (range.test(version)) {
7676
return {
7777
performance: {
@@ -84,7 +84,7 @@ namespace ts {
8484
performance.mark(end);
8585
}
8686
performance.measure(name, start, end);
87-
if (end = "__performance.measure-fix__") {
87+
if (end === "__performance.measure-fix__") {
8888
performance.clearMarks("__performance.measure-fix__");
8989
}
9090
}

src/compiler/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"corePublic.ts",
1414
"core.ts",
1515
"debug.ts",
16+
"semver.ts",
1617
"performanceCore.ts",
1718
"performance.ts",
1819
"perfLogger.ts",
19-
"semver.ts",
2020
"tracing.ts",
2121

2222
"types.ts",

0 commit comments

Comments
 (0)