Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): avoid storing LView in __ngContext__
Currently we save a reference to an `LView` on most DOM nodes created by Angular either by saving the `LView` directly in the `__ngContext__` or by saving the `LContext` which has a reference to the `LView`. This can be a problem if the DOM node is retained in memory, because the `LView` has references to all of the child nodes of the view, as well as other internal data structures. Previously we tried to resolve the issue by clearing the `__ngContext__` when a node is removed (see #36011), but we decided not to proceeed, because it can slow down destruction due to a megamorphic write. These changes aim to address the issue while reducing the performance impact by assigning a unique ID when an `LView` is created and adding it to `__ngContext__`. All active views are tracked in an array where their unique ID corresponds to an index. Using an array should guarantee fast creation, destruction and lookups. We don't need to worry about leaks within that array, because `LView`s are an internal data structure and we have complete control over when they are created and destroyed. Fixes #41047.
- Loading branch information
Showing
34 changed files
with
505 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {assertNumber} from '../../util/assert'; | ||
import {ID, LView} from '../interfaces/view'; | ||
|
||
// Keeps track of the currently-active LViews. | ||
const TRACKED_LVIEWS = new Map<number, LView>(); | ||
|
||
// Used for generating unique IDs for LViews. | ||
let uniqueIdCounter = 0; | ||
|
||
/** Starts tracking an LView and returns a unique ID that can be used for future lookups. */ | ||
export function trackLView(lView: LView): number { | ||
const id = uniqueIdCounter++; | ||
TRACKED_LVIEWS.set(id, lView); | ||
return id; | ||
} | ||
|
||
/** Gets an LView by its unique ID. */ | ||
export function getLViewById(id: number): LView|null { | ||
ngDevMode && assertNumber(id, 'ID used for LView lookup must be a number'); | ||
return TRACKED_LVIEWS.get(id) || null; | ||
} | ||
|
||
/** Stops tracking an LView. */ | ||
export function stopTrackingLView(lView: LView): void { | ||
ngDevMode && assertNumber(lView[ID], 'Cannot stop tracking an LView that does not have an ID'); | ||
TRACKED_LVIEWS.delete(lView[ID]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.