Kernel: a mask of the non-empty ready queues, for best() and sliceContender()

firstReady() scanned the 128 priority queues and sliceContender() up to the running thread's priority at every
pick, every transfer and every updateLimit (each out-call): Kernel::best was 2.6% of the game thread in a 1P match.
The mask is kept after every change to a queue; the choices are the same.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Thomas MartinandClaude Opus 5.5 committed 2026-11-04 23:15:23 +01:00
1 parent 7f0a6eeb04
commit 24292a623e
2 files changed
+27 -8

No files matched your search

+25 -8
View File
@@ -1,6 +1,8 @@
// BozoEngine's EE kernel. See bozo/ee/kernel.h and docs/specs/kernel.md.
#include "bozo/ee/kernel.h"
#include <bit>
#include <algorithm>
namespace bozo::ee
@@ -60,11 +62,23 @@ namespace bozo::ee
// The ready queues hold the ready threads; the running thread is outside them (calllog: a thread that gives way
// goes to the tail of its queue, behind the threads already there).
void Kernel::noteQueue(uint32_t p)
{
const uint64_t bit = 1ull << (p & 63u);
if (ready_[p].empty())
readyMask_[p >> 6] &= ~bit;
else
readyMask_[p >> 6] |= bit;
}
int32_t Kernel::firstReady() const
{
for (const auto &q : ready_)
if (!q.empty())
return q.front();
// The lowest non-empty queue, by the mask of non-empty queues (the dispatcher asks at every pick, and at every
// transfer: scanning 128 queues was ~2.5% of a 1P match's game thread).
if (readyMask_[0])
return ready_[std::countr_zero(readyMask_[0])].front();
if (readyMask_[1])
return ready_[64 + std::countr_zero(readyMask_[1])].front();
return 0;
}
@@ -86,11 +100,10 @@ namespace bozo::ee
auto it = threads_.find(running_);
if (running_ <= 0 || it == threads_.end() || it->second->state != ThreadState::Running)
return false;
const uint32_t p = it->second->priority % kPriorities;
for (uint32_t q = 0; q <= p; ++q)
if (!ready_[q].empty())
return true;
return false;
const uint32_t p = it->second->priority % kPriorities; // any non-empty queue at p or below
if (p < 64)
return (readyMask_[0] & (p == 63 ? ~0ull : (2ull << p) - 1u)) != 0;
return readyMask_[0] != 0 || (readyMask_[1] & (p == 127 ? ~0ull : (2ull << (p - 64)) - 1u)) != 0;
}
void Kernel::runOrder(std::vector<int32_t> &out) const
@@ -134,12 +147,14 @@ namespace bozo::ee
{
t.state = ThreadState::Ready;
ready_[t.priority % kPriorities].push_back(t.id);
noteQueue(t.priority % kPriorities);
}
void Kernel::removeFromQueue(Thread &t)
{
auto &q = ready_[t.priority % kPriorities];
q.erase(std::remove(q.begin(), q.end(), t.id), q.end());
noteQueue(t.priority % kPriorities);
}
void Kernel::deschedule(bool toTail)
@@ -156,6 +171,7 @@ namespace bozo::ee
q.push_back(t->id);
else
q.push_front(t->id);
noteQueue(t->priority % kPriorities);
}
}
running_ = 0;
@@ -275,6 +291,7 @@ namespace bozo::ee
removeFromQueue(*t);
t->priority = priority;
ready_[priority % kPriorities].push_back(t->id);
noteQueue(priority % kPriorities);
}
else
t->priority = priority;
+2
View File
@@ -332,6 +332,8 @@ namespace bozo::ee
std::map<int32_t, std::unique_ptr<Thread>> threads_;
std::array<std::deque<int32_t>, kPriorities> ready_;
uint64_t readyMask_[2] = {0, 0}; // bit p: ready_[p] not empty (kept by noteQueue after every change)
void noteQueue(uint32_t p);
int32_t running_ = 0;
bool switchRequested_ = false;
uint64_t picks_ = 0; // a rotation of the running thread's queue: the next transfer gives way