From 3cecc77022c522f11474592cdb5b12b8a0539317 Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Fri, 16 Feb 2024 14:36:17 -0800 Subject: [PATCH 1/9] Skip incref/decref for reserved handles. --- system/include/emscripten/val.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index 81e7c0d2e88f2..ff1739ed07e99 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -49,7 +49,8 @@ enum { _EMVAL_UNDEFINED = 2, _EMVAL_NULL = 4, _EMVAL_TRUE = 6, - _EMVAL_FALSE = 8 + _EMVAL_FALSE = 8, + _EMVAL_LAST_RESERVED_HANDLE = 8, }; typedef struct _EM_DESTRUCTORS* EM_DESTRUCTORS; @@ -385,11 +386,13 @@ class val { } val(const val& v) : val(v.as_handle()) { - internal::_emval_incref(handle); + if (uses_refcount()) { + internal::_emval_incref(handle); + } } ~val() { - if (handle) { + if (uses_refcount()) { internal::_emval_decref(as_handle()); handle = 0; } @@ -630,6 +633,12 @@ class val { : handle(handle), thread(pthread_self()) {} + // Whether this value is a uses incref/decref (true) or is a special reserved + // value (false). + bool uses_refcount() const { + return handle > reinterpret_cast(internal::_EMVAL_LAST_RESERVED_HANDLE); + } + template friend val internal::wrapped_extend(const std::string& , const val& ); @@ -788,7 +797,9 @@ struct BindingType::value && typedef EM_VAL WireType; static WireType toWireType(const val& v) { EM_VAL handle = v.as_handle(); - _emval_incref(handle); + if (uses_refcount()) { + _emval_incref(handle); + } return handle; } static T fromWireType(WireType v) { From c9724825485c6afbb28e40e0f9d8bf53a6a64d6e Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Fri, 16 Feb 2024 14:53:33 -0800 Subject: [PATCH 2/9] Fix function ref. --- system/include/emscripten/val.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index ff1739ed07e99..1e8b3e5573d05 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -797,7 +797,7 @@ struct BindingType::value && typedef EM_VAL WireType; static WireType toWireType(const val& v) { EM_VAL handle = v.as_handle(); - if (uses_refcount()) { + if (v.uses_refcount()) { _emval_incref(handle); } return handle; From 4dd427adca2a3d770bb126a4603dbda9f5fe8abb Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Fri, 16 Feb 2024 15:57:54 -0800 Subject: [PATCH 3/9] Add an rvalue reference version of toWireType. --- system/include/emscripten/val.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index 1e8b3e5573d05..002be3c1aff74 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -405,6 +405,13 @@ class val { return handle; } + // Takes ownership of the handle away from, and invalidates, this instance. + EM_VAL take_handle() { + EM_VAL taken = as_handle(); + handle = 0; + return taken; + } + val& operator=(val&& v) & { val tmp(std::move(v)); this->~val(); @@ -795,6 +802,15 @@ template struct BindingType::value && !std::is_const::value>::type> { typedef EM_VAL WireType; + + // Marshall to JS with move semantics when we can invalidate the temporary val + // object. + static WireType toWireType(val&& v) { + return v.take_handle(); + } + + // Marshal to JS with copy semantics when we cannot transfer the val objects + // reference count. static WireType toWireType(const val& v) { EM_VAL handle = v.as_handle(); if (v.uses_refcount()) { From f5844fed9364d1d35b32e93361493661f0de27ed Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Fri, 16 Feb 2024 16:03:00 -0800 Subject: [PATCH 4/9] rebaseline --- test/code_size/embind_val_wasm.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/code_size/embind_val_wasm.json b/test/code_size/embind_val_wasm.json index 43f4200146319..782ba06bff296 100644 --- a/test/code_size/embind_val_wasm.json +++ b/test/code_size/embind_val_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 431, "a.js": 7080, "a.js.gz": 2999, - "a.wasm": 11428, - "a.wasm.gz": 5724, - "total": 19181, - "total_gz": 9154 + "a.wasm": 11436, + "a.wasm.gz": 5728, + "total": 19189, + "total_gz": 9158 } From 402332072e2ebf8d355eaaaabbcd0c4edff84211 Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Wed, 21 Feb 2024 08:57:32 -0800 Subject: [PATCH 5/9] s/uses_refcount/uses_refcount/ --- system/include/emscripten/val.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index 002be3c1aff74..ab6ce84d788ac 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -386,13 +386,13 @@ class val { } val(const val& v) : val(v.as_handle()) { - if (uses_refcount()) { + if (uses_ref_count()) { internal::_emval_incref(handle); } } ~val() { - if (uses_refcount()) { + if (uses_ref_count()) { internal::_emval_decref(as_handle()); handle = 0; } @@ -642,7 +642,7 @@ class val { // Whether this value is a uses incref/decref (true) or is a special reserved // value (false). - bool uses_refcount() const { + bool uses_ref_count() const { return handle > reinterpret_cast(internal::_EMVAL_LAST_RESERVED_HANDLE); } @@ -813,7 +813,7 @@ struct BindingType::value && // reference count. static WireType toWireType(const val& v) { EM_VAL handle = v.as_handle(); - if (v.uses_refcount()) { + if (v.uses_ref_count()) { _emval_incref(handle); } return handle; From fe46c92a3c6d6de2a5debca0a50b5fdf04c542e5 Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Wed, 21 Feb 2024 15:54:31 -0800 Subject: [PATCH 6/9] s/take_handle/release_handle/g --- system/include/emscripten/val.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index ab6ce84d788ac..be233d087659a 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -406,7 +406,7 @@ class val { } // Takes ownership of the handle away from, and invalidates, this instance. - EM_VAL take_handle() { + EM_VAL release_handle() { EM_VAL taken = as_handle(); handle = 0; return taken; @@ -806,7 +806,7 @@ struct BindingType::value && // Marshall to JS with move semantics when we can invalidate the temporary val // object. static WireType toWireType(val&& v) { - return v.take_handle(); + return v.release_handle(); } // Marshal to JS with copy semantics when we cannot transfer the val objects From 3b8ddde82963a60569a0763c7747181463eabd90 Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Wed, 21 Feb 2024 19:34:03 -0800 Subject: [PATCH 7/9] s/release_handle/release_ownership/g --- system/include/emscripten/val.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/include/emscripten/val.h b/system/include/emscripten/val.h index be233d087659a..9721f66f7999e 100644 --- a/system/include/emscripten/val.h +++ b/system/include/emscripten/val.h @@ -406,7 +406,7 @@ class val { } // Takes ownership of the handle away from, and invalidates, this instance. - EM_VAL release_handle() { + EM_VAL release_ownership() { EM_VAL taken = as_handle(); handle = 0; return taken; @@ -806,7 +806,7 @@ struct BindingType::value && // Marshall to JS with move semantics when we can invalidate the temporary val // object. static WireType toWireType(val&& v) { - return v.release_handle(); + return v.release_ownership(); } // Marshal to JS with copy semantics when we cannot transfer the val objects From 4a25612ba8154fe906a8fc296f2d1dbd22c5faa4 Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Wed, 21 Feb 2024 19:49:46 -0800 Subject: [PATCH 8/9] rebaseline --- test/code_size/embind_val_wasm.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/code_size/embind_val_wasm.json b/test/code_size/embind_val_wasm.json index 782ba06bff296..b116735a43683 100644 --- a/test/code_size/embind_val_wasm.json +++ b/test/code_size/embind_val_wasm.json @@ -5,6 +5,6 @@ "a.js.gz": 2999, "a.wasm": 11436, "a.wasm.gz": 5728, - "total": 19189, + "total": 19184, "total_gz": 9158 } From 94a152a3dc2b12dd67a81cdf9b9736d46dfa8915 Mon Sep 17 00:00:00 2001 From: Mike Rolig Date: Fri, 23 Feb 2024 13:41:17 -0800 Subject: [PATCH 9/9] rebaseline --- test/code_size/embind_val_wasm.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/code_size/embind_val_wasm.json b/test/code_size/embind_val_wasm.json index b116735a43683..8f7f51e0dd8ed 100644 --- a/test/code_size/embind_val_wasm.json +++ b/test/code_size/embind_val_wasm.json @@ -3,8 +3,8 @@ "a.html.gz": 431, "a.js": 7080, "a.js.gz": 2999, - "a.wasm": 11436, - "a.wasm.gz": 5728, + "a.wasm": 11431, + "a.wasm.gz": 5727, "total": 19184, - "total_gz": 9158 + "total_gz": 9157 }