Skip to content

Firing run loop on mousemove unnecessary? #11540

Closed

Description

@asakusuma

Looks like a new run loop is kicked off every time the mouse moves.

Is this really necessary? @stefanpenner suggested that we might instead have components opt-in to needing this, instead of assuming we need a run loop on mouse movement.

Activity

  1. stefanpenner commented on Jun 23, 2015

    @stefanpenner
    Member

    It's only happening just in-case someone has a mouse related handler on one of the rendered components. If instead components informed the event dispatcher the events they need on render and no longer need on un render. We could remove this, instead only listening to exactly what is required.

    The mouse/touch events introduce an unfortunate cost, especially when they are not frequently used.

  2. asakusuma commented on Jun 24, 2015

    @asakusuma
    ContributorAuthor

    Is there a way to infer this from the component, or would the component need to manually declare what is needs?

    If the latter, we'd need to choose the set of events that are listened for by default, and the set of events you need to manually opt in to.

  3. stefanpenner commented on Jun 24, 2015

    @stefanpenner
    Member

    Is there a way to infer this from the component

    yes, https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/views/component.js#L223 is how one declares interest in an event from a component.

  4. asakusuma commented on Jun 24, 2015

    @asakusuma
    ContributorAuthor

    So then we could just ref count this on component creation and the decrement on destruction, and then add and remove this listener as needed?

  5. chadhietala commented on Jun 24, 2015

    @chadhietala
    Contributor

    I think this gets tricky with improved actions, because you can declare event handlers in the template.

    <input onmouseover={{action 'reasonable/unreasonable'}} />
    

    You completely loose the ability to introspect the class.

  6. rwjblue commented on Jun 24, 2015

    @rwjblue
    Member

    @chadhietala - In that scenario, you are not actually using the EventDispatcher (or Ember event bubbling at all), you are registering a custom onmouseover handler.

  7. chadhietala commented on Jun 24, 2015

    @chadhietala
    Contributor

    @rwjblue So it doesn't cause a runloop? Is this the same for the old on= syntax as well?

  8. rwjblue commented on Jun 24, 2015

    @rwjblue
    Member

    @chadhietala - No, {{action "foo" on="mouseover"}} would definitely go through EventDispatcher, and would have the issue you are cautioning against if we didn't take it into account.

  9. stefanpenner commented on Jun 24, 2015

    @stefanpenner
    Member

    Yup it causes a run loop, but it's one that you want. Since you are absolutely invoking ember code. This is much better then all the preemptive handlers that run for. All mouse events regardless of if a handler is present.

    An interim step is actually to skip the run loop for events that don't find a handler.

  10. stefanpenner commented on Jun 24, 2015

    @stefanpenner
    Member

    https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/system/event_dispatcher.js#L234-L242 could likely detect if the given view has such a handler, and only invoke the run-loop if it is about to enter user code.

    To handle bubbling correctly, it may need to be slightly more creative.

    This approach I believe will be the easiest, no ref counting needed. Sure we sill need to attach event listeners to everything, but we only bother invoking a run-loop if we truely are about to invoke code that requires it.

    I suspect this will cure the original concern which prompted this issue.

  11. asakusuma commented on Jun 24, 2015

    @asakusuma
    ContributorAuthor

    So to make sure I understand this, the idea is to:

    • Check the immediate view to see if actually has a handler for the given event
    • If handler present, proceed
    • If not, repeat on parent, if no parent, no-op

    This sounds like it will address my issue. However, what is more expensive, traversing the view hierarchy looking for handlers, or executing a no-op run loop?

  12. stefanpenner commented on Jun 24, 2015

    @stefanpenner
    Member

    We traverse it anyways

  13. runspired commented on Jun 24, 2015

    @runspired
    Contributor

    The mouse/touch events introduce an unfortunate cost, especially when they are not frequently used.

    jQuery fast paths event handling for many events, I think Ember should likely look into doing so too, in addition to any inquiry into using useCapture for events. Therese a huge perf win being able to delegate directly instead of bubbling, and the current implementation not only needs to bubble, but needs to run multiple checks at each view/component layer for handlers.

    I'd put the overall cost on this as an extra .5-4ms / touchmove or mousemove event that's being used to manipulate position on screen (much of which depends on how many layers it needs to bubble through etc.). Not horrendous, but definitely expensive when lots of such events are involved.

    (Edit: that cost I peg above is a rough eyeball of the difference between using touchmove through Ember and using it as a direct handler on the component)

  14. stefanpenner commented on Jun 24, 2015

    @stefanpenner
    Member

    I believe we are limited by our ie9 support and a true bubbling phase. Maybe not?

    Would love for someone to dive into this area an propose some improvements. Enumerating cost/benefit would also be handy

  15. runspired commented on Jun 24, 2015

    @runspired
    Contributor

    IE9 allows for useCapture, IE8 support doesn't.

  16. 20 remaining items

  17. runspired commented on Aug 11, 2015

    @runspired
    Contributor

    I did some perf testing on useCapture using a mechanism that would allow us to maintain ember's current eventing mechanics for the moment without changing any expected behavior: http://jsperf.com/event-delegation-with-capture-vs-bubble

    TL;DR ~45% performance boost across the board.

  18. stefanpenner commented on Aug 13, 2015

    @stefanpenner
    Member

    @rwjblue closed in favor of rfc/implementation?

  19. runspired commented on Aug 13, 2015

    @runspired
    Contributor

    your commit doesn't fix the run loop join, only the ability to turn mousemove off

  20. runspired commented on Aug 13, 2015

    @runspired
    Contributor
  21. stefanpenner commented on Aug 13, 2015

    @stefanpenner
    Member

    Maybe we need to bundle up some of the talking points and push some work for capture phase + improved event dispatcher.

    Having vague hard to action issues is annoying.

  22. runspired commented on Aug 13, 2015

    @runspired
    Contributor

    @stefanpenner I have a branch where I am working on this actually, I think it's been added to your Friday schedule as a talking point

  23. stefanpenner commented on Aug 13, 2015

    @stefanpenner
    Member

    Awesome. Can you link it. Or submit a WIP for review?

  24. runspired commented on Aug 13, 2015

    @runspired
    Contributor

    PR coming as soon as I finish off the walker. Will also be adding a second flag for resolution caching (e.g. from element to handler) so that repeated firing is super fast.

    https://github.com/runspired/ember.js/commit/f682df07fbb6ba92d13ee618b19f764bf85262f9

  25. rwjblue commented on Aug 13, 2015

    @rwjblue
    Member

    @stefanpenner - It is also on our agenda for Friday.

  26. rwjblue commented on Aug 13, 2015

    @rwjblue
    Member

    Sorry I was trying to reference this issue, not close it.

  27. added a commit that references this issue on Sep 9, 2015
    60ab022
  28. added a commit that references this issue on Sep 12, 2015
    eb4c478
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions