From fbc9537952622645a4a45ce28ec90956c85922f9 Mon Sep 17 00:00:00 2001 From: Vikram Subramanian Date: Wed, 4 Oct 2017 01:53:39 -0700 Subject: [PATCH] perf(compiler): fix perf issue in loading aot summaries in jit compiler The current `flattenSummaries` function re-process the same NgModule summary even if it has been processed before. Certain modules like CommonModule are repeated multiple times in the module tree and it is expanded out every time. This was making unit tests using AOT summaries really slow. This will also slow down JIT bootstrap applications that load AOT summaries for component libraries. The fix is to remember which summaries were seen before and not to process them again. --- packages/compiler/src/jit/compiler.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/compiler/src/jit/compiler.ts b/packages/compiler/src/jit/compiler.ts index 409b3a49b1..18ddbe5686 100644 --- a/packages/compiler/src/jit/compiler.ts +++ b/packages/compiler/src/jit/compiler.ts @@ -335,14 +335,22 @@ function assertComponent(meta: CompileDirectiveMetadata) { } } -function flattenSummaries(fn: () => any[], out: CompileTypeSummary[] = []): CompileTypeSummary[] { - fn().forEach((entry) => { +function flattenSummaries( + fn: () => any[], out: CompileTypeSummary[] = [], + seen = new Set<() => any[]>()): CompileTypeSummary[] { + if (seen.has(fn)) { + return out; + } + seen.add(fn); + const summaries = fn(); + for (let i = 0; i < summaries.length; i++) { + const entry = summaries[i]; if (typeof entry === 'function') { - flattenSummaries(entry, out); + flattenSummaries(entry, out, seen); } else { out.push(entry); } - }); + } return out; }