You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by sl...@apache.org on 2023/01/09 20:05:54 UTC
[daffodil] 01/04: Use a cached thread pool for coroutine threads
This is an automated email from the ASF dual-hosted git repository.
slawrence pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git
commit 1df4bcda00d395e0b71b999def359d6f769281c8
Author: Steve Lawrence <sl...@apache.org>
AuthorDate: Thu Jan 5 14:50:40 2023 -0500
Use a cached thread pool for coroutine threads
Instead of creating a new thread everytime a new coroutine is created
(i.e. everytime we unparse something with SAX), coroutines now get
threads from a cached thread pool. This allows reuse of previously
created threads, avoiding the overhead of creating threads.
DAFFODIL-2400
---
.../org/apache/daffodil/util/Coroutines.scala | 38 +++++++++++++++++++---
1 file changed, 33 insertions(+), 5 deletions(-)
diff --git a/daffodil-lib/src/main/scala/org/apache/daffodil/util/Coroutines.scala b/daffodil-lib/src/main/scala/org/apache/daffodil/util/Coroutines.scala
index 3dca4c19a..472bb1538 100644
--- a/daffodil-lib/src/main/scala/org/apache/daffodil/util/Coroutines.scala
+++ b/daffodil-lib/src/main/scala/org/apache/daffodil/util/Coroutines.scala
@@ -20,10 +20,39 @@
import org.apache.daffodil.exceptions.UnsuppressableException
import java.util.concurrent.ArrayBlockingQueue
+ import java.util.concurrent.Executors
+
+ import scala.concurrent.ExecutionContext
+ import scala.concurrent.Future
import scala.util.Failure
import scala.util.Success
import scala.util.Try
+ object Coroutine {
+ /**
+ * This execution context should be used when creating Coroutine threads to
+ * improve performance. Creating threads has high overhead, but getting
+ * threads from a thread pool we reduce some of that overhead.
+ *
+ * This thread pool is a cached thread pool, which means unused threads are
+ * closed after 60 seconds of unuse. This also places no limits on the
+ * number of threads that will be created, so if many coroutines are created
+ * at the same time we could potentially starve the system of
+ * threads/processing. However, we only create one thread per coroutine, and
+ * we currently only create one coroutine per SAX unparse, and it is
+ * hopefully unlikely that a user will create enough parallel SAX unparse
+ * calls to cause issues.
+ */
+ val executionContext = new ExecutionContext {
+ private val threadPool = Executors.newCachedThreadPool()
+ def execute(runnable: Runnable): Unit = threadPool.submit(runnable)
+
+ //$COVERAGE-OFF$
+ def reportFailure(t: Throwable): Unit = {} //do nothing
+ //$COVERAGE-ON$
+ }
+ }
+
/**
* General purpose Co-routines.
*
@@ -55,15 +84,14 @@
*/
protected def isMain: Boolean = false
- private var thread_ : Option[Thread] = None
+ private var thread_ : Option[Future[Unit]] = None
private final def init(): Unit = {
if (!isMain && thread_.isEmpty) {
- val thr = new Thread {
- override def run(): Unit = self.run()
- }
+ val thr = Future {
+ self.run()
+ } (Coroutine.executionContext)
thread_ = Some(thr)
- thr.start()
}
}