From 96938b1549d28d3fb8a22a39366811fe2a375268 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu>
Date: Wed, 28 Dec 2022 16:28:24 +0100
Subject: [PATCH] [TaskScheduler] Avoid copying the `std::function` member when
 assigning `Task` objects

Copy constructor and assignment operator of the `Task` class has been
deleted to avoid unnecessary copies of the `std::function` member, which
can get pretty heavy in some cases.
---
 src/shared/scheduler/TaskScheduler.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/shared/scheduler/TaskScheduler.h b/src/shared/scheduler/TaskScheduler.h
index 6d76a8a86..cca462797 100644
--- a/src/shared/scheduler/TaskScheduler.h
+++ b/src/shared/scheduler/TaskScheduler.h
@@ -166,6 +166,16 @@ private:
          * @param policy The task policy in case of a miss
          */
         explicit Task(function_t function, uint32_t period, Policy policy);
+
+        // Delete copy constructor and copy assignment operator to avoid copying
+        // and force moving
+        Task(const Task& other)            = delete;
+        Task& operator=(const Task& other) = delete;
+
+        // Define a move constructor and a move assignment operator to avoid
+        // copying std::function
+        Task(Task&& other)            = default;
+        Task& operator=(Task&& other) = default;
     };
 
     struct Event
@@ -221,9 +231,9 @@ private:
      */
     void enqueue(Event event, int64_t startTick);
 
-    static TaskStatsResult fromTaskIdPairToStatsResult(Task task, size_t id)
+    static TaskStatsResult fromTaskIdPairToStatsResult(const Task& task,
+                                                       size_t id)
     {
-
         return TaskStatsResult{id,
                                task.period,
                                task.activationStats.getStats(),
-- 
GitLab