Skip to content

Commit

Permalink
Cap thread counts for workers
Browse files Browse the repository at this point in the history
Because there's already CPUs with 128 cores out there, spawning more
threads than that is pointless and just causes more overhead. Most
thread pools are now capped at 128 threads, but loading ORA files is
capped at 32 because those threads aren't persistent, so the overhead is
greater. These numbers are totally arbitrary, but I don't have a system
like that to test the actual performance on.
  • Loading branch information
askmeaboutlo0m committed Dec 21, 2023
1 parent c39b69f commit a59fd89
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Unreleased Version 2.2.0-pre
* Fix: Make notification sounds work in the AppImage release. Thanks anonymous for reporting.
* Fix: Don't exit the program in the pathological case of initiating a quit, being prompted to save, cancelling the save dialog and then saving again.
* Fix: Properly update current layer fill source when switching layers.
* Fix: Cap the number of threads used for parallel processing, because 128 core CPUs exist.

2023-12-08 Version 2.2.0-beta.11
* Server Fix: No longer show "cannot look up one session and then join another" when joining a session with an ID alias. Thanks Kink and Fabian for finding this.
Expand Down
3 changes: 2 additions & 1 deletion src/desktop/utils/animationrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct FrameRenderJob {
AnimationRenderer::AnimationRenderer(QObject *parent)
: QObject(parent)
, m_worker(DP_worker_new(
64, sizeof(FrameRenderJob), DP_thread_cpu_count(), handleWorkerJob))
64, sizeof(FrameRenderJob), DP_thread_cpu_count(128),
handleWorkerJob))
, m_vmbs(compat::cast_6<compat::sizetype>(DP_worker_thread_count(m_worker)))
, m_batchId(0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/drawdance/libcommon/dpcommon/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ bool DP_semaphore_must_try_wait_at(const char *file, int line,

DP_ThreadId DP_thread_current_id(void);

int DP_thread_cpu_count(void);
int DP_thread_cpu_count(int max);

DP_Thread *DP_thread_new(DP_ThreadFn fn, void *data);

Expand Down
4 changes: 2 additions & 2 deletions src/drawdance/libcommon/dpcommon/threading_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ DP_ThreadId DP_thread_current_id(void)
return (DP_ThreadId)pthread_self();
}

int DP_thread_cpu_count(void)
int DP_thread_cpu_count(int max)
{
static int cpus;
if (cpus == 0) {
cpus = DP_max_int(1, DP_long_to_int(sysconf(_SC_NPROCESSORS_ONLN)));
}
return cpus;
return DP_min_int(cpus, max);
}

static void *run_thread(void *arg)
Expand Down
4 changes: 2 additions & 2 deletions src/drawdance/libcommon/dpcommon/threading_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ extern "C" unsigned long long DP_thread_current_id(void)
#endif
}

extern "C" int DP_thread_cpu_count(void)
extern "C" int DP_thread_cpu_count(int max)
{
return DP_max_int(1, QThread::idealThreadCount());
return DP_min_int(DP_max_int(1, QThread::idealThreadCount()), max);
}

extern "C" DP_Thread *DP_thread_new(DP_ThreadFn fn, void *data)
Expand Down
4 changes: 2 additions & 2 deletions src/drawdance/libcommon/dpcommon/threading_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ DP_ThreadId DP_thread_current_id(void)
return GetCurrentThreadId();
}

int DP_thread_cpu_count(void)
int DP_thread_cpu_count(int max)
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return DP_max_int(1, sysinfo.dwNumberOfProcessors);
return DP_min_int(DP_max_int(1, sysinfo.dwNumberOfProcessors), max);
}

// Wrapper function is needed to change return type. Casting function ptr is UB.
Expand Down
2 changes: 1 addition & 1 deletion src/drawdance/libengine/dpengine/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ static DP_CanvasState *ora_read_stack_xml(DP_ReadOraContext *c,
if ((flags & DP_LOAD_FLAG_SINGLE_THREAD) == 0) {
c->worker =
DP_worker_new(64, sizeof(struct DP_OraLoadLayerContentParams),
DP_thread_cpu_count(), ora_load_layer_content_job);
DP_thread_cpu_count(32), ora_load_layer_content_job);
if (!c->worker) {
DP_warn("Error creating worker: %s", DP_error());
}
Expand Down
2 changes: 1 addition & 1 deletion src/drawdance/libengine/dpengine/paint_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ DP_PaintEngine *DP_paint_engine_new_inc(
pe->reset_locked = false;
pe->paint_thread = DP_thread_new(run_paint_engine, pe);
pe->renderer =
DP_renderer_new(DP_thread_cpu_count(), renderer_tile_fn,
DP_renderer_new(DP_thread_cpu_count(128), renderer_tile_fn,
renderer_unlock_fn, renderer_resize_fn, renderer_user);
pe->meta.acl_change_flags = 0;
DP_VECTOR_INIT_TYPE(&pe->meta.cursor_changes, DP_PaintEngineCursorChange,
Expand Down
2 changes: 1 addition & 1 deletion src/drawdance/libengine/dpengine/save.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ save_animation_frames(DP_CanvasState *cs, const char *path,
int frame_count = count_frames(start, end_inclusive);
DP_Worker *worker = DP_worker_new(DP_int_to_size(frame_count),
sizeof(struct DP_SaveFrameJobParams *),
DP_thread_cpu_count(), save_frame_job);
DP_thread_cpu_count(128), save_frame_job);
if (!worker) {
DP_mutex_free(progress_mutex);
return DP_SAVE_RESULT_INTERNAL_ERROR;
Expand Down

0 comments on commit a59fd89

Please sign in to comment.