Closed Bug 1203350 Opened 9 years ago Closed 9 years ago

Unify certain arithmetic operators in TimeStamp

Categories

(Core :: mozglue, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla43
Tracking Status
firefox43 --- fixed

People

(Reporter: birtles, Assigned: birtles)

References

Details

Attachments

(1 file)

Follow-up from bug 1202556, we should remove duplicate code between operator+ and operator+= by writing one in terms of the other. Likewise for subtraction.
Attachment #8658995 - Flags: review?(nfroyd) → review+
Thank you!
Comment on attachment 8658995 [details] [diff] [review]
Unify arithmetic operators in TimeStamp

Review of attachment 8658995 [details] [diff] [review]:
-----------------------------------------------------------------

One minor thing I thought of after the fact:

::: mozglue/misc/TimeStamp.h
@@ +491,5 @@
>  
>    TimeStamp operator+(const TimeDuration& aOther) const
>    {
> +    TimeStamp result = *this;
> +    return result += aOther;

Actually, can you write this as:

result += aOther;
return result;

so the compiler's named-value return optimization kicks in?  I'm unsure of whether the right thing would happen with trying to copy-constructor the returned reference from operator+=.

@@ +496,5 @@
>    }
>    TimeStamp operator-(const TimeDuration& aOther) const
>    {
> +    TimeStamp result = *this;
> +    return result -= aOther;

Likewise here.
(In reply to Nathan Froyd [:froydnj] from comment #3)
> Actually, can you write this as:
> 
> result += aOther;
> return result;
> 
> so the compiler's named-value return optimization kicks in?  I'm unsure of
> whether the right thing would happen with trying to copy-constructor the
> returned reference from operator+=.

Yeah, I was wondering the exact same thing. I did some digging around to see if compiler's would apply RVO if I rolled up the two statements and I got the impression from MSVC's docs that it probably would (it only called out returning separately-named objects as problematic) but better to be safe.
https://hg.mozilla.org/mozilla-central/rev/b2d47f9818ce
Status: ASSIGNED → RESOLVED
Closed: 9 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla43
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: