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

Make sure we call events listeners in the order they are registered (fix #4743) #4769

Merged
merged 1 commit into from
Aug 2, 2016

Conversation

yohanboniface
Copy link
Member

Attempt to fix #4743.

This makes sure the event listeners are called in the order they are registered.
The main change is not to store the events by context anymore (this being a hash, thus having an impredictable loop order).
The drawback being that when adding a listener the cost of finding duplicates is a higher in the case the current feature as a lot of listeners for a given event type.

This is the output of the event-perf debug page for master:
screenshot from 2016-07-30 20-44-32

And the same for the current branch:
screenshot from 2016-07-30 20-44-14

For comparison, this is for RC1:
screenshot from 2016-07-30 20-49-33

So as we can see, the current branch perform a bit less than the rc2 when adding twice the same function with same context (and still worst than rc1 that does not try to find duplicates), but this much better than rc1 on fire.
And I think keeping the listeners order is not negotiable.
We should keep in mind that the event-perf debug page has he bias of being a bit synthetical and not real use cases.

One option could be that we do no check for duplicates when adding events as it was until rc2.

As a side result, the code is also more simple, and thus more readable, as it is.

return;
}
}

listeners.push(newListener);
typeListeners.listeners.push(newListener);
Copy link
Member

Choose a reason for hiding this comment

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

Why not listeners here? It looks like listeners === typeListeners.listeners ?

Copy link
Member Author

Choose a reason for hiding this comment

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

No good reason ;)
I removed listeners local variable at first, and then apparently forgot to put it back here.
Fixing it, thanks! :)

@perliedman
Copy link
Member

👍 LGTM!

The performance regression looks acceptable given that we win totally predictable and easy to understand call order for listeners.

@mourner
Copy link
Member

mourner commented Aug 2, 2016

Merging this, but I'm still concerned because benchmarks may not cover some common use cases that can substantially regress from this (because of the quadratic behavior). But let's keep master in a working condition and see if we can improve on performance after that.

@fab1an
Copy link
Contributor

fab1an commented Aug 26, 2016

This should fix: #4475 as well.

@@ -96,30 +96,22 @@ L.Evented = L.Class.extend({
var typeListeners = this._events[type];
if (!typeListeners) {
typeListeners = {
listeners: {},
listeners: [],
count: 0
Copy link
Contributor

@fab1an fab1an Aug 26, 2016

Choose a reason for hiding this comment

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

I think this count variable can be removed and replaced by listeners.length.

Actually I think the whole typeListeners object can be replaced by the listeners array itself now.

Copy link
Member

Choose a reason for hiding this comment

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

@fab1an you're correct! We did away with typeListeners yesterday! 017d29c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vector rendering regression in rc2
4 participants