Avoid dynamic_cast() for threads where possible.

It's reasonable to trust currentThread and threadReadyQueue, and these eat
a few percentage points of time.
This commit is contained in:
Unknown W. Brackets 2013-03-16 11:17:44 -07:00
parent d8c0a9410c
commit 14e065cbb2
2 changed files with 18 additions and 9 deletions

View File

@ -383,10 +383,23 @@ public:
} }
} }
// ONLY use this when you know the handle is valid.
template <class T>
T *GetFast(SceUID handle)
{
const SceUID realHandle = handle - handleOffset;
if (realHandle < 0 || realHandle >= maxCount || !occupied[realHandle])
{
ERROR_LOG(HLE, "Kernel: Bad fast object handle %i (%08x)", handle, handle);
return 0;
}
return static_cast<T *>(pool[realHandle]);
}
template <class T> template <class T>
T* GetByModuleByEntryAddr(u32 entryAddr) T* GetByModuleByEntryAddr(u32 entryAddr)
{ {
for (int i=0; i <4096; i++) for (int i = 0; i < maxCount; ++i)
{ {
T* t = dynamic_cast<T*>(pool[i]); T* t = dynamic_cast<T*>(pool[i]);

View File

@ -595,12 +595,9 @@ void MipsCall::setReturnValue(u64 value)
savedV1 = (value >> 32) & 0xFFFFFFFF; savedV1 = (value >> 32) & 0xFFFFFFFF;
} }
// TODO: Should move to this wrapper so we can keep the current thread as a SceUID instead
// of a dangerous raw pointer.
Thread *__GetCurrentThread() { Thread *__GetCurrentThread() {
u32 error;
if (currentThread != 0) if (currentThread != 0)
return kernelObjects.Get<Thread>(currentThread, error); return kernelObjects.GetFast<Thread>(currentThread);
else else
return NULL; return NULL;
} }
@ -784,9 +781,8 @@ bool __KernelSwitchOffThread(const char *reason)
if (current && current->isRunning()) if (current && current->isRunning())
__KernelChangeReadyState(current, threadID, true); __KernelChangeReadyState(current, threadID, true);
u32 error;
// Idle 0 chosen entirely arbitrarily. // Idle 0 chosen entirely arbitrarily.
Thread *t = kernelObjects.Get<Thread>(threadIdleID[0], error); Thread *t = kernelObjects.GetFast<Thread>(threadIdleID[0]);
if (t) if (t)
{ {
__KernelSwitchContext(t, reason); __KernelSwitchContext(t, reason);
@ -1275,9 +1271,9 @@ Thread *__KernelNextThread() {
} }
} }
u32 error; // Assume threadReadyQueue has not become corrupt.
if (bestThread != -1) if (bestThread != -1)
return kernelObjects.Get<Thread>(bestThread, error); return kernelObjects.GetFast<Thread>(bestThread);
else else
return 0; return 0;
} }