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(core): explore approach to avoid storing LView in __ngContext__ #41293

Closed
wants to merge 1 commit into from

Conversation

crisbeto
Copy link
Member

Note: this is mostly an exploratory PR to check whether the approach actually works and to start a discussion. There's more work to be done to make it mergeable (e.g. it introduces new circular dependencies, there are no unit tests etc.).

Explores adding a unique ID to each LView which is stored in __ngContext__, rather than the LView itself. This will avoid worsening memory leaks where a detached element is retained along with its LView.

Furthermore, it reworks LContext to store the ID of its LView, rather than the LView itself, because the LContext can also be stored in __ngContext__.

@@ -108,6 +108,33 @@
"packages/compiler/src/render3/view/styling_builder.ts",
"packages/compiler/src/render3/view/template.ts"
],
[
Copy link
Member Author

Choose a reason for hiding this comment

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

Please ignore these circular dependencies. If we decide to go for this approach, I'll make sure that no new circular dependencies are introduced.

@@ -123,6 +123,8 @@ function renderChildComponents(hostLView: LView, components: number[]): void {
}
}

const ACTIVE_LVIEWS: (LView|null)[] = [];
Copy link
Member Author

Choose a reason for hiding this comment

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

This part is potentially leaky, but IMO it'll be fine, because this is the only place where LViews are created and destroyed and we have full control over it.

Copy link
Contributor

@mhevery mhevery left a comment

Choose a reason for hiding this comment

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

This Looks promising, I think we should explore further. My thoughts:

  • With this we are essentially getting into the business of memory management. In practice this is a ref counting scheme. The concern with ref-counting schemes is that one may forget to release memory.
  • I think the key observation here is that when LView gets destroyed we already have to keep track of it because we need to call ngDestroy life-cycle events. This means that once destroyed there is no way to resurrect the data. Piggybacking to clean up ACTIVE_LVIEWS would be a minor cost.
  • There is still a concern that someone may detach ViewRef and than just drop it without proper ngDestroy. This would create a memory leak, but a good argument can be made that it is app error since the ViewRef could be re-added and dropping it creates an unreachable references.

NOTE:

  • Did not review the code in depth only looked at the main algorithm for tracking ids
  • Will need a way to deal with holes in the ACTIVE_LVIEWS and also to ensure that we are not creating holey arrays.

@josephperrott josephperrott added the area: core Issues related to the framework runtime label Mar 23, 2021
@ngbot ngbot bot added this to the Backlog milestone Mar 23, 2021
**Note:** this is mostly an exploratory PR to check whether the approach actually works and to start a discussion. There's more work to be done to make it mergeable (e.g. it introduces new circular dependencies, there are no unit tests etc.).

Explores adding a unique ID to each `LView` which is stored in `__ngContext__`, rather than the LView itself. This will avoid worsening memory leaks where a detached element is retained along with its `LView`.

Also reworks `LContext` to store the ID of its `LView`, rather than the `LView` itself, because the `LContext` can also be stored in `__ngContext__`.
@crisbeto
Copy link
Member Author

Closing in favor of #41358.

@crisbeto crisbeto closed this Mar 28, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants