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

refactor(ivy): rewrite flatten function to be more memory efficient #30468

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration/_payload-limits.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"master": {
"uncompressed": {
"runtime": 1497,
"main": 166739,
"main": 166799,
"polyfills": 43626
}
}
Expand Down Expand Up @@ -34,4 +34,4 @@
}
}
}
}
}
24 changes: 10 additions & 14 deletions integration/side-effects/snapshots/core/esm2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,16 @@ function getSymbolIterator() {

if ("undefined" === typeof ngI18nClosureMode) _global["ngI18nClosureMode"] = "undefined" !== typeof goog && "function" === typeof goog.getMsg;

function flatten(list, mapFn) {
const result = [];
let i = 0;
while (i < list.length) {
const item = list[i];
if (Array.isArray(item)) if (item.length > 0) {
list = item.concat(list.slice(i + 1));
i = 0;
} else i++; else {
result.push(mapFn ? mapFn(item) : item);
i++;
}
}
return result;
function flatten(list, dst) {
if (void 0 === dst) dst = list;
Copy link
Member

Choose a reason for hiding this comment

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

AFAIK uglify will do this change for us, so we can use undefined here for readability

for (let i = 0; i < list.length; i++) {
let item = list[i];
if (Array.isArray(item)) {
if (dst === list) dst = list.slice(0, i);
flatten(item, dst);

Choose a reason for hiding this comment

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

we could maybe check the length of dst and call flatten if there are any elements inside?

} else if (dst !== list) dst.push(item);
}
return dst;
}

class EventEmitter extends Subject {
Expand Down
15 changes: 9 additions & 6 deletions packages/core/src/render3/jit/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ export function compileNgModuleDefs(moduleType: NgModuleType, ngModule: NgModule
ngModuleDef = getCompilerFacade().compileNgModule(
angularCoreEnv, `ng:///${moduleType.name}/ngModuleDef.js`, {
type: moduleType,
bootstrap: flatten(ngModule.bootstrap || EMPTY_ARRAY, resolveForwardRef),
bootstrap: flatten(ngModule.bootstrap || EMPTY_ARRAY).map(resolveForwardRef),
mhevery marked this conversation as resolved.
Show resolved Hide resolved
declarations: declarations.map(resolveForwardRef),
imports: flatten(ngModule.imports || EMPTY_ARRAY, resolveForwardRef)
imports: flatten(ngModule.imports || EMPTY_ARRAY)
.map(resolveForwardRef)
.map(expandModuleWithProviders),
exports: flatten(ngModule.exports || EMPTY_ARRAY, resolveForwardRef)
exports: flatten(ngModule.exports || EMPTY_ARRAY)
.map(resolveForwardRef)
.map(expandModuleWithProviders),
emitInline: true,
schemas: ngModule.schemas ? flatten(ngModule.schemas) : null,
Expand Down Expand Up @@ -156,12 +158,12 @@ function verifySemanticsOfNgModuleDef(moduleType: NgModuleType): void {
const errors: string[] = [];
const declarations = maybeUnwrapFn(ngModuleDef.declarations);
const imports = maybeUnwrapFn(ngModuleDef.imports);
flatten(imports, unwrapModuleWithProvidersImports).forEach(verifySemanticsOfNgModuleDef);
flatten(imports).map(unwrapModuleWithProvidersImports).forEach(verifySemanticsOfNgModuleDef);
const exports = maybeUnwrapFn(ngModuleDef.exports);
declarations.forEach(verifyDeclarationsHaveDefinitions);
const combinedDeclarations: Type<any>[] = [
...declarations.map(resolveForwardRef), //
...flatten(imports.map(computeCombinedExports), resolveForwardRef),
...flatten(imports.map(computeCombinedExports)).map(resolveForwardRef),
];
exports.forEach(verifyExportsAreDeclaredOrReExported);
declarations.forEach(verifyDeclarationIsUnique);
Expand All @@ -170,7 +172,8 @@ function verifySemanticsOfNgModuleDef(moduleType: NgModuleType): void {
const ngModule = getAnnotation<NgModule>(moduleType, 'NgModule');
if (ngModule) {
ngModule.imports &&
flatten(ngModule.imports, unwrapModuleWithProvidersImports)
flatten(ngModule.imports)
.map(unwrapModuleWithProvidersImports)
.forEach(verifySemanticsOfNgModuleDef);
ngModule.bootstrap && ngModule.bootstrap.forEach(verifyCorrectBootstrapType);
ngModule.bootstrap && ngModule.bootstrap.forEach(verifyComponentIsPartOfNgModule);
Expand Down
31 changes: 15 additions & 16 deletions packages/core/src/util/array_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ export function addAllToArray(items: any[], arr: any[]) {
}

/**
* Flattens an array in non-recursive way. Input arrays are not modified.
* Flattens an array.
*/
export function flatten(list: any[], mapFn?: (value: any) => any): any[] {
const result: any[] = [];
let i = 0;
while (i < list.length) {
const item = list[i];
export function flatten(list: any[], dst?: any[]): any[] {
if (dst === undefined) dst = list;
for (let i = 0; i < list.length; i++) {
let item = list[i];
if (Array.isArray(item)) {
if (item.length > 0) {
list = item.concat(list.slice(i + 1));
i = 0;
} else {
i++;
// we need to inline it.
if (dst === list) {
// Our assumption that the list was already flat was wrong and
// we need to clone flat since we need to write to it.
dst = list.slice(0, i);
}
} else {
result.push(mapFn ? mapFn(item) : item);
i++;
flatten(item, dst);
} else if (dst !== list) {
dst.push(item);
}
}
return result;
}
return dst;
}