Skip to content

Avoid writing files that are not changed while compiling incrementally. #6937

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

Merged
merged 5 commits into from
Apr 4, 2016
Merged
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ namespace ts {
referenceCount: number;
}

interface OutputFingerprint {
hash: string;
mtime: Date;
}

interface OutputFingerprintMap {
[fileName: string]: OutputFingerprint;
}

declare var require: any;
declare var module: any;
declare var process: any;
Expand Down Expand Up @@ -226,6 +235,7 @@ namespace ts {
const _fs = require("fs");
const _path = require("path");
const _os = require("os");
const _crypto = require("crypto");

// average async stat takes about 30 microseconds
// set chunk size to do 30 files in < 1 millisecond
Expand Down Expand Up @@ -439,12 +449,26 @@ namespace ts {
return buffer.toString("utf8");
}

const outputFingerprintMap: OutputFingerprintMap = {};

function writeFile(fileName: string, data: string, writeByteOrderMark?: boolean): void {
// If a BOM is required, emit one
if (writeByteOrderMark) {
data = "\uFEFF" + data;
}

const md5 = getMd5(data);
Copy link
Contributor

Choose a reason for hiding this comment

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

i do not think we should do this for all files. in a normal batch compilation secnario, you will doing a fileExits, stat, and createHash needlessly.

Instead, we should expose a new optional function on sys to getModifiedTime, and createHash. in the watch logic, we can query them as needed, and store the map outputFileName => { mtime, hash} there as well.

const mtimeBefore = _fs.existsSync(fileName) && _fs.statSync(fileName).mtime;

if (mtimeBefore && outputFingerprintMap.hasOwnProperty(fileName)) {
const fingerprint = outputFingerprintMap[fileName];

// If output has not been changed, and the file has no external modification
if (fingerprint.hash === md5 && fingerprint.mtime.getTime() === mtimeBefore.getTime()) {
return;
}
}

let fd: number;

try {
Expand All @@ -456,6 +480,19 @@ namespace ts {
_fs.closeSync(fd);
}
}

const mtimeAfter = _fs.statSync(fileName).mtime;

outputFingerprintMap[fileName] = {
hash: md5,
mtime: mtimeAfter
};
}

function getMd5(data: string): string {
const hash = _crypto.createHash("md5");
hash.update(data);
return hash.digest("hex");
}

function getCanonicalPath(path: string): string {
Expand Down