Bug 1477512: Part 5 - Rearrange the fields of nsThread for better packing. r?erahm
This takes 16 bytes off of the allocated size of each instance.
MozReview-Commit-ID: AhfN6MWvVL1
--- a/xpcom/threads/nsThread.cpp
+++ b/xpcom/threads/nsThread.cpp
@@ -655,28 +655,28 @@ nsThread::SaveMemoryReportNearOOM(Should
int sCanaryOutputFD = -1;
#endif
nsThread::nsThread(NotNull<SynchronizedEventQueue*> aQueue,
MainThreadFlag aMainThread,
uint32_t aStackSize)
: mEvents(aQueue.get())
, mEventTarget(new ThreadEventTarget(mEvents.get(), aMainThread == MAIN_THREAD))
+ , mShutdownContext(nullptr)
, mScriptObserver(nullptr)
- , mPriority(PRIORITY_NORMAL)
, mThread(nullptr)
+ , mStackSize(aStackSize)
, mNestedEventLoopDepth(0)
- , mStackSize(aStackSize)
- , mShutdownContext(nullptr)
+ , mCurrentEventLoopDepth(-1)
, mShutdownRequired(false)
- , mIsMainThread(aMainThread)
+ , mPriority(PRIORITY_NORMAL)
+ , mIsMainThread(uint8_t(aMainThread))
, mCanInvokeJS(false)
, mCurrentEvent(nullptr)
, mCurrentEventStart(TimeStamp::Now())
- , mCurrentEventLoopDepth(-1)
, mCurrentPerformanceCounter(nullptr)
{
}
nsThread::~nsThread()
{
NS_ASSERTION(mRequestedShutdownContexts.IsEmpty(),
"shouldn't be waiting on other threads to shutdown");
@@ -1098,17 +1098,17 @@ nsThread::ProcessNextEvent(bool aMayWait
// This only applys to the toplevel event loop! Nested event loops (e.g.
// during sync dispatch) are waiting for some state change and must be able
// to block even if something has requested shutdown of the thread. Otherwise
// we'll just busywait as we endlessly look for an event, fail to find one,
// and repeat the nested event loop since its state change hasn't happened yet.
bool reallyWait = aMayWait && (mNestedEventLoopDepth > 0 || !ShuttingDown());
Maybe<Scheduler::EventLoopActivation> activation;
- if (mIsMainThread == MAIN_THREAD) {
+ if (IsMainThread()) {
DoMainThreadSpecificProcessing(reallyWait);
activation.emplace();
}
++mNestedEventLoopDepth;
// We only want to create an AutoNoJSAPI on threads that actually do DOM stuff
// (including workers). Those are exactly the threads that have an
@@ -1143,17 +1143,17 @@ nsThread::ProcessNextEvent(bool aMayWait
activation.ref().SetEvent(event, priority);
}
*aResult = (event.get() != nullptr);
if (event) {
LOG(("THRD(%p) running [%p]\n", this, event.get()));
- if (MAIN_THREAD == mIsMainThread) {
+ if (IsMainThread()) {
BackgroundHangMonitor().NotifyActivity();
}
bool schedulerLoggingEnabled = mozilla::StaticPrefs::dom_performance_enable_scheduler_timing();
if (schedulerLoggingEnabled
&& mNestedEventLoopDepth > mCurrentEventLoopDepth
&& mCurrentPerformanceCounter) {
// This is a recursive call, we're saving the time
@@ -1163,22 +1163,22 @@ nsThread::ProcessNextEvent(bool aMayWait
}
#ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
// If we're on the main thread, we want to record our current runnable's
// name in a static so that BHR can record it.
Array<char, kRunnableNameBufSize> restoreRunnableName;
restoreRunnableName[0] = '\0';
auto clear = MakeScopeExit([&] {
- if (MAIN_THREAD == mIsMainThread) {
+ if (IsMainThread()) {
MOZ_ASSERT(NS_IsMainThread());
sMainThreadRunnableName = restoreRunnableName;
}
});
- if (MAIN_THREAD == mIsMainThread) {
+ if (IsMainThread()) {
nsAutoCString name;
GetLabeledRunnableName(event, name, priority);
MOZ_ASSERT(NS_IsMainThread());
restoreRunnableName = sMainThreadRunnableName;
// Copy the name into sMainThreadRunnableName's buffer, and append a
// terminating null.
@@ -1366,17 +1366,17 @@ nsThread::SetScriptObserver(mozilla::Cyc
MOZ_ASSERT(!mScriptObserver);
mScriptObserver = aScriptObserver;
}
void
nsThread::DoMainThreadSpecificProcessing(bool aReallyWait)
{
- MOZ_ASSERT(mIsMainThread == MAIN_THREAD);
+ MOZ_ASSERT(IsMainThread());
ipc::CancelCPOWs();
if (aReallyWait) {
BackgroundHangMonitor().NotifyWait();
}
// Fire a memory pressure notification, if one is pending.
--- a/xpcom/threads/nsThread.h
+++ b/xpcom/threads/nsThread.h
@@ -177,41 +177,51 @@ protected:
struct nsThreadShutdownContext* ShutdownInternal(bool aSync);
static mozilla::OffTheBooksMutex& ThreadListMutex();
static mozilla::LinkedList<nsThread>& ThreadList();
RefPtr<mozilla::SynchronizedEventQueue> mEvents;
RefPtr<mozilla::ThreadEventTarget> mEventTarget;
+ // The shutdown contexts for any other threads we've asked to shut down.
+ nsTArray<nsAutoPtr<struct nsThreadShutdownContext>> mRequestedShutdownContexts;
+ // The shutdown context for ourselves.
+ struct nsThreadShutdownContext* mShutdownContext;
+
mozilla::CycleCollectedJSContext* mScriptObserver;
- int32_t mPriority;
- uint32_t mThreadId;
PRThread* mThread;
- uint32_t mNestedEventLoopDepth;
- uint32_t mStackSize;
void* mStackBase = nullptr;
+ uint32_t mStackSize;
+ uint32_t mThreadId;
- // The shutdown context for ourselves.
- struct nsThreadShutdownContext* mShutdownContext;
- // The shutdown contexts for any other threads we've asked to shut down.
- nsTArray<nsAutoPtr<struct nsThreadShutdownContext>> mRequestedShutdownContexts;
+ uint32_t mNestedEventLoopDepth;
+ uint32_t mCurrentEventLoopDepth;
mozilla::Atomic<bool> mShutdownRequired;
- MainThreadFlag mIsMainThread;
+
+ int8_t mPriority;
+
+ uint8_t mIsMainThread;
+
+ bool IsMainThread() const
+ {
+ return MainThreadFlag(mIsMainThread) == MAIN_THREAD;
+ }
// Set to true if this thread creates a JSRuntime.
bool mCanInvokeJS;
- mozilla::TimeStamp mNextIdleDeadline;
// Used to track which event is being executed by ProcessNextEvent
nsCOMPtr<nsIRunnable> mCurrentEvent;
+
mozilla::TimeStamp mCurrentEventStart;
- uint32_t mCurrentEventLoopDepth;
+ mozilla::TimeStamp mNextIdleDeadline;
+
RefPtr<mozilla::PerformanceCounter> mCurrentPerformanceCounter;
};
class MOZ_STACK_CLASS nsThreadEnumerator final
{
public:
nsThreadEnumerator()
: mMal(nsThread::ThreadListMutex())