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

Fix dependency order and observer registration #41253

Merged
merged 1 commit into from
Oct 26, 2020
Merged
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
22 changes: 17 additions & 5 deletions src/compiler/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ts.performance {
let perfHooks: PerformanceHooks | undefined;
let perfObserver: PerformanceObserver | undefined;
let perfEntryList: PerformanceObserverEntryList | undefined;
// when set, indicates the implementation of `Performance` to use for user timing.
// when unset, indicates user timing is unavailable or disabled.
let performanceImpl: Performance | undefined;
Expand Down Expand Up @@ -42,6 +41,8 @@ namespace ts.performance {
}

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

/**
* Marks a performance event.
Expand Down Expand Up @@ -71,7 +72,7 @@ namespace ts.performance {
* @param markName The name of the mark.
*/
export function getCount(markName: string) {
return perfEntryList?.getEntriesByName(markName, "mark").length || 0;
return counts.get(markName) || 0;
}

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

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

/**
Expand All @@ -104,7 +105,7 @@ namespace ts.performance {
if (!performanceImpl) {
perfHooks ||= tryGetNativePerformanceHooks();
if (!perfHooks) return false;
perfObserver ||= new perfHooks.PerformanceObserver(list => perfEntryList = list);
perfObserver ||= new perfHooks.PerformanceObserver(updateStatisticsFromList);
perfObserver.observe({ entryTypes: ["mark", "measure"] });
performanceImpl = perfHooks.performance;
}
Expand All @@ -115,5 +116,16 @@ namespace ts.performance {
export function disable() {
perfObserver?.disconnect();
performanceImpl = undefined;
counts.clear();
durations.clear();
}

function updateStatisticsFromList(list: PerformanceObserverEntryList) {
for (const mark of list.getEntriesByType("mark")) {
counts.set(mark.name, (counts.get(mark.name) || 0) + 1);
}
for (const measure of list.getEntriesByType("measure")) {
durations.set(measure.name, (durations.get(measure.name) || 0) + measure.duration);
}
}
}
4 changes: 2 additions & 2 deletions src/compiler/performanceCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace ts {
// optional `start` and `end` arguments for `performance.measure`.
// See https://github.com/nodejs/node/pull/32651 for more information.
const version = new Version(process.versions.node);
const range = new VersionRange("<12 || 13 <13.13");
const range = new VersionRange("<12.16.3 || 13 <13.13");
if (range.test(version)) {
return {
performance: {
Expand All @@ -84,7 +84,7 @@ namespace ts {
performance.mark(end);
}
performance.measure(name, start, end);
if (end = "__performance.measure-fix__") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... Should we have a lint for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a --strictBoolean (or --pedanticBoolean?) flag that requires all conditions be boolean (including !). It wouldn't catch if (f = true), but it also would mean you'd have to write a lot of if (obj !== null && obj !== undefined) instead of if (obj) because !!obj would also be a violation...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while (next = read()) seems like a sane pattern, so this feels more like a heuristic than a compiler error. Having said that, I would probably personally use such a mode, assuming it had a (totally reasonable) exception for !!expr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, mostly just using = in an if without a reference to the assigned variable within the if. (Since I know we do this intentionally in relationship checking, but the difference there is we actually reference the checked variable)

if (end === "__performance.measure-fix__") {
performance.clearMarks("__performance.measure-fix__");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"corePublic.ts",
"core.ts",
"debug.ts",
"semver.ts",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a comment so it doesn't get alphabetized back?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should never be alphabetized, they are all order dependent.

"performanceCore.ts",
"performance.ts",
"perfLogger.ts",
"semver.ts",
"tracing.ts",

"types.ts",
Expand Down