From a5137cdd9d599492aa46b56729db663760159c50 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Sun, 13 Jul 2025 13:27:42 +0100 Subject: [PATCH 1/2] Use the more optimal overload for copying `val` 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. --- system/include/emscripten/val.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index dd44f3f8adb31..b48c7a2867d19 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -368,6 +368,8 @@ class EMBIND_VISIBILITY_DEFAULT val { } } + val(val& v) : val(static_cast(v)) {} + ~val() { if (uses_ref_count()) { internal::_emval_decref(as_handle()); From 4b27a8d5d8c3aec417ef6ee721cabb2d4c38ec94 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 15 Jul 2025 01:31:25 +0100 Subject: [PATCH 2/2] Add comment --- system/include/emscripten/val.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index b48c7a2867d19..64fc4a0556f24 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -368,6 +368,9 @@ class EMBIND_VISIBILITY_DEFAULT val { } } + // Add an explicit overload for `val&` as well. + // Without it, C++ will try to use the `T&&` constructor instead of the more + // efficient `val(const val&)` when trying to copy a `val` instance. val(val& v) : val(static_cast(v)) {} ~val() {