Skip to content

Commit 02ad6c8

Browse files
authored
Use the more optimal overload for copying val (#24694)
I found by accident (while stepping via the debugger) that for `val(v)` where `v` is `val&` C++ chooses the `T&&` overload as more precise one instead of `const val&`. The runtime behaviour of the two is equivalent, so I'm not sure it can be easily tested, but forwarding to the const overload would certainly be a bit more optimal.
1 parent e7d1118 commit 02ad6c8

File tree

1 file changed

+5
-0
lines changed
  • system/include/emscripten

1 file changed

+5
-0
lines changed

system/include/emscripten/val.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ class EMBIND_VISIBILITY_DEFAULT val {
368368
}
369369
}
370370

371+
// Add an explicit overload for `val&` as well.
372+
// Without it, C++ will try to use the `T&&` constructor instead of the more
373+
// efficient `val(const val&)` when trying to copy a `val` instance.
374+
val(val& v) : val(static_cast<const val&>(v)) {}
375+
371376
~val() {
372377
if (uses_ref_count()) {
373378
internal::_emval_decref(as_handle());

0 commit comments

Comments
 (0)