Bug 142752 - [Mac][iOS] setSharedTimerFireInterval() / stopSharedTimer() are expensive
Summary: [Mac][iOS] setSharedTimerFireInterval() / stopSharedTimer() are expensive
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Platform (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on: 143025
Blocks:
  Show dependency treegraph
 
Reported: 2015-03-16 15:09 PDT by Chris Dumez
Modified: 2015-03-24 16:10 PDT (History)
9 users (show)

See Also:


Attachments
Patch (6.32 KB, patch)
2015-03-16 15:19 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews100 for mac-mavericks (652.12 KB, application/zip)
2015-03-16 15:42 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews104 for mac-mavericks-wk2 (632.60 KB, application/zip)
2015-03-16 15:44 PDT, Build Bot
no flags Details
Patch (7.08 KB, patch)
2015-03-16 17:27 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews100 for mac-mavericks (523.62 KB, application/zip)
2015-03-16 18:14 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews104 for mac-mavericks-wk2 (573.52 KB, application/zip)
2015-03-16 18:22 PDT, Build Bot
no flags Details
Patch (10.29 KB, patch)
2015-03-16 20:33 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (10.38 KB, patch)
2015-03-16 20:52 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (10.41 KB, patch)
2015-03-16 21:00 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (10.88 KB, patch)
2015-03-16 21:03 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews101 for mac-mavericks (524.13 KB, application/zip)
2015-03-16 21:52 PDT, Build Bot
no flags Details
Patch (11.35 KB, patch)
2015-03-16 22:15 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (11.71 KB, patch)
2015-03-16 22:29 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (11.75 KB, patch)
2015-03-16 22:45 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (12.22 KB, patch)
2015-03-16 22:49 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (32.35 KB, patch)
2015-03-17 11:39 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews102 for mac-mavericks (532.73 KB, application/zip)
2015-03-17 12:29 PDT, Build Bot
no flags Details
Patch (32.38 KB, patch)
2015-03-17 13:28 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (38.24 KB, patch)
2015-03-17 13:54 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (38.26 KB, patch)
2015-03-17 14:31 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2015-03-16 15:09:40 PDT
setSharedTimerFireInterval() / stopSharedTimer() are expensive on Mac and iOS on pages using a lot of timers.

For example, on bing.com, ~15.4% of the CPU time is spent in setSharedTimerFireInterval() and ~14.7% of the CPU time is spent in stopSharedTimer(). The expensive calls are CFRunLoopAddTimer (11.4%), CFRunLoopTimerInvalidate (14.1%), CFRunLoopTimerCreate (3.3%).

The issue is that we keep creating, adding to run loop modes, and then destroying the sharedTimer for each firing event. This is very expensive.
In such case, the CFRunLoopTimerRef documentation advises to
"""
... create a repeating timer with an initial firing time in the distant future (or the initial firing time) and a very large repeat interval—on the order of decades or more—and add it to all the necessary run loop modes. Then, when you know when the timer should fire next, you reset the firing time with CFRunLoopTimerSetNextFireDate, perhaps from the timer’s own callback function. This technique effectively produces a reusable, asynchronous timer.
""" [1].

Doing so greatly decreases CPU using in setSharedTimerFireInterval() (15.4% -> 4.6%) and stopSharedTimer() (14.6% -> 8.6%).

[1] https://developer.apple.com/library/prerelease/ios/documentation/CoreFoundation/Reference/CFRunLoopTimerRef/index.html#//apple_ref/c/func/CFRunLoopTimerSetNextFireDate

Radar: <rdar://problem/20176731>.
Comment 1 Chris Dumez 2015-03-16 15:19:34 PDT
Created attachment 248753 [details]
Patch
Comment 2 Build Bot 2015-03-16 15:41:56 PDT
Comment on attachment 248753 [details]
Patch

Attachment 248753 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.appspot.com/results/6626488874958848

New failing tests:
inspector-protocol/dom-debugger/node-removed.html
Comment 3 Build Bot 2015-03-16 15:42:00 PDT
Created attachment 248758 [details]
Archive of layout-test-results from ews100 for mac-mavericks

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews100  Port: mac-mavericks  Platform: Mac OS X 10.9.5
Comment 4 Build Bot 2015-03-16 15:44:49 PDT
Comment on attachment 248753 [details]
Patch

Attachment 248753 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.appspot.com/results/5011494735446016

New failing tests:
inspector-protocol/dom-debugger/node-removed.html
Comment 5 Build Bot 2015-03-16 15:44:53 PDT
Created attachment 248759 [details]
Archive of layout-test-results from ews104 for mac-mavericks-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews104  Port: mac-mavericks-wk2  Platform: Mac OS X 10.9.5
Comment 6 Alexey Proskuryakov 2015-03-16 15:50:42 PDT
Oh wait, is it this patch that actually breaks inspector-protocol/dom-debugger/node-removed.html? I'm no longer sure if it was for a good reason that I disabled it in bug 142753.
Comment 7 Chris Dumez 2015-03-16 15:57:21 PDT
The attached results say the test fails with a timeout, it does not say anything about  a crash.
Comment 8 Alexey Proskuryakov 2015-03-16 16:03:26 PDT
Yes, that's what it says. But if you look at the bot, there are crash logs.
Comment 9 Chris Dumez 2015-03-16 17:27:23 PDT
Created attachment 248771 [details]
Patch
Comment 10 Antti Koivisto 2015-03-16 17:47:31 PDT
I already tried to do this in bug 109765. Didn't stick.
Comment 11 Antti Koivisto 2015-03-16 17:49:54 PDT
That patch also tries (and fails) to deal with nested runloops.
Comment 12 Build Bot 2015-03-16 18:14:50 PDT
Comment on attachment 248771 [details]
Patch

Attachment 248771 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.appspot.com/results/5282230683303936

New failing tests:
inspector-protocol/dom-debugger/node-removed.html
Comment 13 Build Bot 2015-03-16 18:14:53 PDT
Created attachment 248773 [details]
Archive of layout-test-results from ews100 for mac-mavericks

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews100  Port: mac-mavericks  Platform: Mac OS X 10.9.5
Comment 14 Chris Dumez 2015-03-16 18:15:28 PDT
(In reply to comment #11)
> That patch also tries (and fails) to deal with nested runloops.

Thanks for the pointer Antti. I wasn't aware this was previously tried. I'll still try and make it work as the performance gain seems worth it.
Comment 15 Build Bot 2015-03-16 18:22:01 PDT
Comment on attachment 248771 [details]
Patch

Attachment 248771 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.appspot.com/results/4816377592414208

New failing tests:
inspector-protocol/dom-debugger/node-removed.html
Comment 16 Build Bot 2015-03-16 18:22:07 PDT
Created attachment 248775 [details]
Archive of layout-test-results from ews104 for mac-mavericks-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews104  Port: mac-mavericks-wk2  Platform: Mac OS X 10.9.5
Comment 17 Chris Dumez 2015-03-16 20:33:05 PDT
Created attachment 248796 [details]
Patch
Comment 18 Chris Dumez 2015-03-16 20:52:28 PDT
Created attachment 248798 [details]
Patch
Comment 19 Chris Dumez 2015-03-16 21:00:37 PDT
Created attachment 248799 [details]
Patch
Comment 20 Chris Dumez 2015-03-16 21:03:18 PDT
Created attachment 248800 [details]
Patch
Comment 21 Build Bot 2015-03-16 21:52:04 PDT
Comment on attachment 248800 [details]
Patch

Attachment 248800 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.appspot.com/results/5369388454641664

New failing tests:
fast/events/scroll-event-during-modal-dialog.html
Comment 22 Build Bot 2015-03-16 21:52:09 PDT
Created attachment 248806 [details]
Archive of layout-test-results from ews101 for mac-mavericks

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews101  Port: mac-mavericks  Platform: Mac OS X 10.9.5
Comment 23 Chris Dumez 2015-03-16 22:15:00 PDT
Created attachment 248817 [details]
Patch
Comment 24 Chris Dumez 2015-03-16 22:29:48 PDT
Created attachment 248824 [details]
Patch
Comment 25 Chris Dumez 2015-03-16 22:45:43 PDT
Created attachment 248825 [details]
Patch
Comment 26 Chris Dumez 2015-03-16 22:49:51 PDT
Created attachment 248826 [details]
Patch
Comment 27 Darin Adler 2015-03-16 23:43:10 PDT
Comment on attachment 248826 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=248826&action=review

Seems really unfortunate that the iOS and Mac versions of this don’t share more code.

I wonder if there is a way to do even better on this, cutting down on the repeated calls to CFRunLoopTimerSetNextFireDate somehow.

> Source/WebCore/platform/SharedTimer.h:79
> +#if PLATFORM(MAC) || PLATFORM(IOS)

Could use PLATFORM(COCOA) for this. We often do. Or we could just put empty removeSharedTimer functions into the other platform SharedTimer implementation files. Seems worthwhile to avoid the #if if at all possible.
Comment 28 Chris Dumez 2015-03-17 11:39:45 PDT
Created attachment 248856 [details]
Patch
Comment 29 Build Bot 2015-03-17 12:29:47 PDT
Comment on attachment 248856 [details]
Patch

Attachment 248856 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.appspot.com/results/4674012311453696

New failing tests:
fast/events/scroll-event-during-modal-dialog.html
Comment 30 Build Bot 2015-03-17 12:29:52 PDT
Created attachment 248860 [details]
Archive of layout-test-results from ews102 for mac-mavericks

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews102  Port: mac-mavericks  Platform: Mac OS X 10.9.5
Comment 31 Chris Dumez 2015-03-17 13:28:55 PDT
Created attachment 248864 [details]
Patch
Comment 32 Chris Dumez 2015-03-17 13:54:07 PDT
Created attachment 248867 [details]
Patch
Comment 33 Chris Dumez 2015-03-17 14:31:55 PDT
Created attachment 248870 [details]
Patch
Comment 34 Antti Koivisto 2015-03-17 15:54:14 PDT
Comment on attachment 248870 [details]
Patch

r=me, nice

Did you test on iOS?
Comment 35 Chris Dumez 2015-03-17 15:56:40 PDT
(In reply to comment #34)
> Comment on attachment 248870 [details]
> Patch
> 
> r=me, nice
> 
> Did you test on iOS?

Well, I tested a few sites. I initially wrote this optimization for bing.com on iOS.
Comment 36 WebKit Commit Bot 2015-03-17 16:43:22 PDT
Comment on attachment 248870 [details]
Patch

Clearing flags on attachment: 248870

Committed r181671: <http://trac.webkit.org/changeset/181671>
Comment 37 WebKit Commit Bot 2015-03-17 16:43:29 PDT
All reviewed patches have been landed.  Closing bug.