Skip to content

fix(compiler): optimize away unnecessary restore/reset view calls#66632

Merged
amishne merged 1 commit intoangular:mainfrom
crisbeto:66286/restore-reset-view-optimization
Jan 20, 2026
Merged

fix(compiler): optimize away unnecessary restore/reset view calls#66632
amishne merged 1 commit intoangular:mainfrom
crisbeto:66286/restore-reset-view-optimization

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Jan 19, 2026

When producing a listener, the template pipeline does the following in separate phases:

  1. Generates all the variables available within its scope.
  2. Adds restoreView and resetView calls if there are any references to local variables (e.g. @let or local refs).
  3. Optimizes away the variables that aren't used.

This means that we can end up in a situation where the references to the variables in the scope no longer exist, but we still enter and leave the view as if they're there which is unnecessary.

These changes add a simple optimization pass that looks specifically for the pattern of a restoreView followed by a return resetView(expr). Furthermore, by changing the order of some optimizations, we're able to drop the getCurrentView variable as well.

Fixes #66286.

@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Jan 19, 2026
@angular-robot angular-robot bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Jan 19, 2026
@ngbot ngbot bot added this to the Backlog milestone Jan 19, 2026
@crisbeto crisbeto force-pushed the 66286/restore-reset-view-optimization branch from 9373cc1 to 8597505 Compare January 19, 2026 08:06
@eneajaho
Copy link
Contributor

I think it fixes #66286 instead right?

When producing a listener, the template pipeline does the following in separate phases:
1. Generates all the variables available within its scope.
2. Adds `restoreView` and `resetView` calls if there are any referenced to local variables (e.g. `@let` or local refs).
3. Optimizes away the variables that aren't used.

This means that we can end up in a situation where the references to the variables in the scope no longer exist, but we still enter and leave the view as if they're there which is unnecessary.

These changes add a simple optimization pass that looks specifically for the pattern of a `restoreView` followed by a `return resetView(expr)`. Furthermore, by changing the order of some optimizations, we're able to drop the `getCurrentView` variable as well.

Fixes angular#66286.
@crisbeto crisbeto force-pushed the 66286/restore-reset-view-optimization branch from 8597505 to e390b87 Compare January 19, 2026 08:19
@crisbeto
Copy link
Member Author

Yep, fixed.

@crisbeto crisbeto added action: global presubmit The PR is in need of a google3 global presubmit and removed action: global presubmit The PR is in need of a google3 global presubmit labels Jan 19, 2026
@crisbeto
Copy link
Member Author

Passing TGP

@crisbeto crisbeto marked this pull request as ready for review January 19, 2026 13:58
@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release and removed action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Jan 20, 2026
@amishne amishne merged commit ce80136 into angular:main Jan 20, 2026
25 checks passed
@amishne
Copy link
Contributor

amishne commented Jan 20, 2026

This PR was merged into the repository. The changes were merged into the following branches:

@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 Feb 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template pipeline leaves behind unnecessary restore/reset view calls in the presence of @let

4 participants