Bug 169236

Summary: Port DOMTimer from std::chrono::milliseconds to WTF::Seconds type
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: DOMAssignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: barraclough, cdumez, commit-queue, dbates, esprehn+autocc, fpizlo, kangil.han, simon.fraser
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 169213    
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch none

Description Chris Dumez 2017-03-06 16:27:21 PST
Port DOMTimer from std::chrono::milliseconds to WTF::Seconds type.
Comment 1 Chris Dumez 2017-03-06 16:39:18 PST
Created attachment 303581 [details]
Patch
Comment 2 Chris Dumez 2017-03-06 16:46:49 PST
Created attachment 303583 [details]
Patch
Comment 3 Simon Fraser (smfr) 2017-03-06 16:48:24 PST
See bug 165079
Comment 4 Chris Dumez 2017-03-06 16:53:19 PST
(In reply to comment #3)
> See bug 165079

Hmm, I wasn't aware of this change. Sad that it has not landed since last year.
How do you want to proceed? My patch should be building now and has more limited scope apparently.
Comment 5 Chris Dumez 2017-03-06 17:00:49 PST
Created attachment 303586 [details]
Patch
Comment 6 Chris Dumez 2017-03-06 17:01:16 PST
Should take care of the debug build.
Comment 7 Simon Fraser (smfr) 2017-03-06 17:06:20 PST
Comment on attachment 303583 [details]
Patch

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

> Source/WebCore/page/DOMTimer.cpp:56
> +static const Seconds maxIntervalForUserGestureForwarding { 1 }; // One second matches Gecko.
> +static const Seconds minIntervalForNonUserObservableChangeTimers { 1 }; // Empirically determined to maximize battery life.

1_s

> Source/WebCore/page/DOMTimer.cpp:417
> +    Seconds interval = std::max(Seconds::fromMilliseconds(1), m_originalInterval);

1_ms

> Source/WebCore/page/DOMTimer.cpp:441
> +    auto randomizedOffsetMS = static_cast<long long>(alignmentIntervalMS * randomizedProportion);
> +    auto adjustedFireTimeMS = static_cast<long long>(fireTime.milliseconds()) - randomizedOffsetMS;

Are you keeping these as long long explicitly for discretization? I used MonotonicTime for the adjustedFireTime.

> Source/WebCore/page/DOMTimer.h:48
> +    static Seconds defaultMinimumInterval() { return Seconds::fromMilliseconds(4); }

4_ms

> Source/WebCore/page/DOMTimer.h:50
> +    static Seconds hiddenPageAlignmentInterval() { return Seconds { 1 }; }

1_s

> Source/WebCore/page/Page.cpp:1364
> +            m_domTimerAlignmentInterval = Seconds { monotonicallyIncreasingTime() - m_timerThrottlingStateLastChangedTime };

Use MonotonicTime, not monotonicallyIncreasingTime()

> Source/WebCore/page/Page.h:718
> +    Seconds m_domTimerAlignmentIntervalIncreaseLimit { 0 };

No need to initialize a Seconds.

> Source/WebCore/platform/Timer.cpp:385
> +        if (auto newAlignedTime = alignedFireTime(Seconds { newTime }))

Are we doing Seconds { newTime } now, instead of just Seconds(newTime)?

> Source/WebCore/testing/Internals.cpp:1039
> +    return !!timer->alignedFireTime(Seconds { 0 });

0_s
Comment 8 Chris Dumez 2017-03-06 18:28:30 PST
Comment on attachment 303583 [details]
Patch

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

>> Source/WebCore/page/DOMTimer.cpp:56
>> +static const Seconds minIntervalForNonUserObservableChangeTimers { 1 }; // Empirically determined to maximize battery life.
> 
> 1_s

Oh, I did not know about these suffixes. I'll use them everywhere.

>> Source/WebCore/page/DOMTimer.cpp:441
>> +    auto adjustedFireTimeMS = static_cast<long long>(fireTime.milliseconds()) - randomizedOffsetMS;
> 
> Are you keeping these as long long explicitly for discretization? I used MonotonicTime for the adjustedFireTime.

I was trying to keep the code as close to the existing one as possible because it is a bit complicated and I worry about introducing bugs. For example, the code used to deal with milliseconds and I would need to look into it more to make sure it does the same thing with seconds.

>> Source/WebCore/page/Page.cpp:1364
>> +            m_domTimerAlignmentInterval = Seconds { monotonicallyIncreasingTime() - m_timerThrottlingStateLastChangedTime };
> 
> Use MonotonicTime, not monotonicallyIncreasingTime()

I did not know about MonotonicTime, I'll use it here.

>> Source/WebCore/page/Page.h:718
>> +    Seconds m_domTimerAlignmentIntervalIncreaseLimit { 0 };
> 
> No need to initialize a Seconds.

Ok.

>> Source/WebCore/platform/Timer.cpp:385
>> +        if (auto newAlignedTime = alignedFireTime(Seconds { newTime }))
> 
> Are we doing Seconds { newTime } now, instead of just Seconds(newTime)?

Darin coding style :)
Comment 9 Chris Dumez 2017-03-06 19:53:43 PST
Created attachment 303606 [details]
Patch
Comment 10 Chris Dumez 2017-03-07 08:24:44 PST
Created attachment 303655 [details]
Patch
Comment 11 Simon Fraser (smfr) 2017-03-07 08:30:03 PST
Comment on attachment 303655 [details]
Patch

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

> Source/WebCore/page/DOMTimer.cpp:404
> +        LOG(DOMTimers, "%p - Updating DOMTimer's repeat interval from %f ms to %f ms due to throttling.", this, previousInterval.milliseconds(), m_currentTimerInterval.milliseconds());

Maybe %.2f in these logs for readability.

> Source/WebCore/page/DOMTimer.cpp:442
> +    Seconds randomizedOffset = alignmentInterval * randomizedProportion;
> +    Seconds adjustedFireTime = fireTime - randomizedOffset;
>      return adjustedFireTime - (adjustedFireTime % alignmentInterval) + alignmentInterval + randomizedOffset;

Did you do some runtime logging/debugging to make sure that this code still results in good alignment?
Comment 12 Chris Dumez 2017-03-07 08:31:02 PST
(In reply to comment #11)
> Comment on attachment 303655 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=303655&action=review
> 
> > Source/WebCore/page/DOMTimer.cpp:404
> > +        LOG(DOMTimers, "%p - Updating DOMTimer's repeat interval from %f ms to %f ms due to throttling.", this, previousInterval.milliseconds(), m_currentTimerInterval.milliseconds());
> 
> Maybe %.2f in these logs for readability.

Ok.

> 
> > Source/WebCore/page/DOMTimer.cpp:442
> > +    Seconds randomizedOffset = alignmentInterval * randomizedProportion;
> > +    Seconds adjustedFireTime = fireTime - randomizedOffset;
> >      return adjustedFireTime - (adjustedFireTime % alignmentInterval) + alignmentInterval + randomizedOffset;
> 
> Did you do some runtime logging/debugging to make sure that this code still
> results in good alignment?

Yes.
Comment 13 Chris Dumez 2017-03-07 08:32:14 PST
Created attachment 303658 [details]
Patch
Comment 14 Chris Dumez 2017-03-07 09:11:11 PST
Comment on attachment 303658 [details]
Patch

Clearing flags on attachment: 303658

Committed r213519: <http://trac.webkit.org/changeset/213519>
Comment 15 Chris Dumez 2017-03-07 09:11:17 PST
All reviewed patches have been landed.  Closing bug.