From 8f631fe3ad2d7ad256ff2fafb7cb00f7dac9335c Mon Sep 17 00:00:00 2001 From: Joseph Cloutier Date: Sun, 11 Aug 2024 21:35:12 -0400 Subject: [PATCH] Optimize `ThreadPool` slightly. `__activeThreads` and `__idleThreads` only need to be allocated for multi-threaded pools. Plus, there's no benefit to using a `List` here; we only add to and remove from the end. And finally, checking `event.job == null` instead of `isOfType()` is faster and avoids an issue in HTML5. Sadly it is less safe, so we might need to revisit it eventually. --- src/lime/system/ThreadPool.hx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/lime/system/ThreadPool.hx b/src/lime/system/ThreadPool.hx index f997abe69..c33390a00 100644 --- a/src/lime/system/ThreadPool.hx +++ b/src/lime/system/ThreadPool.hx @@ -188,13 +188,13 @@ class ThreadPool extends WorkOutput /** The set of threads actively running a job. **/ - private var __activeThreads:Map = new Map(); + private var __activeThreads:Map; /** A list of idle threads. Not to be confused with `idleThreads`, a public variable equal to `__idleThreads.length`. **/ - private var __idleThreads:List = new List(); + private var __idleThreads:Array; #end private var __jobQueue:JobList = new JobList(); @@ -219,6 +219,14 @@ class ThreadPool extends WorkOutput this.minThreads = minThreads; this.maxThreads = maxThreads; + + #if lime_threads + if (this.mode == MULTI_THREADED) + { + __activeThreads = new Map(); + __idleThreads = []; + } + #end } /** @@ -409,7 +417,7 @@ class ThreadPool extends WorkOutput return; } - if (event.event != WORK || !#if (haxe_ver >= 4.2) Std.isOfType #else Std.is #end (event.job, JobData)) + if (event.event != WORK || event.job == null) { // Go idle. event = null; @@ -492,7 +500,7 @@ class ThreadPool extends WorkOutput job.doWork.makePortable(); #end - var thread:Thread = __idleThreads.isEmpty() ? createThread(__executeThread) : __idleThreads.pop(); + var thread:Thread = __idleThreads.length == 0 ? createThread(__executeThread) : __idleThreads.pop(); __activeThreads[job.id] = thread; thread.sendMessage({event: WORK, job: job}); }