From 804db2dde3f13834891d11b47dc0f0e18b9d6320 Mon Sep 17 00:00:00 2001 From: Karl Wiberg Date: Tue, 15 Sep 2020 11:07:10 +0200 Subject: [PATCH] UntypedFunction: Eliminate an unnecessary indirection in the implementation Bug: webrtc:11943 Change-Id: Ib57b13977910ede430e4228a9d382fb765bf1d84 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/184261 Commit-Queue: Karl Wiberg Reviewed-by: Mirko Bonadei Cr-Commit-Position: refs/heads/master@{#32120} --- rtc_base/function.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rtc_base/function.h b/rtc_base/function.h index cbc5f7dabd..005c360e9e 100644 --- a/rtc_base/function.h +++ b/rtc_base/function.h @@ -32,24 +32,26 @@ template struct CallHelpers; template struct CallHelpers { + // Return type of the three helpers below. using return_type = RetT; + // Complete function type of the three helpers below. + using function_type = RetT(VoidUnion*, ArgT...); + // Helper for calling the `void_ptr` case of VoidUnion. template static RetT CallVoidPtr(VoidUnion* vu, ArgT... args) { return (*static_cast(vu->void_ptr))(std::forward(args)...); } + // Helper for calling the `fun_ptr` case of VoidUnion. static RetT CallFunPtr(VoidUnion* vu, ArgT... args) { return (reinterpret_cast(vu->fun_ptr))( std::forward(args)...); } + // Helper for calling the `inline_storage` case of VoidUnion. template static RetT CallInlineStorage(VoidUnion* vu, ArgT... args) { return (*reinterpret_cast(&vu->inline_storage))( std::forward(args)...); } - static RetT DoCall(FunVoid* f, VoidUnion* vu, ArgT... args) { - return reinterpret_cast(f)( - vu, std::forward(args)...); - } }; } // namespace webrtc_function_impl @@ -188,8 +190,9 @@ class UntypedFunction final { template typename webrtc_function_impl::CallHelpers::return_type Call( ArgT&&... args) { - return webrtc_function_impl::CallHelpers::DoCall( - call_, &f_, std::forward(args)...); + return reinterpret_cast< + typename webrtc_function_impl::CallHelpers::function_type*>( + call_)(&f_, std::forward(args)...); } // Returns true iff we don't need to call a destructor. This is guaranteed