From 543dfcd4d80c93f311c36bc2f0d98757ddd476d5 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 19 Mar 2013 08:40:52 -0700 Subject: [PATCH 1/3] Optimize thread ready queue pop_front(). --- Core/HLE/sceKernelThread.cpp | 56 +++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 4b847effbb..504df10208 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -431,30 +431,38 @@ public: u32 stackBlock; }; -// std::vector with push_front(), remove(), etc. struct ThreadList { + static const int START_PAD = 0x10; + size_t start; std::vector list; + ThreadList() : start(START_PAD) + { + list.resize(START_PAD); + } + inline bool empty() const { - return list.empty(); + return start == list.size(); } inline size_t size() const { - return list.size(); + return list.size() - start; } inline SceUID &front() { - return list.front(); + return list[start]; } inline void push_front(const SceUID threadID) { if (empty()) push_back(threadID); + else if (start > 0) + list[--start] = threadID; else { size_t oldSize = list.size(); @@ -471,24 +479,52 @@ struct ThreadList inline void pop_front() { - size_t newSize = list.size() - 1; - list.resize(newSize); - if (newSize > 0) - memmove(&list[0], &list[1], newSize * sizeof(SceUID)); + if (start < START_PAD) + ++start; + else + { + size_t newSize = list.size() - 1; + list.resize(newSize); + if (newSize > 0) + memmove(&list[0], &list[1], newSize * sizeof(SceUID)); + } } inline void pop_back() { - list.pop_back(); + if (list.size() > START_PAD) + list.pop_back(); + else + start = START_PAD; } inline void remove(const SceUID threadID) { - list.erase(std::remove(list.begin(), list.end(), threadID), list.end()); + if (empty()) + return; + if (front() == threadID) + { + ++start; + return; + } + + auto new_end = std::remove(list.begin(), list.end(), threadID); + if (new_end - list.begin() >= START_PAD) + list.erase(new_end, list.end()); + // TODO: Probably not efficient, but probably won't hit often hopefully? + else if (new_end != list.end()) + { + ++start; + size_t oldSize = list.size(); + list.resize(START_PAD); + if (oldSize > 0) + memmove(&list[0], &list[1], oldSize * sizeof(SceUID)); + } } void DoState(PointerWrap &p) { + p.Do(start); p.Do(list); } }; From cd8e0e675c953bf1cb24b46e7602c1c31ad51605 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 20 Mar 2013 00:06:19 -0700 Subject: [PATCH 2/3] Small optimization to sceKernelRotateThreadReadyQueue(). --- Core/HLE/sceKernelThread.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 504df10208..104d076bc2 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -1754,20 +1754,21 @@ int sceKernelRotateThreadReadyQueue(int priority) if (priority <= 0x07 || priority > 0x77) return SCE_KERNEL_ERROR_ILLEGAL_PRIORITY; - if (!threadReadyQueue[priority].empty()) + auto &queue = threadReadyQueue[priority]; + if (!queue.empty()) { // In other words, yield to everyone else. if (cur->nt.currentPriority == priority) { - threadReadyQueue[priority].push_back(currentThread); + queue.push_back(currentThread); cur->nt.status = THREADSTATUS_READY; } // Yield the next thread of this priority to all other threads of same priority. else if (threadReadyQueue[priority].size() > 1) { SceUID first = threadReadyQueue[priority].front(); - threadReadyQueue[priority].pop_front(); - threadReadyQueue[priority].push_back(first); + queue.pop_front(); + queue.push_back(first); } hleReSchedule("rotatethreadreadyqueue"); From ea91632ff5539b356f62bc2c97f3b90290ede703 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 20 Mar 2013 08:11:24 -0700 Subject: [PATCH 3/3] Allow the thread ready queue to shrink, simplify. --- Core/HLE/sceKernelThread.cpp | 50 ++++++++++++++---------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/Core/HLE/sceKernelThread.cpp b/Core/HLE/sceKernelThread.cpp index 104d076bc2..763c63676b 100644 --- a/Core/HLE/sceKernelThread.cpp +++ b/Core/HLE/sceKernelThread.cpp @@ -434,6 +434,7 @@ public: struct ThreadList { static const int START_PAD = 0x10; + static const int MIN_PAD = 0x04; size_t start; std::vector list; @@ -459,17 +460,10 @@ struct ThreadList inline void push_front(const SceUID threadID) { - if (empty()) - push_back(threadID); - else if (start > 0) + if (start > 0) list[--start] = threadID; else - { - size_t oldSize = list.size(); - list.resize(oldSize + 1); - memmove(&list[1], &list[0], oldSize * sizeof(SceUID)); - list[0] = threadID; - } + list.insert(list.begin(), threadID); } inline void push_back(const SceUID threadID) @@ -479,23 +473,13 @@ struct ThreadList inline void pop_front() { - if (start < START_PAD) - ++start; - else - { - size_t newSize = list.size() - 1; - list.resize(newSize); - if (newSize > 0) - memmove(&list[0], &list[1], newSize * sizeof(SceUID)); - } + ++start; } inline void pop_back() { - if (list.size() > START_PAD) - list.pop_back(); - else - start = START_PAD; + list.pop_back(); + rebalance(); } inline void remove(const SceUID threadID) @@ -509,16 +493,20 @@ struct ThreadList } auto new_end = std::remove(list.begin(), list.end(), threadID); - if (new_end - list.begin() >= START_PAD) - list.erase(new_end, list.end()); - // TODO: Probably not efficient, but probably won't hit often hopefully? - else if (new_end != list.end()) + if (new_end == list.end()) + return; + + list.erase(new_end, list.end()); + rebalance(); + } + + inline void rebalance() + { + if (list.size() < MIN_PAD) { - ++start; - size_t oldSize = list.size(); - list.resize(START_PAD); - if (oldSize > 0) - memmove(&list[0], &list[1], oldSize * sizeof(SceUID)); + size_t diff = START_PAD - list.size(); + start += diff; + list.insert(list.begin(), diff, 0); } }