<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div><br>On May 22, 2016, at 10:46 PM, Michal Debski <<a href="mailto:m.debski@samsung.com">m.debski@samsung.com</a>> wrote:<br><br></div><blockquote type="cite"><div>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="IE=5" http-equiv="X-UA-Compatible">
<meta content="IE=5" http-equiv="X-UA-Compatible">
<meta name="GENERATOR" content="MSHTML 11.00.9600.18125">
<meta content="IE=5" http-equiv="X-UA-Compatible">
<meta content="IE=5" http-equiv="X-UA-Compatible">
<meta name="GENERATOR" content="ActiveSquare">
<meta content="IE=5" http-equiv="X-UA-Compatible">
<p>Hi,</p>
<p> </p>
<p>sorry but this really bugs me. Isn't this enough?</p>
<p> </p>
<p><span style="FONT-FAMILY: Courier New">namespace WTF {<br></span><span style="FONT-FAMILY: Courier New"><br>using nanoseconds = std::chrono::duration<double, std::nano>;<br>using microseconds = std::chrono::duration<double, std::micro>;<br>using milliseconds = std::chrono::duration<double, std::milli>;<br>using seconds = std::chrono::duration<double>;<br>using minutes = std::chrono::duration<double, std::ratio<60>>;<br>using hours = std::chrono::duration<double, std::ratio<3600>>;</span></p>
<p><span style="FONT-FAMILY: Courier New"><br>template <class Clock><br>std::chrono::time_point<Clock, seconds> now()<br>{<br> return Clock::now();<br>}<br></span></p></div></blockquote><div>Can you clarify how this returns fractional seconds?</div><div><br></div><div>Note that your code snippets are not enough to cover WebKit's use of clocks. For example we would need wall clock and monotonic clock classes with time_point types. If we have to significantly customize std::chrono and forbid some but not all of its API, then probably the total amount of code to do this would be comparable to writing our own Seconds/WallTime/MonotonicTime classes.</div><blockquote type="cite"><div><p><span style="FONT-FAMILY: Courier New"><br>}</span></p>
<p> </p>
<p>and forbid using std::chrono::clock::now()/durations with check-style. It seems like the best of both worlds. </p></div></blockquote><div>It's an interesting perspective. But I would find it confusing if we used a non-canonical kind of std::chrono. And I'm not convinced that the changes required to make this work are as simple as what you say. </div><blockquote type="cite"><div><p>Oh and the infinity:</p>
<p> </p>
<p><span style="FONT-FAMILY: Courier New">namespace std {<br>namespace chrono {<br><br>template<><br>struct duration_values<double> {<br> static constexpr double min() { return -std::numeric_limits<double>::infinity(); }<br> static constexpr double zero() { return .0; }<br> static constexpr double max() { return std::numeric_limits<double>::infinity(); }<br>};<br><br></span><span style="FONT-FAMILY: Courier New">}<br>}</span> </p></div></blockquote><blockquote type="cite">
<p>Best regards,<br>Michal Debski</p>
<p> </p>
<p>------- <b>Original Message</b> -------</p>
<p><b>Sender</b> : Filip Pizlo<<a href="mailto:fpizlo@apple.com">fpizlo@apple.com</a>></p>
<p><b>Date</b> : May 23, 2016 02:41 (GMT+01:00)</p>
<p><b>Title</b> : [webkit-dev] RFC: stop using std::chrono, go back to using doubles for time</p>
<p> </p>
<meta content="IE=5" http-equiv="X-UA-Compatible">
<div style="WORD-WRAP: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space">Hi everyone!
<div><br></div>
<div>I’d like us to stop using std::chrono and go back to using doubles for time. First I list the things that I think we wanted to get from std::chrono - the reasons why we started switching to it in the first place. Then I list some disadvantages of std::chrono that we've seen from fixing std::chrono-based code. Finally I propose some options for how to use doubles for time.</div>
<div><br></div>
<div><b>Why we switched to std::chrono</b></div>
<div><br></div>
<div>A year ago we started using std::chrono for measuring time. std::chrono has a rich typesystem for expressing many different kinds of time. For example, you can distinguish between an absolute point in time and a relative time. And you can distinguish between different units, like nanoseconds, milliseconds, etc.</div>
<div><br></div>
<div>Before this, we used doubles for time. std::chrono’s advantages over doubles are:</div>
<div><br></div>
<div><i>Easy to remember what unit is used:</i> We sometimes used doubles for milliseconds and sometimes for seconds. std::chrono prevents you from getting the two confused.</div>
<div><br></div>
<div><i>Easy to remember what kind of clock is used:</i> We sometimes use the monotonic clock and sometimes the wall clock (aka the real time clock). Bad things would happen if we passed a time measured using the monotonic clock to functions that expected time measured using the wall clock, and vice-versa. I know that I’ve made this mistake in the past, and it can be painful to debug.</div>
<div><br></div>
<div>In short, std::chrono uses compile-time type checking to catch some bugs.</div>
<div><br></div>
<div><b>Disadvantages of using std::chrono</b></div>
<div><br></div>
<div>We’ve seen some problems with std::chrono, and I think that the problems outweigh the advantages. std::chrono suffers from a heavily templatized API that results in template creep in our own internal APIs. std::chrono’s default of integers without overflow protection means that math involving std::chrono is inherently more dangerous than math involving double. This is particularly bad when we use time to speak about timeouts.</div>
<div><br></div>
<div><i>Too many templates:</i><b> </b>std::chrono uses templates heavily. It’s overkill for measuring time. This leads to verbosity and template creep throughout common algorithms that take time as an argument. For example if we use doubles, a method for sleeping for a second might look like sleepForSeconds(double). This works even if someone wants to sleep for a nanoseconds, since 0.000001 is easy to represent using a double. Also, multiplying or dividing a double by a small constant factor (1,000,000,000 is small by double standards) is virtually guaranteed to avoid any loss of precision. But as soon as such a utility gets std::chronified, it becomes a template. This is because you cannot have sleepFor(std::chrono::seconds), since that wouldn’t allow you to represent fractions of seconds. This brings me to my next point.</div>
<div><br></div>
<div><i>Overflow danger:</i><b> </b>std::chrono is based on integers and its math methods do not support overflow protection. This has led to serious bugs like <a href="https://bugs.webkit.org/show_bug.cgi?id=157924">https://bugs.webkit.org/show_bug.cgi?id=157924</a>. This cancels out the “remember what unit is used” benefit cited above. It’s true that I know what type of time I have, but as soon as I duration_cast it to another unit, I may overflow. The type system does not help! This is insane: std::chrono requires you to do more work when writing multi-unit code, so that you satisfy the type checker, but you still have to be just as paranoid around multi-unit scenarios. Forgetting that you have milliseconds and using it as seconds is trivially fixable. But if std::chrono flags such an error and you fix it with a duration_cast (as any std::chrono tutorial will tell you to do), you’ve just introduced an unchecked overflow and such unchecked overflows are known to cause bugs that manifest as pages not working correctly.</div>
<div><br></div>
<div>I think that doubles are better than std::chrono in multi-unit scenarios. It may be possible to have std::chrono work with doubles, but this probably implies us writing our own clocks. std::chrono’s default clocks use integers, not doubles. It also may be possible to teach std::chrono to do overflow protection, but that would make me so sad since using double means not having to worry about overflow at all.</div>
<div><br></div>
<div>The overflow issue is interesting because of its implications for how we do timeouts. The way to have a method with an optional timeout is to do one of these things:</div>
<div><br></div>
<div>- Use 0 to mean no timeout.</div>
<div>- Have one function for timeout and one for no timeout.</div>
<div>- Have some form of +Inf or INT_MAX to mean no timeout. This makes so much mathematical sense.</div>
<div><br></div>
<div>WebKit takes the +Inf/INT_MAX approach. I like this approach the best because it makes the most mathematical sense: not giving a timeout is exactly like asking for a timeout at time-like infinity. When used with doubles, this Just Works. +Inf is greater than any value and it gets preserved properly in math (+Inf * real = +Inf, so it survives gracefully in unit conversions; +Inf + real = +Inf, so it also survives absolute-to-relative conversions).</div>
<div><br></div>
<div>But this doesn’t work with std::chrono. The closest thing to +Inf is duration::max(), i.e. some kind of UINT_MAX, but this is guaranteed to overflow anytime it’s converted to a more precise unit of time (nanoseconds::max() converted to milliseconds is something bogus). It appears that std::chrono doesn’t have a good story for infinite timeout, which means that anyone writing a function that can optionally have a timeout is going to have a bad time. We have plenty of such functions in WebKit. For example, I’m not sure how to come up with a feel-good solution to <a href="https://bugs.webkit.org/show_bug.cgi?id=157937">https://bugs.webkit.org/show_bug.cgi?id=157937</a> so long as we use std::chrono.</div>
<div><br></div>
<div><b>Going back to doubles</b></div>
<div><br></div>
<div>Considering these facts, I propose that we switch back to using doubles for time. We can either simply revert to the way we used doubles before, or we can come up with some more sophisticated approach that blends the best of both worlds. I prefer plain doubles because I love simplicity.</div>
<div><br></div>
<div><i>Simply revert to our old ways:</i><i style="FONT-WEIGHT: bold"> </i>I like this approach the best because it involves only very simple changes. Prior to std::chrono, we used a double to measure time in seconds. It was understood that seconds was the default unit. We would use both monotonic and wall clocks, and we used double for both of them.</div>
<div><br></div>
<div><i>Come up with a new type system: </i>Having learned from std::chrono and doubles, it seems that the best typesystem for time would comprise three classes: Seconds, WallTime, and MonotonicTime. Seconds would be a class that holds a double and supports +/+=/-/-=/</<=/>/>=/==/!= operations, as well as conversions to a raw double for when you really need it. WallTime and MonotonicTime would be wrappers for Seconds with a more limited set of available operations. You can convert WallTime or MonotonicTime to Seconds and vice-versa, but some operators are overloaded to make casts unnecessary in most cases (WallTime + Seconds = WallTime, WallTime - WallTime = Seconds, etc). This would save us from forgetting the unit or the clock. The name of the Seconds class is a dead give-away, and WallTime and MonotonicTime will not yield you a value that is unit-sensitive unless you say something like WallTime::toSeconds(). There will be no easy way to convert WallTime to MonotonicTime and vice-versa, since we want to discourage such conversions.</div>
<div><br></div>
<div>Personally I feel very comfortable with doubles for time. I like to put the word “Seconds” into variable names and function names (waitForSeconds(double) is a dead give-away). On the other hand, I sort of like the idea of a type system to protect clock mix-ups. I think that’s the biggest benefit we got from std::chrono.</div>
<div><br></div>
<div>If it was entirely up to me, I’d go for doubles. I think that there needs to be a high burden of proof for using types to catch semantic bugs. A type system *will* slow you down when writing code, so the EV (expected value) of the time savings from bugs caught early needs to be greater than the EV of the time lost due to spoonfeeding the compiler or having to remember how to use those classes. Although I know that using doubles sometimes meant we had bugs, I don’t think they were frequent or severe enough for the odds to be good for the Seconds/WallTime/MonotonicTime solution.</div>
<div><br></div>
<div>Thoughts?</div>
<div><br></div>
<div>-Filip</div>
<div><br></div></div>
<p> </p><!--SP:m.debski--><!--m.debski:EP-->
<p> </p><img src="http://ext.w1.samsung.net/mailcheck/SeenTimeChecker?do=db8b98bb7a7f8724402b66f89b5cf2c644f37dc2c6fc00abbe6ab1917a07b10d1206aaf9898405b98e8ca330c543ab93df31051f7c759327d6600ca1ffd9c3fdf6ddc9ea09393184cf878f9a26ce15a0" border="0" width="0" height="0" style="display:none"></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>webkit-dev mailing list</span><br><span><a href="mailto:webkit-dev@lists.webkit.org">webkit-dev@lists.webkit.org</a></span><br><span><a href="https://lists.webkit.org/mailman/listinfo/webkit-dev">https://lists.webkit.org/mailman/listinfo/webkit-dev</a></span><br></div></blockquote></body></html>