8000 perf(core): Avoid traversal when `markForCheck` is called on a view being refreshed by atscott · Pull Request #54347 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

perf(core): Avoid traversal when markForCheck is called on a view being refreshed #54347

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

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/core/src/application/application_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,5 @@ export function whenStable(applicationRef: ApplicationRef): Promise<void> {


function shouldRecheckView(view: LView): boolean {
return requiresRefreshOrTraversal(view);
// TODO(atscott): We need to support rechecking views marked dirty again in afterRender hooks
// in order to support the transition to zoneless. b/308152025
/* || !!(view[FLAGS] & LViewFlags.Dirty); */
return requiresRefreshOrTraversal(view) || !!(view[FLAGS] & LViewFlags.Dirty);
}
2 changes: 2 additions & 0 deletions packages/core/src/render3/instructions/change_detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export function refreshView<T>(
!isInCheckNoChangesPass && lView[ENVIRONMENT].inlineEffectRunner?.flush();


lView[FLAGS] |= LViewFlags.ExecutingRefresh;
// Start component reactive context
// - We might already be in a reactive context if this is an embedded view of the host.
// - We might be descending into a view that needs a consumer.
Expand Down Expand Up @@ -277,6 +278,7 @@ export function refreshView<T>(
maybeReturnReactiveLViewConsumer(currentConsumer);
}
leaveView();
lView[FLAGS] &= ~LViewFlags.ExecutingRefresh;
}
}

Expand Down
< 10000 span class="sr-only"> 12 changes: 8 additions & 4 deletions packages/core/src/render3/instructions/mark_view_dirty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ import {getLViewParent} from '../util/view_utils';
* an additional time to get the root view and schedule a tick on it.
*
* @param lView The starting LView to mark dirty
* @returns the root LView
*/
export function markViewDirty(lView: LView): LView|null {
export function markViewDirty(lView: LView) {
lView[ENVIRONMENT].changeDetectionScheduler?.notify();
while (lView) {
// If we're already refreshing the view, we should not mark it or ancestors dirty.
// This is `ExpressionHasChangedAfterItWasCheckedError` if any bindings have actually changed
// and completely unnecessary to refresh views again if there weren't any updated bindings.
if (lView[FLAGS] & LViewFlags.ExecutingRefresh) {
return;
}
lView[FLAGS] |= LViewFlags.Dirty;
const parent = getLViewParent(lView);
// Stop traversing up as soon as you find a root view that wasn't attached to any container
if (isRootView(lView) && !parent) {
return lView;
return;
}
// continue otherwise
lView = parent!;
}
return null;
}
7 changes: 6 additions & 1 deletion packages/core/src/render3/interfaces/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,15 @@ export const enum LViewFlags {
*/
HasChildViewsToRefresh = 1 << 13,

/**
* Flag used to indicate that we are current running `refreshView` on the LView.
*/
ExecutingRefresh = 1 << 14,

/**
* This is the count of the bits the 1 was shifted above (base 10)
*/
IndexWithinInitPhaseShift = 14,
IndexWithinInitPhaseShift = 15,

/**
* Index of the current init phase on last 21 bits
Expand Down
3 changes: 1 addition & 2 deletions packages/core/test/acceptance/after_render_hook_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,7 @@ describe('after render hooks', () => {
const appRef = TestBed.inject(ApplicationRef);
appRef.attachView(fixture.componentRef.hostView);
appRef.tick();
// TODO(atscott): We need to support this for zoneless to succeed
expect(fixture.nativeElement.innerText).toBe('0');
expect(fixture.nativeElement.innerText).toBe('1');
});

it('throws error when causing infinite updates', () => {
Expand Down
0