IDTracker has some unnecessary RefPtr::swap() calls that end up throwing away one of the swapped values

RESOLVED FIXED in Firefox 63

Status

()

enhancement
P2
normal
RESOLVED FIXED
Last year
5 months ago

People

(Reporter: dholbert, Assigned: dholbert)

Tracking

Trunk
mozilla63
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(firefox63 fixed)

Details

Attachments

(1 attachment)

(Kind of related to bug 1287624)

In IDTracker::Reset and IDTracker::ResetWithID, we have the following patterns:

"Reset" snippet (added in bug 344258):
>  if (aWatch) {
>    RefPtr<nsAtom> atom = NS_Atomize(ref);
>    if (!atom)
>      return;
>    atom.swap(mWatchID);
>  }

"ResetWithID" snippet (added in bug 474743):
>  if (aWatch) {
>    RefPtr<nsAtom> atom = aID;
>    atom.swap(mWatchID);
>  }

Here, "atom" is a temporary variable that gets discarded immediately -- when we exit the scope of the "if (aWatch)" check.  So there's no obvious reason that we need to use a swap(...) call -- all we really want to do is replace what's in mWatchID.

Moreover: the "atom" null-check in Reset is unnecessary, because NS_Atomize is documented as "Never returns null":
https://dxr.mozilla.org/mozilla-central/rev/e4e2245fc1428c33db179ca2f242b922ad988d79/xpcom/ds/nsAtom.h#224-225

So we can do away with the |atom| local variable entirely, and we can directly assign mWatchID instead of using RefPtr::swap()
Notes:
* NS_Atomize never returns null, so this patch removes a null check on its
  return value.
* There's no need for the |atom| local variable and for our swap() call on it.
  It's equivalent and simpler for us to just directly assign mWatchID, rather
  than to use this disposable local variable.
Assignee: nobody → dholbert
Status: NEW → ASSIGNED
For reference, mWatchID has type "RefPtr<nsAtom>" and is declared here in IDTracker.h:
>  RefPtr<nsAtom> mWatchID;
https://searchfox.org/mozilla-central/rev/55da592d85c2baf8d8818010c41d9738c97013d2/dom/base/IDTracker.h#206
(I noticed this issue via code inspection of contextual code when reviewing bug 1486488. note to self: wait until that bug lands before landing this, to avoid needlessly bitrotting a larger patch.)
Comment on attachment 9004386 [details]
Bug 1486633: Simplify the logic for updating mWatchID in IDTracker.cpp. r=smaug

Olli Pettay [:smaug] has approved the revision.
Attachment #9004386 - Flags: review+
Pushed by dholbert@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/54beb60ca7d5
Simplify the logic for updating mWatchID in IDTracker.cpp. r=smaug
Priority: -- → P2
https://hg.mozilla.org/mozilla-central/rev/54beb60ca7d5
Status: ASSIGNED → RESOLVED
Closed: Last year
Resolution: --- → FIXED
Target Milestone: --- → mozilla63
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.