Skip to content

Commit

Permalink
Finalize as many completed jobs as possible
Browse files Browse the repository at this point in the history
Instead of finishing one job per thread per frame, now copy job pointers to an on-thread array and run their finalizers in one pass.

This means we can queue potentially 1000s of jobs in a single frame / load step without then needing to finalize them over the next 250 frames.

Potentially investigate an upper limit on the number of finalized jobs; e.g. 100/thread/frame to avoid frame hitches.
  • Loading branch information
Web-eWorks committed May 29, 2021
1 parent 7957a05 commit 986ba8f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/JobQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,31 +206,36 @@ void AsyncJobQueue::Finish(Job *job, const uint8_t threadIdx)
Uint32 AsyncJobQueue::FinishJobs()
{
PROFILE_SCOPED()
Uint32 finished = 0;

// semi-magic number to avoid frivolous reallocs
m_finishedScratch.reserve(32);

const uint32_t numRunners = m_runners.size();
for (uint32_t i = 0; i < numRunners; ++i) {
SDL_LockMutex(m_finishedLock[i]);
if (m_finished[i].empty()) {
SDL_UnlockMutex(m_finishedLock[i]);
continue;

while (!m_finished[i].empty()) {
assert(m_finished[i].front());
m_finishedScratch.push_back(std::move(m_finished[i].front()));
m_finished[i].pop_front();
}
Job *job = m_finished[i].front();
m_finished[i].pop_front();
SDL_UnlockMutex(m_finishedLock[i]);

assert(job);
SDL_UnlockMutex(m_finishedLock[i]);
}

for (Job *job : m_finishedScratch) {
// if its already been cancelled then its taken care of, so we just forget about it
if (!job->cancelled) {
job->UnlinkHandle();
job->OnFinish();
finished++;
}

delete job;
}

Uint32 finished = m_finishedScratch.size();
m_finishedScratch.clear();

return finished;
}

Expand Down Expand Up @@ -441,6 +446,7 @@ void SyncJobQueue::Cancel(Job *job)

Uint32 SyncJobQueue::RunJobs(Uint32 count)
{
PROFILE_SCOPED()
Uint32 executed = 0;
assert(count >= 1);
for (Uint32 i = 0; i < count; ++i) {
Expand Down
2 changes: 2 additions & 0 deletions src/JobQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class AsyncJobQueue : public JobQueue {
SDL_mutex *m_finishedLock[MAX_THREADS];

std::vector<JobRunner *> m_runners;
// scratch space for finishing jobs on main thread
std::vector<Job *> m_finishedScratch;

bool m_shutdown;
};
Expand Down

0 comments on commit 986ba8f

Please sign in to comment.