Skip to content

Commit 3be49d6

Browse files
RaisinTentargos
authored andcommitted
src: prevent extra copies of TimerWrap::TimerCb
I noticed that we were taking `TimerCb` as a `const&` and then copying that into the member. This is completely fine when the constructor is called with an lvalue. However, when called with an rvalue, we can allow the `std::function` to be moved into the member instead of falling back to a copy, so I changed the constructors to take in universal references. Also, `std::function` constructors can take in multiple arguments, so I further modified the constructors to use variadic templates. Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40665 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent a5a1691 commit 3be49d6

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@
642642
'src/tracing/trace_event_common.h',
643643
'src/tracing/traced_value.h',
644644
'src/timer_wrap.h',
645+
'src/timer_wrap-inl.h',
645646
'src/tty_wrap.h',
646647
'src/udp_wrap.h',
647648
'src/util.h',

src/inspector_agent.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "node_process-inl.h"
1616
#include "node_url.h"
1717
#include "util-inl.h"
18-
#include "timer_wrap.h"
18+
#include "timer_wrap-inl.h"
1919
#include "v8-inspector.h"
2020
#include "v8-platform.h"
2121

src/timer_wrap-inl.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef SRC_TIMER_WRAP_INL_H_
2+
#define SRC_TIMER_WRAP_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "timer_wrap.h"
7+
8+
#include <utility>
9+
10+
#include "env.h"
11+
#include "uv.h"
12+
13+
namespace node {
14+
15+
template <typename... Args>
16+
inline TimerWrap::TimerWrap(Environment* env, Args&&... args)
17+
: env_(env), fn_(std::forward<Args>(args)...) {
18+
uv_timer_init(env->event_loop(), &timer_);
19+
timer_.data = this;
20+
}
21+
22+
template <typename... Args>
23+
inline TimerWrapHandle::TimerWrapHandle(Environment* env, Args&&... args) {
24+
timer_ = new TimerWrap(env, std::forward<Args>(args)...);
25+
env->AddCleanupHook(CleanupHook, this);
26+
}
27+
28+
} // namespace node
29+
30+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
31+
32+
#endif // SRC_TIMER_WRAP_INL_H_

src/timer_wrap.cc

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1+
#include "timer_wrap.h" // NOLINT(build/include_inline)
2+
#include "timer_wrap-inl.h"
3+
14
#include "env-inl.h"
25
#include "memory_tracker-inl.h"
3-
#include "timer_wrap.h"
46
#include "uv.h"
57

68
namespace node {
79

8-
TimerWrap::TimerWrap(Environment* env, const TimerCb& fn)
9-
: env_(env),
10-
fn_(fn) {
11-
uv_timer_init(env->event_loop(), &timer_);
12-
timer_.data = this;
13-
}
14-
1510
void TimerWrap::Stop() {
1611
if (timer_.data == nullptr) return;
1712
uv_timer_stop(&timer_);
@@ -48,13 +43,6 @@ void TimerWrap::OnTimeout(uv_timer_t* timer) {
4843
t->fn_();
4944
}
5045

51-
TimerWrapHandle::TimerWrapHandle(
52-
Environment* env,
53-
const TimerWrap::TimerCb& fn) {
54-
timer_ = new TimerWrap(env, fn);
55-
env->AddCleanupHook(CleanupHook, this);
56-
}
57-
5846
void TimerWrapHandle::Stop() {
5947
if (timer_ != nullptr)
6048
return timer_->Stop();

src/timer_wrap.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class TimerWrap final : public MemoryRetainer {
1616
public:
1717
using TimerCb = std::function<void()>;
1818

19-
TimerWrap(Environment* env, const TimerCb& fn);
19+
template <typename... Args>
20+
explicit inline TimerWrap(Environment* env, Args&&... args);
21+
2022
TimerWrap(const TimerWrap&) = delete;
2123

2224
inline Environment* env() const { return env_; }
@@ -50,9 +52,8 @@ class TimerWrap final : public MemoryRetainer {
5052

5153
class TimerWrapHandle : public MemoryRetainer {
5254
public:
53-
TimerWrapHandle(
54-
Environment* env,
55-
const TimerWrap::TimerCb& fn);
55+
template <typename... Args>
56+
explicit inline TimerWrapHandle(Environment* env, Args&&... args);
5657

5758
TimerWrapHandle(const TimerWrapHandle&) = delete;
5859

0 commit comments

Comments
 (0)