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

perf(ivy): attempt to coalesce listeners only in presence of directives #29859

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/core/src/render3/instructions/listener.ts
Expand Up @@ -14,7 +14,7 @@ import {GlobalTargetResolver, RElement, Renderer3, isProceduralRenderer} from '.
import {CLEANUP, FLAGS, LView, LViewFlags, RENDERER, TVIEW} from '../interfaces/view';
import {assertNodeOfPossibleTypes} from '../node_assert';
import {getLView, getPreviousOrParentTNode} from '../state';
import {getComponentViewByIndex, getNativeByTNode, unwrapRNode} from '../util/view_utils';
import {getComponentViewByIndex, getNativeByTNode, hasDirectives, unwrapRNode} from '../util/view_utils';
import {BindingDirection, generatePropertyAliases, getCleanup, handleError, loadComponentRenderer, markViewDirty} from './shared';

/**
Expand Down Expand Up @@ -132,12 +132,16 @@ function listenerInternal(
// In order to have just one native event handler in presence of multiple handler functions,
// we just register a first handler function as a native event listener and then chain
// (coalesce) other handler functions on top of the first native handler function.
//
let existingListener = null;
// Please note that the coalescing described here doesn't happen for events specifying an
// alternative target (ex. (document:click)) - this is to keep backward compatibility with the
// view engine.
const existingListener =
eventTargetResolver ? null : findExistingListener(lView, eventName, tNode.index);
// Also, we don't have to search for existing listeners is there are no directives
Copy link
Contributor

@kara kara Apr 12, 2019

Choose a reason for hiding this comment

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

Fix now or in follow-up (not gating)

Suggested change
// Also, we don't have to search for existing listeners is there are no directives
// Also, we don't have to search for existing listeners if there are no directives

// matching on a given node as we can't register multiple event handlers for the same event in
// a template (this would mean having duplicate attributes).
if (!eventTargetResolver && hasDirectives(tNode)) {
existingListener = findExistingListener(lView, eventName, tNode.index);
}
if (existingListener !== null) {
// Attach a new listener at the head of the coalesced listeners list.
(<any>listenerFn).__ngNextListenerFn__ = (<any>existingListener).__ngNextListenerFn__;
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/render3/util/view_utils.ts
Expand Up @@ -131,6 +131,13 @@ export function getNativeByTNode(tNode: TNode, hostView: LView): RNode {
return unwrapRNode(hostView[tNode.index]);
}

/**
* A helper function that returns `true` if a given `TNode` has any matching directives.
*/
export function hasDirectives(tNode: TNode): boolean {
return tNode.directiveEnd > tNode.directiveStart;
}

export function getTNode(index: number, view: LView): TNode {
ngDevMode && assertGreaterThan(index, -1, 'wrong index for TNode');
ngDevMode && assertLessThan(index, view[TVIEW].data.length, 'wrong index for TNode');
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Expand Up @@ -848,6 +848,9 @@
{
"name": "hasClassInput"
},
{
"name": "hasDirectives"
},
{
"name": "hasParentInjector"
},
Expand Down