Skip to content

Commit 75cb595

Browse files
authored
Merge pull request microsoft#10327 from Microsoft/simplePerformanceAPI
Simplifies performance API
2 parents 2d1b68f + 4e04b75 commit 75cb595

File tree

6 files changed

+77
-82
lines changed

6 files changed

+77
-82
lines changed

src/compiler/binder.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ namespace ts {
8989
const binder = createBinder();
9090

9191
export function bindSourceFile(file: SourceFile, options: CompilerOptions) {
92-
const start = performance.mark();
92+
performance.mark("beforeBind");
9393
binder(file, options);
94-
performance.measure("Bind", start);
94+
performance.mark("afterBind");
95+
performance.measure("Bind", "beforeBind", "afterBind");
9596
}
9697

9798
function createBinder(): (file: SourceFile, options: CompilerOptions) => void {

src/compiler/checker.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -17542,11 +17542,10 @@ namespace ts {
1754217542
}
1754317543

1754417544
function checkSourceFile(node: SourceFile) {
17545-
const start = performance.mark();
17546-
17545+
performance.mark("beforeCheck");
1754717546
checkSourceFileWorker(node);
17548-
17549-
performance.measure("Check", start);
17547+
performance.mark("afterCheck");
17548+
performance.measure("Check", "beforeCheck", "afterCheck");
1755017549
}
1755117550

1755217551
// Fully type check a source file and collect the relevant diagnostics.

src/compiler/parser.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ namespace ts {
421421
}
422422

423423
export function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes = false, scriptKind?: ScriptKind): SourceFile {
424-
const start = performance.mark();
424+
performance.mark("beforeParse");
425425
const result = Parser.parseSourceFile(fileName, sourceText, languageVersion, /*syntaxCursor*/ undefined, setParentNodes, scriptKind);
426-
427-
performance.measure("Parse", start);
426+
performance.mark("afterParse");
427+
performance.measure("Parse", "beforeParse", "afterParse");
428428
return result;
429429
}
430430

src/compiler/performance.ts

+48-62
Original file line numberDiff line numberDiff line change
@@ -6,104 +6,90 @@ namespace ts {
66
}
77

88
/*@internal*/
9+
/** Performance measurements for the compiler. */
910
namespace ts.performance {
10-
/** Performance measurements for the compiler. */
1111
declare const onProfilerEvent: { (markName: string): void; profiler: boolean; };
12-
let profilerEvent: (markName: string) => void;
13-
let counters: MapLike<number>;
14-
let measures: MapLike<number>;
12+
13+
const profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true
14+
? onProfilerEvent
15+
: (markName: string) => { };
16+
17+
let enabled = false;
18+
let profilerStart = 0;
19+
let counts: Map<number>;
20+
let marks: Map<number>;
21+
let measures: Map<number>;
1522

1623
/**
17-
* Emit a performance event if ts-profiler is connected. This is primarily used
18-
* to generate heap snapshots.
24+
* Marks a performance event.
1925
*
20-
* @param eventName A name for the event.
26+
* @param markName The name of the mark.
2127
*/
22-
export function emit(eventName: string) {
23-
if (profilerEvent) {
24-
profilerEvent(eventName);
28+
export function mark(markName: string) {
29+
if (enabled) {
30+
marks[markName] = timestamp();
31+
counts[markName] = (counts[markName] || 0) + 1;
32+
profilerEvent(markName);
2533
}
2634
}
2735

2836
/**
29-
* Increments a counter with the specified name.
37+
* Adds a performance measurement with the specified name.
3038
*
31-
* @param counterName The name of the counter.
39+
* @param measureName The name of the performance measurement.
40+
* @param startMarkName The name of the starting mark. If not supplied, the point at which the
41+
* profiler was enabled is used.
42+
* @param endMarkName The name of the ending mark. If not supplied, the current timestamp is
43+
* used.
3244
*/
33-
export function increment(counterName: string) {
34-
if (counters) {
35-
counters[counterName] = (getProperty(counters, counterName) || 0) + 1;
45+
export function measure(measureName: string, startMarkName?: string, endMarkName?: string) {
46+
if (enabled) {
47+
const end = endMarkName && marks[endMarkName] || timestamp();
48+
const start = startMarkName && marks[startMarkName] || profilerStart;
49+
measures[measureName] = (measures[measureName] || 0) + (end - start);
3650
}
3751
}
3852

3953
/**
40-
* Gets the value of the counter with the specified name.
54+
* Gets the number of times a marker was encountered.
4155
*
42-
* @param counterName The name of the counter.
43-
*/
44-
export function getCount(counterName: string) {
45-
return counters && getProperty(counters, counterName) || 0;
46-
}
47-
48-
/**
49-
* Marks the start of a performance measurement.
56+
* @param markName The name of the mark.
5057
*/
51-
export function mark() {
52-
return measures ? timestamp() : 0;
58+
export function getCount(markName: string) {
59+
return counts && counts[markName] || 0;
5360
}
5461

5562
/**
56-
* Adds a performance measurement with the specified name.
63+
* Gets the total duration of all measurements with the supplied name.
5764
*
58-
* @param measureName The name of the performance measurement.
59-
* @param marker The timestamp of the starting mark.
65+
* @param measureName The name of the measure whose durations should be accumulated.
6066
*/
61-
export function measure(measureName: string, marker: number) {
62-
if (measures) {
63-
measures[measureName] = (getProperty(measures, measureName) || 0) + (timestamp() - marker);
64-
}
67+
export function getDuration(measureName: string) {
68+
return measures && measures[measureName] || 0;
6569
}
6670

6771
/**
6872
* Iterate over each measure, performing some action
69-
*
73+
*
7074
* @param cb The action to perform for each measure
7175
*/
7276
export function forEachMeasure(cb: (measureName: string, duration: number) => void) {
73-
return forEachKey(measures, key => cb(key, measures[key]));
74-
}
75-
76-
/**
77-
* Gets the total duration of all measurements with the supplied name.
78-
*
79-
* @param measureName The name of the measure whose durations should be accumulated.
80-
*/
81-
export function getDuration(measureName: string) {
82-
return measures && getProperty(measures, measureName) || 0;
77+
for (const key in measures) {
78+
cb(key, measures[key]);
79+
}
8380
}
8481

8582
/** Enables (and resets) performance measurements for the compiler. */
8683
export function enable() {
87-
counters = { };
88-
measures = {
89-
"I/O Read": 0,
90-
"I/O Write": 0,
91-
"Program": 0,
92-
"Parse": 0,
93-
"Bind": 0,
94-
"Check": 0,
95-
"Emit": 0,
96-
};
97-
98-
profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true
99-
? onProfilerEvent
100-
: undefined;
84+
counts = createMap<number>();
85+
marks = createMap<number>();
86+
measures = createMap<number>();
87+
enabled = true;
88+
profilerStart = timestamp();
10189
}
10290

103-
/** Disables (and clears) performance measurements for the compiler. */
91+
/** Disables performance measurements for the compiler. */
10492
export function disable() {
105-
counters = undefined;
106-
measures = undefined;
107-
profilerEvent = undefined;
93+
enabled = false;
10894
}
10995
}

src/compiler/program.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -857,9 +857,10 @@ namespace ts {
857857
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile {
858858
let text: string;
859859
try {
860-
const start = performance.mark();
860+
performance.mark("beforeIORead");
861861
text = sys.readFile(fileName, options.charset);
862-
performance.measure("I/O Read", start);
862+
performance.mark("afterIORead");
863+
performance.measure("I/O Read", "beforeIORead", "afterIORead");
863864
}
864865
catch (e) {
865866
if (onError) {
@@ -926,7 +927,7 @@ namespace ts {
926927

927928
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
928929
try {
929-
const start = performance.mark();
930+
performance.mark("beforeIOWrite");
930931
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
931932

932933
if (isWatchSet(options) && sys.createHash && sys.getModifiedTime) {
@@ -936,7 +937,8 @@ namespace ts {
936937
sys.writeFile(fileName, data, writeByteOrderMark);
937938
}
938939

939-
performance.measure("I/O Write", start);
940+
performance.mark("afterIOWrite");
941+
performance.measure("I/O Write", "beforeIOWrite", "afterIOWrite");
940942
}
941943
catch (e) {
942944
if (onError) {
@@ -1118,7 +1120,7 @@ namespace ts {
11181120
// Track source files that are source files found by searching under node_modules, as these shouldn't be compiled.
11191121
const sourceFilesFoundSearchingNodeModules = createMap<boolean>();
11201122

1121-
const start = performance.mark();
1123+
performance.mark("beforeProgram");
11221124

11231125
host = host || createCompilerHost(options);
11241126

@@ -1216,8 +1218,8 @@ namespace ts {
12161218
};
12171219

12181220
verifyCompilerOptions();
1219-
1220-
performance.measure("Program", start);
1221+
performance.mark("afterProgram");
1222+
performance.measure("Program", "beforeProgram", "afterProgram");
12211223

12221224
return program;
12231225

@@ -1460,14 +1462,15 @@ namespace ts {
14601462
// checked is to not pass the file to getEmitResolver.
14611463
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile);
14621464

1463-
const start = performance.mark();
1465+
performance.mark("beforeEmit");
14641466

14651467
const emitResult = emitFiles(
14661468
emitResolver,
14671469
getEmitHost(writeFileCallback),
14681470
sourceFile);
14691471

1470-
performance.measure("Emit", start);
1472+
performance.mark("afterEmit");
1473+
performance.measure("Emit", "beforeEmit", "afterEmit");
14711474
return emitResult;
14721475
}
14731476

src/compiler/sourcemap.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace ts {
4646

4747
export function createSourceMapWriter(host: EmitHost, writer: EmitTextWriter): SourceMapWriter {
4848
const compilerOptions = host.getCompilerOptions();
49+
const extendedDiagnostics = compilerOptions.extendedDiagnostics;
4950
let currentSourceFile: SourceFile;
5051
let sourceMapDir: string; // The directory in which sourcemap will be
5152
let stopOverridingSpan = false;
@@ -240,7 +241,9 @@ namespace ts {
240241
return;
241242
}
242243

243-
const start = performance.mark();
244+
if (extendedDiagnostics) {
245+
performance.mark("beforeSourcemap");
246+
}
244247

245248
const sourceLinePos = getLineAndCharacterOfPosition(currentSourceFile, pos);
246249

@@ -282,7 +285,10 @@ namespace ts {
282285

283286
updateLastEncodedAndRecordedSpans();
284287

285-
performance.measure("Source Map", start);
288+
if (extendedDiagnostics) {
289+
performance.mark("afterSourcemap");
290+
performance.measure("Source Map", "beforeSourcemap", "afterSourcemap");
291+
}
286292
}
287293

288294
function getStartPos(range: TextRange) {

0 commit comments

Comments
 (0)