@@ -45,6 +45,8 @@ struct Coroutine {
void *data;
CoroutineEntry *entry;
+ QLIST_ENTRY(Coroutine) pool_next;
+
jmp_buf env;
};
@@ -24,16 +24,35 @@
#include "qemu-coroutine.h"
#include "qemu-coroutine-int.h"
+enum {
+ /* Maximum free pool size prevents holding too many freed coroutines */
+ POOL_MAX_SIZE = 64,
+};
+
+static QLIST_HEAD(, Coroutine) pool = QLIST_HEAD_INITIALIZER(&pool);
+static unsigned int pool_size;
static Coroutine leader;
static Coroutine *current;
-static void coroutine_terminate(Coroutine *co)
+static void coroutine_delete(Coroutine *co)
{
- trace_qemu_coroutine_terminate(co);
qemu_free(co->stack);
qemu_free(co);
}
+static void coroutine_terminate(Coroutine *co)
+{
+ trace_qemu_coroutine_terminate(co);
+
+ if (pool_size < POOL_MAX_SIZE) {
+ QLIST_INSERT_HEAD(&pool, co, pool_next);
+ co->caller = NULL;
+ pool_size++;
+ } else {
+ coroutine_delete(co);
+ }
+}
+
static Coroutine *coroutine_new(void)
{
const size_t stack_size = 4 << 20;
@@ -49,7 +68,15 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
{
Coroutine *co;
- co = coroutine_new();
+ co = QLIST_FIRST(&pool);
+
+ if (co) {
+ QLIST_REMOVE(co, pool_next);
+ pool_size--;
+ } else {
+ co = coroutine_new();
+ }
+
co->entry = entry;
return co;
This patch speeds up coroutine creation by reusing freed coroutines. When a coroutine terminates it is placed in the pool instead of having its resources freed. The next time a coroutine is created it can be taken straight from the pool and requires no initialization. Performance results on an Intel Core2 Duo T9400 (2.53GHz) for ./check-coroutine --benchmark-lifecycle 20000000: No pooling: 19.5 sec With pooling: 1.1 sec Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> --- qemu-coroutine-int.h | 2 ++ qemu-coroutine.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-)