refactor(ivy): rewrite flatten function to be more memory efficient (#30468)
The `flatten` function used `concat` and `slice` which created a lot of intermediary object allocations. Because `flatten` is used from query any benchmark which used query would exhibit high minor GC counts. PR Close #30468
This commit is contained in:
parent
c9f5f3d802
commit
f3c69e7f6b
|
@ -3,7 +3,7 @@
|
|||
"master": {
|
||||
"uncompressed": {
|
||||
"runtime": 1497,
|
||||
"main": 166739,
|
||||
"main": 166799,
|
||||
"polyfills": 43626
|
||||
}
|
||||
}
|
||||
|
@ -34,4 +34,4 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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++;
|
||||
}
|
||||
function flatten(list, dst) {
|
||||
if (void 0 === dst) dst = list;
|
||||
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);
|
||||
} else if (dst !== list) dst.push(item);
|
||||
}
|
||||
return result;
|
||||
return dst;
|
||||
}
|
||||
|
||||
class EventEmitter extends Subject {
|
||||
|
|
|
@ -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),
|
||||
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,
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue