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

Optimize haste map data structure for serialization/deserialization. #8171

Merged
merged 10 commits into from Mar 20, 2019
60 changes: 23 additions & 37 deletions packages/jest-haste-map/src/__tests__/index.test.js
Expand Up @@ -340,28 +340,28 @@ describe('HasteMap', () => {

expect(data.files).toEqual(
createMap({
'fruits/Banana.js': ['Banana', 32, 42, 1, ['Strawberry'], null],
'fruits/Pear.js': ['Pear', 32, 42, 1, ['Banana', 'Strawberry'], null],
'fruits/Strawberry.js': ['Strawberry', 32, 42, 1, [], null],
'fruits/__mocks__/Pear.js': ['', 32, 42, 1, ['Melon'], null],
'fruits/Banana.js': ['Banana', 32, 42, 1, 'Strawberry', null],
'fruits/Pear.js': ['Pear', 32, 42, 1, 'Banana\tStrawberry', null],
'fruits/Strawberry.js': ['Strawberry', 32, 42, 1, '', null],
'fruits/__mocks__/Pear.js': ['', 32, 42, 1, 'Melon', null],
// node modules
'fruits/node_modules/fbjs/lib/flatMap.js': [
'flatMap',
32,
42,
1,
[],
'',
null,
],
'fruits/node_modules/react/React.js': [
'React',
32,
42,
1,
['Component'],
'Component',
null,
],
'vegetables/Melon.js': ['Melon', 32, 42, 1, [], null],
'vegetables/Melon.js': ['Melon', 32, 42, 1, '', null],
}),
);

Expand Down Expand Up @@ -410,18 +410,11 @@ describe('HasteMap', () => {

// The node crawler returns "null" for the SHA-1.
data.files = createMap({
'fruits/Banana.js': ['Banana', 32, 42, 0, ['Strawberry'], null],
'fruits/Pear.js': [
'Pear',
32,
42,
0,
['Banana', 'Strawberry'],
null,
],
'fruits/Strawberry.js': ['Strawberry', 32, 42, 0, [], null],
'fruits/__mocks__/Pear.js': ['', 32, 42, 0, ['Melon'], null],
'vegetables/Melon.js': ['Melon', 32, 42, 0, [], null],
'fruits/Banana.js': ['Banana', 32, 42, 0, 'Strawberry', null],
'fruits/Pear.js': ['Pear', 32, 42, 0, 'Banana/Strawberry', null],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing \t delimiter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks.

'fruits/Strawberry.js': ['Strawberry', 32, 42, 0, '', null],
'fruits/__mocks__/Pear.js': ['', 32, 42, 0, 'Melon', null],
'vegetables/Melon.js': ['Melon', 32, 42, 0, '', null],
});

return Promise.resolve({
Expand All @@ -446,39 +439,39 @@ describe('HasteMap', () => {
32,
42,
1,
['Strawberry'],
'Strawberry',
'7772b628e422e8cf59c526be4bb9f44c0898e3d1',
],
'fruits/Pear.js': [
'Pear',
32,
42,
1,
['Banana', 'Strawberry'],
'Banana\tStrawberry',
'89d0c2cc11dcc5e1df50b8af04ab1b597acfba2f',
],
'fruits/Strawberry.js': [
'Strawberry',
32,
42,
1,
[],
'',
'e8aa38e232b3795f062f1d777731d9240c0f8c25',
],
'fruits/__mocks__/Pear.js': [
'',
32,
42,
1,
['Melon'],
'Melon',
'8d40afbb6e2dc78e1ba383b6d02cafad35cceef2',
],
'vegetables/Melon.js': [
'Melon',
32,
42,
1,
[],
'',
'f16ccf6f2334ceff2ddb47628a2c5f2d748198ca',
],
}),
Expand Down Expand Up @@ -626,18 +619,18 @@ describe('HasteMap', () => {
32,
42,
1,
['Blackberry'],
'Blackberry',
null,
],
'fruits/Strawberry.ios.js': [
'Strawberry',
32,
42,
1,
['Raspberry'],
'Raspberry',
null,
],
'fruits/Strawberry.js': ['Strawberry', 32, 42, 1, ['Banana'], null],
'fruits/Strawberry.js': ['Strawberry', 32, 42, 1, 'Banana', null],
}),
);

Expand Down Expand Up @@ -724,14 +717,7 @@ describe('HasteMap', () => {
expect(useBuitinsInContext(data.clocks)).toEqual(mockClocks);

const files = new Map(initialData.files);
files.set('fruits/Banana.js', [
'Banana',
32,
42,
1,
['Kiwi'],
null,
]);
files.set('fruits/Banana.js', ['Banana', 32, 42, 1, 'Kiwi', null]);

expect(useBuitinsInContext(data.files)).toEqual(files);

Expand Down Expand Up @@ -1102,7 +1088,7 @@ describe('HasteMap', () => {

expect(data.files).toEqual(
createMap({
'fruits/Banana.js': ['Banana', 32, 42, 1, ['Strawberry'], null],
'fruits/Banana.js': ['Banana', 32, 42, 1, 'Strawberry', null],
}),
);

Expand Down Expand Up @@ -1136,7 +1122,7 @@ describe('HasteMap', () => {

expect(data.files).toEqual(
createMap({
'fruits/Banana.js': ['Banana', 32, 42, 1, ['Strawberry'], null],
'fruits/Banana.js': ['Banana', 32, 42, 1, 'Strawberry', null],
}),
);
});
Expand Down
8 changes: 5 additions & 3 deletions packages/jest-haste-map/src/index.ts
Expand Up @@ -326,7 +326,7 @@ class HasteMap extends EventEmitter {
name: string,
...extra: Array<string>
): string {
const hash = crypto.createHash('md5').update(extra.join(''));
const hash = crypto.createHash('md5').update(extra.join('') + 'v2');
scotthovestadt marked this conversation as resolved.
Show resolved Hide resolved
return path.join(
tmpdir,
name.replace(/\W/g, '-') + '-' + hash.digest('hex'),
Expand Down Expand Up @@ -522,7 +522,9 @@ class HasteMap extends EventEmitter {
setModule(metadataId, metadataModule);
}

fileMetadata[H.DEPENDENCIES] = metadata.dependencies || [];
fileMetadata[H.DEPENDENCIES] = metadata.dependencies
? metadata.dependencies.join('\t')
: '';

if (computeSha1) {
fileMetadata[H.SHA1] = metadata.sha1;
Expand Down Expand Up @@ -940,7 +942,7 @@ class HasteMap extends EventEmitter {
stat ? stat.mtime.getTime() : -1,
stat ? stat.size : 0,
0,
[],
'',
null,
];
hasteMap.files.set(relativeFilePath, fileMetadata);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/types.ts
Expand Up @@ -51,7 +51,7 @@ export type FileMetaData = [
/* mtime */ number,
/* size */ number,
/* visited */ 0 | 1,
/* dependencies */ Array<string>,
/* dependencies */ string,
/* sha1 */ string | null | undefined
];

Expand Down