1325254 - optimize TimerThread data structures

1325254 P1 Make TimerThread::mTimers store RefPtr<nsTimerImpl> objects.

1325254 P2 Make TimerThread list store an entry struct and just drop nsTimerImpl ref on cancel.

1325254 P3 Sort TimerThread list as a binary heap.

1325254 P4 Dynamically allocate Entry structs stored in TimerThread::mTimers.

1325254 P5 Make nsITimer::Cancel() O(c).
This commit is contained in:
win7-7 2024-04-27 20:04:28 +03:00 committed by wuggy
commit f109b02bc3
4 changed files with 157 additions and 43 deletions

View file

@ -32,12 +32,18 @@ extern mozilla::LogModule* GetTimerLog();
{0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8} \
}
class nsTimerImplHolder;
// TimerThread, nsTimerEvent, and nsTimer have references to these. nsTimer has
// a separate lifecycle so we can Cancel() the underlying timer when the user of
// the nsTimer has let go of its last reference.
class nsTimerImpl
{
~nsTimerImpl() {}
~nsTimerImpl()
{
MOZ_ASSERT(!mHolder);
}
public:
typedef mozilla::TimeStamp TimeStamp;
@ -158,6 +164,8 @@ public:
mType == nsITimer::TYPE_REPEATING_SLACK_LOW_PRIORITY;
}
void SetHolder(nsTimerImplHolder* aHolder);
nsCOMPtr<nsIEventTarget> mEventTarget;
void LogFiring(const Callback& aCallback, uint8_t aType, uint32_t aDelay);
@ -168,6 +176,10 @@ public:
uint32_t aType,
Callback::Name aName);
// This weak reference must be cleared by the nsTimerImplHolder by calling
// SetHolder(nullptr) before the holder is destroyed.
nsTimerImplHolder* mHolder;
// These members are set by the initiating thread, when the timer's type is
// changed and during the period where it fires on that thread.
uint8_t mType;
@ -219,4 +231,39 @@ private:
RefPtr<nsTimerImpl> mImpl;
};
// A class that holds on to an nsTimerImpl. This lets the nsTimerImpl object
// directly instruct its holder to forget the timer, avoiding list lookups.
class nsTimerImplHolder
{
public:
explicit nsTimerImplHolder(nsTimerImpl* aTimerImpl)
: mTimerImpl(aTimerImpl)
{
if (mTimerImpl) {
mTimerImpl->SetHolder(this);
}
}
~nsTimerImplHolder()
{
if (mTimerImpl) {
mTimerImpl->SetHolder(nullptr);
}
}
void
Forget(nsTimerImpl* aTimerImpl)
{
if (MOZ_UNLIKELY(!mTimerImpl)) {
return;
}
MOZ_ASSERT(aTimerImpl == mTimerImpl);
mTimerImpl->SetHolder(nullptr);
mTimerImpl = nullptr;
}
protected:
RefPtr<nsTimerImpl> mTimerImpl;
};
#endif /* nsTimerImpl_h___ */