Skip to content

Commit d4621fe

Browse files
committed
More compact storage micro-optimization.
1 parent 8fec4f0 commit d4621fe

12 files changed

Lines changed: 236 additions & 232 deletions

File tree

packages/jest-haste-map/src/__tests__/fastpath-test.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
* original author: dead_horse <dead_horse@qq.com>
2424
* ported by: yaycmyk <evan@yaycmyk.com>
2525
*/
26+
'use strict';
2627

27-
jest.dontMock('../fastpath');
28+
jest.unmock('../fastpath');
2829

29-
const fp = require('../fastpath');
30+
let fp;
3031

3132
describe('fast-path', () => {
33+
3234
const actualPlatform = process.platform;
3335
const invalidInputTests = [
3436
true,
@@ -62,6 +64,7 @@ describe('fast-path', () => {
6264
it('should resolve given paths', () => {
6365
process.platform = actualPlatform;
6466

67+
fp = require('../fastpath');
6568
expect(fp.basename(__filename)).toEqual('fastpath-test.js');
6669
expect(fp.basename(__filename, '.js')).toEqual('fastpath-test');
6770
expect(fp.basename('')).toEqual('');
@@ -75,6 +78,7 @@ describe('fast-path', () => {
7578
it('should handle backslashes properly (win32)', () => {
7679
process.platform = 'win32';
7780

81+
fp = require('../fastpath');
7882
expect(fp.basename('\\dir\\basename.ext')).toEqual('basename.ext');
7983
expect(fp.basename('\\basename.ext')).toEqual('basename.ext');
8084
expect(fp.basename('basename.ext')).toEqual('basename.ext');
@@ -83,6 +87,7 @@ describe('fast-path', () => {
8387
});
8488

8589
it('should handle backslashes properly (posix)', () => {
90+
fp = require('../fastpath');
8691
expect(fp.basename('\\dir\\basename.ext')).toEqual('\\dir\\basename.ext');
8792
expect(fp.basename('\\basename.ext')).toEqual('\\basename.ext');
8893
expect(fp.basename('basename.ext')).toEqual('basename.ext');
@@ -91,6 +96,7 @@ describe('fast-path', () => {
9196
});
9297

9398
it('should handle control characters (posix)', () => {
99+
fp = require('../fastpath');
94100
// POSIX filenames may include control characters
95101
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
96102
const name = 'Icon' + String.fromCharCode(13);
@@ -102,6 +108,7 @@ describe('fast-path', () => {
102108
it('should extract the extension from a path', () => {
103109
process.platform = actualPlatform;
104110

111+
fp = require('../fastpath');
105112
expect(fp.extname(__filename)).toEqual('.js');
106113
expect(fp.extname('')).toEqual('');
107114
expect(fp.extname('/path/to/file')).toEqual('');
@@ -149,6 +156,7 @@ describe('fast-path', () => {
149156
it('should handle path backslashes (win32)', () => {
150157
process.platform = 'win32';
151158

159+
fp = require('../fastpath');
152160
// On windows, backspace is a path separator.
153161
expect(fp.extname('.\\')).toEqual('');
154162
expect(fp.extname('..\\')).toEqual('');
@@ -161,6 +169,8 @@ describe('fast-path', () => {
161169
});
162170

163171
it('should handle path backslashes (posix)', () => {
172+
fp = require('../fastpath');
173+
164174
// On unix, backspace is a valid name component like any other character.
165175
expect(fp.extname('.\\')).toEqual('');
166176
expect(fp.extname('..\\')).toEqual('.\\');
@@ -175,6 +185,8 @@ describe('fast-path', () => {
175185

176186
describe('dirname', () => {
177187
it('should isolate the directory name from a path (posix)', () => {
188+
fp = require('../fastpath');
189+
178190
expect(fp.dirname('/a/b/')).toEqual('/a');
179191
expect(fp.dirname('/a/b')).toEqual('/a');
180192
expect(fp.dirname('/a')).toEqual('/');
@@ -186,6 +198,7 @@ describe('fast-path', () => {
186198
it('should isolate the directory name from a path (win32)', () => {
187199
process.platform = 'win32';
188200

201+
fp = require('../fastpath');
189202
expect(fp.dirname('C:\\')).toEqual('C:\\');
190203
expect(fp.dirname('c:\\')).toEqual('c:\\');
191204
expect(fp.dirname('c:\\foo')).toEqual('c:\\');
@@ -314,6 +327,8 @@ describe('fast-path', () => {
314327
]);
315328

316329
it('should join the paths correctly (posix)', () => {
330+
fp = require('../fastpath');
331+
317332
posixJoinTests.forEach(test => {
318333
const actual = fp.join.apply(fp, test[0]);
319334
const expected = test[1];
@@ -329,6 +344,7 @@ describe('fast-path', () => {
329344
it('should join the paths correctly (win32)', () => {
330345
process.platform = 'win32';
331346

347+
fp = require('../fastpath');
332348
win32JoinTests.forEach(test => {
333349
const actual = fp.join.apply(fp, test[0]);
334350
const expected = test[1].replace(/\//g, '\\');
@@ -342,14 +358,18 @@ describe('fast-path', () => {
342358
});
343359

344360
it('should throw for invalid input', () => {
361+
fp = require('../fastpath');
362+
345363
invalidInputTests.forEach(test => {
346-
expect(() => fp.jointest).toThrow();
364+
expect(() => fp.join(test)).toThrow();
347365
});
348366
});
349367
});
350368

351369
describe('normalize', () => {
352370
it('should return a valid path (posix)', () => {
371+
fp = require('../fastpath');
372+
353373
expect(fp.normalize('./fixtures///b/../b/c.js')).toEqual('fixtures/b/c.js');
354374
expect(fp.normalize('/foo/../../../bar')).toEqual('/bar');
355375
expect(fp.normalize('a//b//../b')).toEqual('a/b');
@@ -359,6 +379,8 @@ describe('fast-path', () => {
359379

360380
it('should return a valid path (win32)', () => {
361381
process.platform = 'win32';
382+
383+
fp = require('../fastpath');
362384
expect(fp.normalize('./fixtures///b/../b/c.js')).toEqual('fixtures\\b\\c.js');
363385
expect(fp.normalize('/foo/../../../bar')).toEqual('\\bar');
364386
expect(fp.normalize('a//b//../b')).toEqual('a\\b');
@@ -394,13 +416,16 @@ describe('fast-path', () => {
394416
it('should resolve the current working directory', () => {
395417
process.platform = actualPlatform;
396418

419+
fp = require('../fastpath');
397420
const actual = fp.resolve('.');
398421
const expected = process.cwd();
399422

400423
expect(actual).toEqual(expected, getErrorMessage('resolve', '.', expected, actual));
401424
});
402425

403426
it('should resolve paths (posix)', () => {
427+
fp = require('../fastpath');
428+
404429
posixResolveTests.forEach(test => {
405430
const actual = fp.resolve.apply(fp, test[0]);
406431
const expected = test[1];
@@ -412,6 +437,7 @@ describe('fast-path', () => {
412437
it('should resolve paths (win32)', () => {
413438
process.platform = 'win32';
414439

440+
fp = require('../fastpath');
415441
win32ResolveTests.forEach(test => {
416442
const actual = fp.resolve.apply(fp, test[0]);
417443
const expected = test[1];
@@ -421,14 +447,17 @@ describe('fast-path', () => {
421447
});
422448

423449
it('should throw for invalid input', () => {
450+
fp = require('../fastpath');
424451
invalidInputTests.forEach(test => {
425-
expect(() => fp.resolvetest).toThrow();
452+
expect(() => fp.resolve(test)).toThrow();
426453
});
427454
});
428455
});
429456

430457
describe('isAbsolute', () => {
431458
it('should work (posix)', () => {
459+
fp = require('../fastpath');
460+
432461
expect(fp.isAbsolute('/home/foo')).toEqual(true);
433462
expect(fp.isAbsolute('/home/foo/..')).toEqual(true);
434463
expect(fp.isAbsolute('bar/')).toEqual(false);
@@ -437,6 +466,9 @@ describe('fast-path', () => {
437466

438467
it('should work (win32)', () => {
439468
process.platform = 'win32';
469+
470+
fp = require('../fastpath');
471+
440472
expect(fp.isAbsolute('//server/file')).toEqual(true);
441473
expect(fp.isAbsolute('\\\\server\\file')).toEqual(true);
442474
expect(fp.isAbsolute('C:/Users/')).toEqual(true);
@@ -473,6 +505,7 @@ describe('fast-path', () => {
473505
];
474506

475507
it('should work (posix)', () => {
508+
fp = require('../fastpath');
476509
posixRelativeTests.forEach(test => {
477510
const actual = fp.relative(test[0], test[1]);
478511
const expected = test[2];
@@ -488,6 +521,7 @@ describe('fast-path', () => {
488521
it('should work (win32)', () => {
489522
process.platform = 'win32';
490523

524+
fp = require('../fastpath');
491525
win32RelativeTests.forEach(test => {
492526
const actual = fp.relative(test[0], test[1]);
493527
const expected = test[2];
@@ -505,10 +539,12 @@ describe('fast-path', () => {
505539
it('should be a backslash (win32)', () => {
506540
process.platform = 'win32';
507541

542+
fp = require('../fastpath');
508543
expect(fp.sep).toEqual('\\');
509544
});
510545

511546
it('should be a forward slash (posix)', () => {
547+
fp = require('../fastpath');
512548
expect(fp.sep).toEqual('/');
513549
});
514550
});
@@ -517,10 +553,12 @@ describe('fast-path', () => {
517553
it('should be a semicolon (win32)', () => {
518554
process.platform = 'win32';
519555

556+
fp = require('../fastpath');
520557
expect(fp.delimiter).toEqual(';');
521558
});
522559

523560
it('should be a colon (posix)', () => {
561+
fp = require('../fastpath');
524562
expect(fp.delimiter).toEqual(':');
525563
});
526564
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
/*
10+
* This file exports a set of constants that are used for Jest's haste map
11+
* serialization. On very large repositories, the haste map cache becomes very
12+
* large to the point where it is the largest overhead in starting up Jest.
13+
*
14+
* This constant key map allows to keep the map smaller without having to build
15+
* a custom serialization library.
16+
*/
17+
module.exports = {
18+
/* shared in file map and module map */
19+
ID: 0,
20+
21+
/* file map attributes */
22+
MTIME: 1,
23+
VISITED: 2,
24+
DEPENDENCIES: 3,
25+
26+
/* module map attributes */
27+
PATH: 1,
28+
TYPE: 2,
29+
30+
/* module types */
31+
MODULE: 0,
32+
PACKAGE: 1,
33+
34+
/* platforms */
35+
GENERIC_PLATFORM: 'g',
36+
};

packages/jest-haste-map/src/crawlers/node.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
'use strict';
1111

12+
const H = require('../constants');
13+
1214
const fs = require('fs');
1315
const os = require('os');
1416
const path = require('path');
1517
const spawn = require('child_process').spawn;
1618

17-
function find(roots, extensions, ignorePattern, callback) {
19+
function find(roots, extensions, ignore, callback) {
1820
const result = [];
1921
let activeCalls = 0;
2022

@@ -28,7 +30,7 @@ function find(roots, extensions, ignorePattern, callback) {
2830
}
2931

3032
names.forEach(file => {
31-
if (ignorePattern.test(file)) {
33+
if (ignore(file)) {
3234
return;
3335
}
3436
activeCalls++;
@@ -61,7 +63,7 @@ function find(roots, extensions, ignorePattern, callback) {
6163
roots.forEach(search);
6264
}
6365

64-
function findNative(roots, extensions, ignorePattern, callback) {
66+
function findNative(roots, extensions, ignore, callback) {
6567
const args = [].concat(roots);
6668
args.push('-type', 'f');
6769
extensions.forEach((ext, index) => {
@@ -80,7 +82,7 @@ function findNative(roots, extensions, ignorePattern, callback) {
8082
child.stdout.on('close', code => {
8183
const lines = stdout.trim()
8284
.split('\n')
83-
.filter(x => !ignorePattern.test(x));
85+
.filter(x => !ignore(x));
8486
const result = [];
8587
let count = lines.length;
8688
lines.forEach(path => {
@@ -96,33 +98,28 @@ function findNative(roots, extensions, ignorePattern, callback) {
9698
});
9799
}
98100

99-
module.exports = function nodeCrawl(roots, extensions, ignorePattern, data) {
101+
module.exports = function nodeCrawl(roots, extensions, ignore, data) {
100102
return new Promise(resolve => {
101103
const callback = list => {
102104
const files = Object.create(null);
103105
list.forEach(fileData => {
104106
const name = fileData[0];
105107
const mtime = fileData[1];
106108
const existingFile = data.files[name];
107-
if (existingFile && existingFile.mtime === mtime) {
108-
//console.log('exists', name);
109+
if (existingFile && existingFile[H.MTIME] === mtime) {
109110
files[name] = existingFile;
110111
} else {
111-
//console.log('add', name);
112-
files[name] = {
113-
mtime,
114-
visited: false,
115-
};
112+
files[name] = [0, mtime, 0, []];
116113
}
117114
});
118115
data.files = files;
119116
resolve(data);
120117
};
121118

122119
if (os.platform() == 'win32') {
123-
find(roots, extensions, ignorePattern, callback);
120+
find(roots, extensions, ignore, callback);
124121
} else {
125-
findNative(roots, extensions, ignorePattern, callback);
122+
findNative(roots, extensions, ignore, callback);
126123
}
127124
});
128125
};

packages/jest-haste-map/src/crawlers/watchman.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
'use strict';
1111

12+
const H = require('../constants');
13+
1214
const denodeify = require('denodeify');
1315
const path = require('../fastpath');
1416
const watchman = require('fb-watchman');
@@ -29,7 +31,7 @@ function WatchmanError(error) {
2931
module.exports = function watchmanCrawl(
3032
roots,
3133
extensions,
32-
ignorePattern,
34+
ignore,
3335
data
3436
) {
3537
const files = data.files;
@@ -78,18 +80,12 @@ module.exports = function watchmanCrawl(
7880
response.files.forEach(fileData => {
7981
const name = root + path.sep + fileData.name;
8082
if (!fileData.exists) {
81-
//console.log('remove', name);
8283
delete files[name];
83-
} else if (!ignorePattern.test(name)) {
84-
//console.log('add', name);
84+
} else if (!ignore(name)) {
8585
const mtime = fileData.mtime_ms.toNumber();
86-
const isNew = !files[name] || files[name].mtime !== mtime;
86+
const isNew = !files[name] || files[name][H.MTIME] !== mtime;
8787
if (isNew) {
88-
files[name] = {
89-
id: null,
90-
mtime,
91-
visited: false,
92-
};
88+
files[name] = [0, mtime, 0, []];
9389
}
9490
}
9591
});

0 commit comments

Comments
 (0)