Take FunctionView rather than any functor reference in the rtc::Thread::Invoke
to generate less versions of the function template and FunctorMessageHandler helper
thus producing less binary size
Bug: None
Change-Id: Idbd6fb1e1f23b9b2dc4e4306a74ef11e74ba94cd
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161044
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29962}
diff --git a/pc/BUILD.gn b/pc/BUILD.gn
index bc44bbb..001bf02 100644
--- a/pc/BUILD.gn
+++ b/pc/BUILD.gn
@@ -87,6 +87,7 @@
"../api:array_view",
"../api:audio_options_api",
"../api:call_api",
+ "../api:function_view",
"../api:ice_transport_factory",
"../api:libjingle_peerconnection_api",
"../api:rtc_error",
diff --git a/pc/channel.h b/pc/channel.h
index c2b9e40..f59a204 100644
--- a/pc/channel.h
+++ b/pc/channel.h
@@ -19,6 +19,7 @@
#include <vector>
#include "api/call/audio_sink.h"
+#include "api/function_view.h"
#include "api/jsep.h"
#include "api/rtp_receiver_interface.h"
#include "api/transport/media/media_transport_config.h"
@@ -259,8 +260,9 @@
void OnMessage(rtc::Message* pmsg) override;
// Helper function template for invoking methods on the worker thread.
- template <class T, class FunctorT>
- T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
+ template <class T>
+ T InvokeOnWorker(const rtc::Location& posted_from,
+ rtc::FunctionView<T()> functor) {
return worker_thread_->Invoke<T>(posted_from, functor);
}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 41e553c..2de8c4f 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -785,6 +785,7 @@
":checks",
":stringutils",
"../api:array_view",
+ "../api:function_view",
"../api:scoped_refptr",
"../api/task_queue",
"network:sent_packet",
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index 0b8905e..c16c39b 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -469,11 +469,22 @@
}
void Thread::InvokeInternal(const Location& posted_from,
- MessageHandler* handler) {
+ rtc::FunctionView<void()> functor) {
TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line",
posted_from.file_and_line(), "src_func",
posted_from.function_name());
- Send(posted_from, handler);
+
+ class FunctorMessageHandler : public MessageHandler {
+ public:
+ explicit FunctorMessageHandler(rtc::FunctionView<void()> functor)
+ : functor_(functor) {}
+ void OnMessage(Message* msg) override { functor_(); }
+
+ private:
+ rtc::FunctionView<void()> functor_;
+ } handler(functor);
+
+ Send(posted_from, &handler);
}
void Thread::QueuedTaskHandler::OnMessage(Message* msg) {
diff --git a/rtc_base/thread.h b/rtc_base/thread.h
index 186d7f4..f433bab 100644
--- a/rtc_base/thread.h
+++ b/rtc_base/thread.h
@@ -21,6 +21,7 @@
#if defined(WEBRTC_POSIX)
#include <pthread.h>
#endif
+#include "api/function_view.h"
#include "api/task_queue/queued_task.h"
#include "api/task_queue/task_queue_base.h"
#include "rtc_base/constructor_magic.h"
@@ -214,12 +215,20 @@
// See ScopedDisallowBlockingCalls for details.
// NOTE: Blocking invokes are DISCOURAGED, consider if what you're doing can
// be achieved with PostTask() and callbacks instead.
- template <class ReturnT, class FunctorT>
- ReturnT Invoke(const Location& posted_from, FunctorT&& functor) {
- FunctorMessageHandler<ReturnT, FunctorT> handler(
- std::forward<FunctorT>(functor));
- InvokeInternal(posted_from, &handler);
- return handler.MoveResult();
+ template <
+ class ReturnT,
+ typename = typename std::enable_if<!std::is_void<ReturnT>::value>::type>
+ ReturnT Invoke(const Location& posted_from, FunctionView<ReturnT()> functor) {
+ ReturnT result;
+ InvokeInternal(posted_from, [functor, &result] { result = functor(); });
+ return result;
+ }
+
+ template <
+ class ReturnT,
+ typename = typename std::enable_if<std::is_void<ReturnT>::value>::type>
+ void Invoke(const Location& posted_from, FunctionView<void()> functor) {
+ InvokeInternal(posted_from, functor);
}
// Posts a task to invoke the functor on |this| thread asynchronously, i.e.
@@ -369,7 +378,8 @@
// Returns true if there is such a message.
bool PopSendMessageFromThread(const Thread* source, _SendMessage* msg);
- void InvokeInternal(const Location& posted_from, MessageHandler* handler);
+ void InvokeInternal(const Location& posted_from,
+ rtc::FunctionView<void()> functor);
std::list<_SendMessage> sendlist_;
std::string name_;