Closed Bug 1446509 Opened 6 years ago Closed 6 years ago

Create 'final' versions of AddRef and Release macros

Categories

(Core :: XPCOM, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla63
Tracking Status
firefox61 --- wontfix
firefox62 --- wontfix
firefox63 --- fixed

People

(Reporter: tjr, Assigned: Alex_Gaynor)

References

(Blocks 2 open bugs)

Details

(Whiteboard: [overhead:noted])

Attachments

(1 file)

Bug 1332680 has identified a lot of Release and AddRef members on various classes that could be marked final and enable devirtualiation.  But those members are generated from macros, so we would need macro versions that have the final keyword.
Summary: Create 'fina' versions of AddRef and Release macros → Create 'final' versions of AddRef and Release macros
Can the classes themselves not be marked `final`?  Or is this mostly applicable to classes like `Runnable`, which are intended to be subclassed, but AddRef/Release are basically not overridable by subclasses, so those methods should be marked `final`?
Flags: needinfo?(tom)
(In reply to Nathan Froyd [:froydnj] from comment #1)
> Can the classes themselves not be marked `final`?  

Only some can.

> Or is this mostly
> applicable to classes like `Runnable`, which are intended to be subclassed,
> but AddRef/Release are basically not overridable by subclasses, so those
> methods should be marked `final`?

Precisely.
Flags: needinfo?(tom)
NS_INLINE_DECL_THREADSAFE_REFCOUNTING, at least, supports passing method attributes as __VA_ARGS__, so we could easily pass `final` there.
How hard would it be to add the same capability to NS_IMPL_CYCLE_COLLECTING_ADDREF and friends, which seem to be very common instances of this pattern?
Changing the macro is two lines of code:

#define NS_IMPL_CYCLE_COLLECTING_ADDREF(klass, ...) \
  NS_IMETHODIMPL_(MozExternalRefCountType) klass::AddRef(void) __VA_ARGS__ \
  ...

But maybe this particular macro, we can just say things are always `final`?  Andrew, how common/smart would it be to override AddRef/Release in cycle collected classes?
Flags: needinfo?(continuation)
NS_DECL_ISUPPORTS_INHERITED is very common (this isn't only CCed classes, of course). I see 486 instances of it (including generated code). However, the actual implementation of AddRef and Release for classes that use this macro will be trivial. NS_IMPL_ADDREF/RELEASE_INHERITED are trivial wrappers to make refcount logging better, which is only used in NS_BUILD_REFCNT_LOGGING (aka debug builds). I've assumed that any whole program optimization we use would be smart enough to boil that off and devirtualize these methods but I've never checked it. Potentially they could be made final in opt builds but not in debug builds, though that feels sketchy.
Flags: needinfo?(continuation)
I'd be in favor of somehow making NS_IMPL_ADDREF/RELASE_INHERITED be essentially no-ops in opt builds: slightly less code to drag around because we're not generating methods that are just forwarding methods.  That'd require a bit of care of NS_DECL_ISUPPORTS_INHERITED, though...

I think it'd be a pretty small win, though, as far as code size goes.  Slightly better icache performance?
Depends on if the compiler/linker is really managing to devirtualize them or not. If they're not getting devirtualized it's potentially a more sizable win.
I should be more precise and say that AddRef and Release still have to be virtual methods in general, but specific call sites that know about any concrete class should potentially be able to make non-virtual calls to AddRef and Release. (Well, I'm not sure what happens with multiple inheritance).
Whiteboard: [overhead:noted]
Assignee: nobody → agaynor
Comment on attachment 8997112 [details]
Bug 1446509 - added final versions of macro for declaring AddRef and Decref; r?froydnj

Nathan Froyd [:froydnj] has approved the revision.

https://phabricator.services.mozilla.com/D2678
Attachment #8997112 - Flags: review+
Keywords: checkin-needed
Pushed by btara@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/8a23e3023c92
added final versions of macro for declaring AddRef and Decref; r=froydnj
Keywords: checkin-needed
https://hg.mozilla.org/mozilla-central/rev/8a23e3023c92
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla63
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: