From 8cb67fd9fa5f69736aa8b7e7920654022698b34c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 24 Mar 2018 02:19:49 +0100 Subject: [PATCH] Library: inline the thread operation when possible The Lumiera thread-wrapper accepts the operation to be performed within the new thread as a function object, function reference or lambda. Some of these types can be directly instantiated in the threadMain function, and thus possibly inlined altogether. This is especially relevant for Lambdas. OTOH, we can not instantiate function references or bound member functions; in those cases we fall back to using a std::function object, possibly incurring heap allocations. --- src/backend/thread-wrapper.hpp | 7 +++---- src/lib/meta/function.hpp | 9 +++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/backend/thread-wrapper.hpp b/src/backend/thread-wrapper.hpp index 42e9b9246..f6b264906 100644 --- a/src/backend/thread-wrapper.hpp +++ b/src/backend/thread-wrapper.hpp @@ -42,6 +42,7 @@ #include "lib/error.hpp" #include "include/logging.h" +#include "lib/meta/function.hpp" #include "lib/result.hpp" extern "C" { @@ -50,14 +51,11 @@ extern "C" { #include "backend/threadpool-init.hpp" #include -#include #include namespace backend { - using std::bind; - using std::function; using lib::Literal; namespace error = lumiera::error; using error::LUMIERA_ERROR_STATE; @@ -125,7 +123,8 @@ namespace backend { static void threadMain (void* arg) { - function _doIt_{forwardInitialiser (arg)}; + using Fun= typename lib::meta::_Fun::Functor; + Fun _doIt_{forwardInitialiser (arg)}; lumiera_thread_sync (); // sync point: arguments handed over diff --git a/src/lib/meta/function.hpp b/src/lib/meta/function.hpp index 8a268aa62..9e6d9fe6f 100644 --- a/src/lib/meta/function.hpp +++ b/src/lib/meta/function.hpp @@ -104,13 +104,17 @@ namespace meta{ template struct _Fun : std::false_type - { }; + { + using Functor = FUN; + }; /** Specialisation for function objects and lambdas */ template struct _Fun> > : _Fun - { }; + { + using Functor = FUN; + }; /** Specialisation for a bare function signature */ template @@ -120,6 +124,7 @@ namespace meta{ using Ret = RET; using Args = Types; using Sig = RET(ARGS...); + using Functor = std::function; }; /** Specialisation for using a function pointer */ template