Read a TimeDuration as an integer count of units without a floating-point round trip
Categories
(Core :: mozglue, task)
Tracking
()
People
(Reporter: jlink, Assigned: jlink)
References
Details
TimeDuration's accessors all return double: ToSeconds(), ToMilliseconds() and ToMicroseconds(). A caller that wants an integer has to cast the result, which round-trips an integer tick count through floating point to get back an integer. Around 46 call sites cast the result explicitly, and a handful more apply floor, ceil or llround to it.
Three problems with that. It converts through double for what is integer arithmetic, mirroring Bug 2073111 in the opposite direction. It truncates toward zero rather than rounding, because that is what a cast does. And it loses precision for long durations, since a tick count above 2^53 is not exactly representable.
The arithmetic is already solved. ToTicksAtRate(uint32_t), added in Bug 2053103, returns the duration as an exact integer count of ticks at a given rate, computed with integer/rational arithmetic directly from the platform tick count, rounded to the nearest tick and saturating at the ends of int64_t. Integer milliseconds is ToTicksAtRate(1000) today. What is missing is a discoverable spelling, so that callers reach for it instead of casting a double.
There is also a secondary inefficiency in the double accessors themselves: ToMicroseconds() is defined as ToMilliseconds() * 1000.0, which is ToSeconds() * 1000.0 * 1000.0. Every call therefore does a divide and two multiplies, plus a call into mozglue that cannot be inlined, to produce one conversion. Per-unit entry points would reduce that to one operation.
Two things to decide:
Rounding. ToTicksAtRate rounds to nearest; a cast truncates toward zero. Converting an existing call site is therefore a behaviour change, and each needs checking individually rather than being swept mechanically. Whether the new accessors should round or truncate is the first question - rounding matches the exact-conversion machinery, truncating matches what call sites do today.
Naming. The integer factories in Bug 2073111 could be overloads because they differ by argument type. These cannot, since they differ only by return type, so they need distinct names.
Description
•