Skip to content

Commit 3c6ceaf

Browse files
committed
Simplified performance timers
1 parent 3bf4f2a commit 3c6ceaf

8 files changed

Lines changed: 63 additions & 172 deletions

File tree

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ namespace ts {
9494
const binder = createBinder();
9595

9696
export function bindSourceFile(file: SourceFile, options: CompilerOptions) {
97-
performance.mark("bindStart");
97+
const bindStart = performance.mark();
9898
binder(file, options);
99-
performance.measure("bindTime", "bindStart");
99+
performance.measure("bindTime", bindStart);
100100
}
101101

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

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16064,11 +16064,11 @@ namespace ts {
1606416064
}
1606516065

1606616066
function checkSourceFile(node: SourceFile) {
16067-
performance.mark("checkStart");
16067+
const checkStart = performance.mark();
1606816068

1606916069
checkSourceFileWorker(node);
1607016070

16071-
performance.measure("checkTime", "checkStart");
16071+
performance.measure("checkTime", checkStart);
1607216072
}
1607316073

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

src/compiler/comments.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,42 +272,42 @@ namespace ts {
272272
reset,
273273
setSourceFile,
274274
getLeadingComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean, getTextRangeCallback?: (contextNode: Node) => TextRange): CommentRange[] {
275-
performance.mark("commentStart");
275+
const commentStart = performance.mark();
276276
const comments = getLeadingComments(range, contextNode, ignoreNodeCallback, getTextRangeCallback);
277-
performance.measure("commentTime", "commentStart");
277+
performance.measure("commentTime", commentStart);
278278
return comments;
279279
},
280280
getTrailingComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean, getTextRangeCallback?: (contextNode: Node) => TextRange): CommentRange[] {
281-
performance.mark("commentStart");
281+
const commentStart = performance.mark();
282282
const comments = getTrailingComments(range, contextNode, ignoreNodeCallback, getTextRangeCallback);
283-
performance.measure("commentTime", "commentStart");
283+
performance.measure("commentTime", commentStart);
284284
return comments;
285285
},
286286
getTrailingCommentsOfPosition(pos: number): CommentRange[] {
287-
performance.mark("commentStart");
287+
const commentStart = performance.mark();
288288
const comments = getTrailingCommentsOfPosition(pos);
289-
performance.measure("commentTime", "commentStart");
289+
performance.measure("commentTime", commentStart);
290290
return comments;
291291
},
292292
emitLeadingComments(range: TextRange, comments: CommentRange[], contextNode?: Node, getTextRangeCallback?: (contextNode: Node) => TextRange): void {
293-
performance.mark("commentStart");
293+
const commentStart = performance.mark();
294294
emitLeadingComments(range, comments, contextNode, getTextRangeCallback);
295-
performance.measure("commentTime", "commentStart");
295+
performance.measure("commentTime", commentStart);
296296
},
297297
emitTrailingComments(range: TextRange, comments: CommentRange[]): void {
298-
performance.mark("commentStart");
298+
const commentStart = performance.mark();
299299
emitLeadingComments(range, comments);
300-
performance.measure("commentTime", "commentStart");
300+
performance.measure("commentTime", commentStart);
301301
},
302302
emitLeadingDetachedComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean): void {
303-
performance.mark("commentStart");
303+
const commentStart = performance.mark();
304304
emitLeadingDetachedComments(range, contextNode, ignoreNodeCallback);
305-
performance.measure("commentTime", "commentStart");
305+
performance.measure("commentTime", commentStart);
306306
},
307307
emitTrailingDetachedComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean): void {
308-
performance.mark("commentStart");
308+
const commentStart = performance.mark();
309309
emitTrailingDetachedComments(range, contextNode, ignoreNodeCallback);
310-
performance.measure("commentTime", "commentStart");
310+
performance.measure("commentTime", commentStart);
311311
}
312312
};
313313
}

src/compiler/core.ts

