perf(ngcc): speed up the getBasePaths() computation (#36881)

This function needs to deduplicate the paths that are found from the
paths mappings. Previously this deduplication was not linear and also
called the expensive `relative()` function many times.

This commit, suggested by @JoostK, reduces the complexity of the deduplication
by using a tree structure built from the segments of each path.

PR Close #36881
This commit is contained in:
Pete Bacon Darwin
2020-05-01 15:13:28 +01:00
committed by Alex Rickabaugh
parent ec6b9cc17d
commit e037840b88
2 changed files with 97 additions and 31 deletions

View File

@ -27,7 +27,7 @@ runInEachFileSystem(() => {
expect(basePaths).toEqual([sourceDirectory]);
});
it('should use each path mapping prefix and sort in descending order', () => {
it('should use each path mapping prefix', () => {
const projectDirectory = _('/path/to/project');
const fs = getFileSystem();
fs.ensureDir(fs.resolve(projectDirectory, 'dist-1'));
@ -41,10 +41,27 @@ runInEachFileSystem(() => {
};
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
expect(basePaths).toEqual([
fs.resolve(projectDirectory, 'sub-folder/dist-2'),
sourceDirectory,
fs.resolve(projectDirectory, 'libs'),
fs.resolve(projectDirectory, 'dist-1'),
fs.resolve(projectDirectory, 'libs'),
fs.resolve(projectDirectory, 'sub-folder/dist-2'),
]);
});
it('should not be confused by folders that have the same starting string', () => {
const projectDirectory = _('/path/to/project');
const fs = getFileSystem();
fs.ensureDir(fs.resolve(projectDirectory, 'a/b'));
fs.ensureDir(fs.resolve(projectDirectory, 'a/b-2'));
fs.ensureDir(fs.resolve(projectDirectory, 'a/b/c'));
const sourceDirectory = _('/path/to/project/node_modules');
const pathMappings = {baseUrl: projectDirectory, paths: {'@dist': ['a/b', 'a/b-2', 'a/b/c']}};
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
expect(basePaths).toEqual([
sourceDirectory,
fs.resolve(projectDirectory, 'a/b'),
fs.resolve(projectDirectory, 'a/b-2'),
]);
});
@ -105,8 +122,8 @@ runInEachFileSystem(() => {
const pathMappings = {baseUrl: _('/'), paths: {'@dist': ['dist']}};
const basePaths = getBasePaths(logger, sourceDirectory, pathMappings);
expect(basePaths).toEqual([
sourceDirectory,
fs.resolve('/dist'),
sourceDirectory,
]);
expect(logger.logs.warn).toEqual([
[`The provided pathMappings baseUrl is the root path ${_('/')}.\n` +