Lines changed: 19 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,158 +1139,64 @@ namespace ts {
11391139
/** Performance measurements for the compiler. */
11401140
/*@internal*/
11411141
export namespace performance {
1142-
interface MarkData {
1143-
markName: string;
1144-
timestamp: number;
1145-
}
1146-
1147-
interface MeasureData {
1148-
measureName: string;
1149-
startMarkName: string;
1150-
endMarkName: string;
1151-
timestamp: number;
1152-
marksOffset: number;
1153-
}
1154-
1155-
export interface Measure {
1156-
name: string;
1157-
startTime: number;
1158-
duration: number;
1159-
}
1160-
1161-
const markTimestamps: Map<number> = {};
1162-
const markCounts: Map<number> = {};
1163-
const measureDurations: Map<number> = {};
1164-
1165-
let start = now();
1142+
let counters: Map<number> = {};
1143+
let measures: Map<number> = {};
11661144
let enabled = false;
11671145

1168-
/** Gets the current timer for performance measurements. */
1169-
export function now() {
1170-
return Date.now();
1171-
}
1172-
11731146
/**
1174-
* Adds a performance mark with the specified name.
1147+
* Increments a counter with the specified name.
11751148
*
1176-
* @param markName The name of the performance mark.
1149+
* @param counterName The name of the counter.
11771150
*/
1178-
export function mark(markName: string) {
1151+
export function increment(counterName: string) {
11791152
if (enabled) {
1180-
markTimestamps[markName] = now();
1181-
markCounts[markName] = getCount(markName) + 1;
1153+
counters[counterName] = (getProperty(counters, counterName) || 0) + 1;
11821154
}
11831155
}
11841156

11851157
/**
1186-
* Gets the names of all marks.
1187-
*/
1188-
export function getMarkNames() {
1189-
return getKeys(markCounts);
1190-
}
1191-
1192-
/**
1193-
* Gets the number of marks with the specified name.
1158+
* Gets the value of the counter with the specified name.
11941159
*
1195-
* @param markName The name of the marks that should be counted.
1160+
* @param counterName The name of the counter.
11961161
*/
1197-
export function getCount(markName: string) {
1198-
return enabled && getProperty(markCounts, markName) || 0;
1162+
export function getCount(counterName: string) {
1163+
return enabled && getProperty(counters, counterName) || 0;
11991164
}
12001165

12011166
/**
1202-
* Gets the most recent timestamp for the marks with the specified name.
1203-
*
1204-
* @param markName The name of the mark.
1167+
* Marks the start of a performance measurement.
12051168
*/
1206-
export function getTimestamp(markName: string) {
1207-
return enabled && getProperty(markTimestamps, markName) || 0;
1208-
}
1209-
1210-
/**
1211-
* Clears performance marks.
1212-
*
1213-
* @param markName The name of the mark whose time values should be cleared. If not
1214-
* specified, all marks will be cleared.
1215-
*/
1216-
export function clearMarks(markName?: string) {
1217-
if (markName === undefined) {
1218-
forEachKey(markTimestamps, clearMark);
1219-
}
1220-
else {
1221-
clearMark(markName);
1222-
}
1223-
}
1224-
1225-
function clearMark(markName: string) {
1226-
if (delete markTimestamps[markName]) {
1227-
delete markCounts[markName];
1228-
}
1169+
export function mark() {
1170+
return enabled ? Date.now() : 0;
12291171
}
12301172

12311173
/**
12321174
* Adds a performance measurement with the specified name.
12331175
*
12341176
* @param measureName The name of the performance measurement.
1235-
* @param startMarkName The name of the starting mark.
1236-
* If provided, the most recent time value of the start mark is used.
1237-
* If not specified, the value is the time that the performance service was
1238-
* initialized or the last time it was reset.
1239-
* @param endMarkName The name of the ending mark.
1240-
* If provided, the most recent time value of the end mark is used.
1241-
* If not specified, the current time is used.
1177+
* @param marker The timestamp of the starting mark.
12421178
*/
1243-
export function measure(measureName: string, startMarkName?: string, endMarkName?: string) {
1179+
export function measure(measureName: string, marker: number) {
12441180
if (enabled) {
1245-
const startTime = startMarkName ? getTimestamp(startMarkName) : start;
1246-
const endTime = endMarkName ? getTimestamp(endMarkName) : now();
1247-
const duration = endTime - startTime;
1248-
measureDurations[measureName] = getDuration(measureName) + duration;
1181+
measures[measureName] = (getProperty(measures, measureName) || 0) + (mark() - marker);
12491182
}
12501183
}
12511184

1252-
/**
1253-
* Gets the names of all recorded measures.
1254-
*/
1255-
export function getMeasureNames() {
1256-
return getKeys(measureDurations);
1257-
}
1258-
12591185
/**
12601186
* Gets the total duration of all measurements with the supplied name.
12611187
*
12621188
* @param measureName The name of the measure whose durations should be accumulated.
12631189
*/
12641190
export function getDuration(measureName: string) {
1265-
return enabled && getProperty(measureDurations, measureName) || 0;
1266-
}
1267-
1268-
/**
1269-
* Clears performance measures.
1270-
*
1271-
* @param measureName The name of the measure whose durations should be cleared. If not
1272-
* specified, all measures will be cleared.
1273-
*/
1274-
export function clearMeasures(measureName?: string) {
1275-
if (measureName === undefined) {
1276-
forEachKey(measureDurations, clearMeasure);
1277-
}
1278-
else {
1279-
clearMeasure(measureName);
1280-
}
1281-
}
1282-
1283-
function clearMeasure(measureName: string) {
1284-
delete measureDurations[measureName];
1191+
return enabled && getProperty(measures, measureName) || 0;
12851192
}
12861193

12871194
/**
12881195
* Resets all marks and measurements in the performance service.
12891196
*/
12901197
export function reset() {
1291-
clearMarks();
1292-
clearMeasures();
1293-
start = now();
1198+
counters = {};
1199+
measures = {};
12941200
}
12951201

12961202
/** Enables performance measurements for the compiler. */

src/compiler/printer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ const _super = (function (geti, seti) {
281281
}
282282

283283
function printSourceFileWithExtendedDiagnostics(node: SourceFile) {
284-
performance.mark("printStart");
284+
const printStart = performance.mark();
285285
printSourceFile(node);
286-
performance.measure("printTime", "printStart");
286+
performance.measure("printTime", printStart);
287287
return node;
288288
}
289289

src/compiler/program.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,9 @@ namespace ts {
776776
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile {
777777
let text: string;
778778
try {
779-
performance.mark("ioReadStart");
779+
const ioReadStart = performance.mark();
780780
text = sys.readFile(fileName, options.charset);
781-
performance.measure("ioReadTime", "ioReadStart");
781+
performance.measure("ioReadTime", ioReadStart);
782782
}
783783
catch (e) {
784784
if (onError) {
@@ -845,7 +845,7 @@ namespace ts {
845845

846846
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
847847
try {
848-
performance.mark("ioWriteStart");
848+
const ioWriteStart = performance.mark();
849849
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
850850

851851
if (isWatchSet(options) && sys.createHash && sys.getModifiedTime) {
@@ -855,7 +855,7 @@ namespace ts {
855855
sys.writeFile(fileName, data, writeByteOrderMark);
856856
}
857857

858-
performance.measure("ioWriteTime", "ioWriteStart");
858+
performance.measure("ioWriteTime", ioWriteStart);
859859
}
860860
catch (e) {
861861
if (onError) {
@@ -957,7 +957,7 @@ namespace ts {
957957
let resolvedTypeReferenceDirectives: Map<ResolvedTypeReferenceDirective> = {};
958958
let fileProcessingDiagnostics = createDiagnosticCollection();
959959

960-
performance.mark("programStart");
960+
const programStart = performance.mark();
961961

962962
host = host || createCompilerHost(options);
963963

@@ -1050,7 +1050,7 @@ namespace ts {
10501050

10511051
verifyCompilerOptions();
10521052

1053-
performance.measure("programTime", "programStart");
1053+
performance.measure("programTime", programStart);
10541054

10551055
return program;
10561056

@@ -1283,7 +1283,7 @@ namespace ts {
12831283
// checked is to not pass the file to getEmitResolver.
12841284
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile);
12851285

1286-
performance.mark("emitStart");
1286+
const emitStart = performance.mark();
12871287

12881288
// TODO(rbuckton): remove USE_TRANSFORMS condition when we switch to transforms permanently.
12891289
let useLegacyEmitter = options.useLegacyEmitter;
@@ -1297,7 +1297,7 @@ namespace ts {
12971297
getEmitHost(writeFileCallback),
12981298
sourceFile);
12991299

1300-
performance.measure("emitTime", "emitStart");
1300+
performance.measure("emitTime", emitStart);
13011301

13021302
return emitResult;
13031303
}

src/compiler/sourcemap.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -782,30 +782,30 @@ namespace ts {
782782
getSourceMapData,
783783
setSourceFile,
784784
emitPos(pos: number): void {
785-
performance.mark("sourcemapStart");
785+
const sourcemapStart = performance.mark();
786786
emitPos(pos);
787-
performance.measure("sourcemapTime", "sourcemapStart");
787+
performance.measure("sourceMapTime", sourcemapStart);
788788
},
789789
emitStart(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (node: Node) => boolean, ignoreChildrenCallback?: (node: Node) => boolean, getTextRangeCallback?: (node: Node) => TextRange): void {
790-
performance.mark("sourcemapStart");
790+
const sourcemapStart = performance.mark();
791791
emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback);
792-
performance.measure("sourcemapTime", "sourcemapStart");
792+
performance.measure("sourceMapTime", sourcemapStart);
793793
},
794794
emitEnd(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (node: Node) => boolean, ignoreChildrenCallback?: (node: Node) => boolean, getTextRangeCallback?: (node: Node) => TextRange): void {
795-
performance.mark("sourcemapStart");
795+
const sourcemapStart = performance.mark();
796796
emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback);
797-
performance.measure("sourcemapTime", "sourcemapStart");
797+
performance.measure("sourceMapTime", sourcemapStart);
798798
},
799799
emitTokenStart(token: SyntaxKind, tokenStartPos: number, contextNode?: Node, ignoreTokenCallback?: (node: Node) => boolean, getTokenTextRangeCallback?: (node: Node, token: SyntaxKind) => TextRange): number {
800-
performance.mark("sourcemapStart");
800+
const sourcemapStart = performance.mark();
801801
tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback);
802-
performance.measure("sourcemapTime", "sourcemapStart");
802+
performance.measure("sourceMapTime", sourcemapStart);
803803
return tokenStartPos;
804804
},
805805
emitTokenEnd(token: SyntaxKind, tokenEndPos: number, contextNode?: Node, ignoreTokenCallback?: (node: Node) => boolean, getTokenTextRangeCallback?: (node: Node, token: SyntaxKind) => TextRange): number {
806-
performance.mark("sourcemapStart");
806+
const sourcemapStart = performance.mark();
807807
tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback);
808-
performance.measure("sourcemapTime", "sourcemapStart");
808+
performance.measure("sourceMapTime", sourcemapStart);
809809
return tokenEndPos;
810810
},
811811
changeEmitSourcePos,

0 commit comments

Comments
 (0)